forked from clwillingham/java-socket.io.client
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathIOWebSocket.java
189 lines (152 loc) · 4.06 KB
/
IOWebSocket.java
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package com.clwillingham.socket.io;
import java.io.IOException;
import java.net.URI;
import java.util.Timer;
import java.util.TimerTask;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import net.tootallnate.websocket.WebSocketClient;
public class IOWebSocket extends WebSocketClient {
private Timer closeTimeout;
private MessageCallback callback;
private IOSocket ioSocket;
private static int currentID = 0;
private String namespace;
public IOWebSocket(URI arg0, IOSocket ioSocket, MessageCallback callback) {
super(arg0);
this.callback = callback;
this.ioSocket = ioSocket;
}
@Override
public void onIOError(IOException arg0) {
// TODO Auto-generated method stub
}
@Override
public void onMessage(String arg0) {
// The javascript socket.io client doesn't seem
// to use the hearbeat timeout at all, just the close
// timeout.
clearCloseTimeout();
setCloseTimeout();
IOMessage message = IOMessage.parseMsg(arg0);
switch (message.getType()) {
case IOMessage.HEARTBEAT:
try {
send("2::");
System.out.println("HeartBeat written to server");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case IOMessage.MESSAGE:
callback.onMessage(message.getMessageData());
break;
case IOMessage.JSONMSG:
try {
callback.onMessage(new JSONObject(message.getMessageData()));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case IOMessage.EVENT:
try {
JSONObject event = new JSONObject(message.getMessageData());
JSONArray args = event.getJSONArray("args");
JSONObject[] argsArray = new JSONObject[args.length()];
for (int i = 0; i < args.length(); i++) {
argsArray[i] = args.getJSONObject(i);
}
String eventName = event.getString("name");
callback.on(eventName, argsArray);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case IOMessage.CONNECT:
ioSocket.onConnect();
break;
case IOMessage.ACK:
String[] parts = message.getMessageData().split("\\+", 2);
int ackId = Integer.parseInt(parts[0]);
JSONArray data = null;
if (parts.length > 1) {
try {
data = new JSONArray(parts[1]);
} catch (JSONException e) {
e.printStackTrace();
}
}
ioSocket.onAcknowledge(ackId, data);
break;
case IOMessage.ERROR:
case IOMessage.DISCONNECT:
//TODO
break;
}
}
@Override
public void onOpen() {
try {
if (!namespace.equals("")) {
init(namespace);
}
} catch (IOException e) {
e.printStackTrace();
}
ioSocket.onOpen();
setCloseTimeout();
}
@Override
public void onClose() {
ioSocket.onClose();
ioSocket.onDisconnect();
}
public void init(String path) throws IOException{
send("1::"+path);
}
public void init(String path, String query) throws IOException{
this.send("1::"+path+"?"+query);
}
public void sendMessage(IOMessage message) throws IOException{
send(message.toString());
}
public void sendMessage(String message) throws IOException{
send(new Message(message).toString());
}
public static int genID(){
currentID++;
return currentID;
}
public void setNamespace(String ns) {
namespace = ns;
}
public String getNamespace() {
return namespace;
}
private void setCloseTimeout() {
closeTimeout = new Timer();
closeTimeout.schedule(new CloseTask(), ioSocket.getClosingTimeout());
}
private void clearCloseTimeout() {
if (closeTimeout != null) {
closeTimeout.cancel();
closeTimeout = null;
}
}
private class CloseTask extends TimerTask {
@Override
public void run() {
try {
close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ioSocket.onDisconnect();
}
}
}