-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathWebSocket.java
More file actions
executable file
·161 lines (139 loc) · 4.01 KB
/
Copy pathWebSocket.java
File metadata and controls
executable file
·161 lines (139 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package javaforce.service;
import java.io.*;
import java.net.*;
import javaforce.*;
/** WebSocket
*
* @author pquiring
*
* See : RFC 6455
*/
public class WebSocket {
protected InputStream is;
protected OutputStream os;
protected String host;
protected String url;
protected String[] cookies;
private boolean connected = true;
private String server_host;
private String client_host;
public static final int TYPE_CONT = 0x0; //do not use
public static final int TYPE_TEXT = 0x1;
public static final int TYPE_BINARY = 0x2;
protected static final int TYPE_CLOSE = 0x08;
protected static final int TYPE_PING = 0x09;
protected static final int TYPE_PONG = 0x0a;
public static final int FIN = 0x80;
public static final int MASK = 0x80;
public WebSocket(String server_host, String client_host, InputStream is, OutputStream os, String host, String url, String[] cookies) {
if (server_host.equals("0:0:0:0:0:0:0:1")) {
server_host = "127.0.0.1";
}
this.server_host = server_host;
if (client_host.equals("0:0:0:0:0:0:0:1")) {
client_host = "127.0.0.1";
}
this.client_host = client_host;
this.is = is;
this.os = os;
this.host = host;
this.url = url;
this.cookies = cookies;
}
/** Free to use data */
public Object userobj;
/** Returns URL used during WebSocket connection request. */
public String getURL() {
return url;
}
public String getClientHost() {
return client_host;
}
public String getServerHost() {
return server_host;
}
public String getHost() {
return host;
}
public InputStream getInputStream() {
return is;
}
public OutputStream getOutputStream() {
return os;
}
public String[] getCookies() {
return cookies;
}
public String getCookie(String name) {
name += "=";
for(int a=0;a<cookies.length;a++) {
if (cookies[a].startsWith(name)) {
return cookies[a].substring(name.length());
}
}
return null;
}
public void addCookie(String name, String value) {
int length = cookies.length;
String[] new_cookies = new String[length + 1];
System.arraycopy(cookies, 0, new_cookies, 0, length);
cookies = new_cookies;
cookies[length] = name + "=" + value;
}
/** Writes a WebSocket message to client.
* Type = TYPE_TEXT
*/
public boolean write(byte[] msg) {
return write(msg, TYPE_TEXT);
}
/** Writes a WebSocket message to client.
* @param type = TYPE_...
*/
public boolean write(byte[] msg, int type) {
try {
//encode a packet and write it
int len = msg.length;
if (len > 16777216) {
throw new Exception("WebSocket message > 16MB");
}
if (len > 65535) {
//create 64bit packet length
byte[] header = new byte[10];
header[0] = (byte)(FIN | type);
header[1] = 127;
//bytes 2-6 : not supported (64bit length), only support 24bit
header[7] = (byte)((len & 0xff0000) >> 16);
header[8] = (byte)((len & 0xff00) >> 8);
header[9] = (byte)(len & 0xff);
os.write(header);
} else if (len >= 126) {
//create 16bit packet length
byte[] header = new byte[4];
header[0] = (byte)(FIN | type);
header[1] = 126;
header[2] = (byte)((len & 0xff00) >> 8);
header[3] = (byte)(len & 0xff);
os.write(header);
} else {
//create 7bit packet length
byte[] header = new byte[2];
header[0] = (byte)(FIN | type);
header[1] = (byte)len;
os.write(header);
}
os.write(msg);
} catch (SocketException e) {
connected = false;
JFLog.logTrace("WebSocket closed:" + client_host);
return false;
} catch (Exception e) {
connected = false;
JFLog.log(e);
return false;
}
return true;
}
public boolean isConnected() {
return connected;
}
}