This repository was archived by the owner on Mar 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 387
/
Copy pathSocketIO.java
432 lines (390 loc) · 11.2 KB
/
SocketIO.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/*
* socket.io-java-client SocketIO.java
*
* Copyright (c) 2012, Enno Boland
* socket.io-java-client is a implementation of the socket.io protocol in Java.
*
* See LICENSE file for more information
*/
package io.socket;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import org.json.JSONObject;
/**
* The Class SocketIO.
*/
public class SocketIO {
/** callback of this Socket. */
private IOCallback callback;
/** connection of this Socket. */
private IOConnection connection;
/** namespace. */
private String namespace;
/** Used for setting header during handshaking. */
private Properties headers = new Properties();
/** Used for setting header during connecting. */
private Map<String, String> websocketHeaders = new HashMap<String, String>();
private URL url;
/**
* Instantiates a new socket.io connection. The object connects after
* calling {@link #connect(URL, IOCallback)} or
* {@link #connect(String, IOCallback)}
*/
public SocketIO() {
}
/**
* Instantiates a new socket.io connection. The object connects after
* calling {@link #connect(IOCallback)}
*
* @param url
* the url
* @throws MalformedURLException
* the malformed url exception
*/
public SocketIO(final String url) throws MalformedURLException {
if (url == null)
throw new RuntimeException("url may not be null.");
setAndConnect(new URL(url), null);
}
/**
* Instantiates a new socket.io connection and sets the request headers used
* while connecting the first time for authorizing. The object connects
* after calling {@link #connect(IOCallback)}
*
* @param url
* the url
* @param headers
* the {@link Properties headers} used while handshaking
* @throws MalformedURLException
* the malformed url exception
*/
public SocketIO(final String url, Properties headers)
throws MalformedURLException {
if (url == null)
throw new RuntimeException("url may not be null.");
if (headers != null)
this.headers = headers;
setAndConnect(new URL(url), null);
}
/**
* Instantiates a new socket.io object and connects to the given url. Do not
* call any of the connect() methods afterwards.
*
* @param url
* the url
* @param callback
* the callback
* @throws MalformedURLException
* the malformed url exception
*/
public SocketIO(final String url, final IOCallback callback)
throws MalformedURLException {
connect(url, callback);
}
/**
* Instantiates a new socket.io object and connects to the given url. Do not
* call any of the connect() methods afterwards.
*
* @param url
* the url
* @param callback
* the callback
*/
public SocketIO(final URL url, final IOCallback callback) {
if (setAndConnect(url, callback) == false)
throw new RuntimeException("url and callback may not be null.");
}
/**
* Instantiates a new socket.io connection. The object connects after
* calling {@link #connect(IOCallback)}
*
* @param url
* the url
*/
public SocketIO(final URL url) {
setAndConnect(url, null);
}
/**
* Set the socket factory used for SSL connections.
* @param socketFactory
*/
public static void setDefaultSSLSocketFactory(SSLContext sslContext) {
IOConnection.setSslContext(sslContext);
}
/**
* connects to supplied host using callback. Do only use this method if you
* instantiate {@link SocketIO} using {@link #SocketIO()}.
*
* @param url
* the url
* @param callback
* the callback
*/
public void connect(final String url, final IOCallback callback)
throws MalformedURLException {
if (setAndConnect(new URL(url), callback) == false) {
if (url == null || callback == null)
throw new RuntimeException("url and callback may not be null.");
else
throw new RuntimeException(
"connect(String, IOCallback) can only be invoked after SocketIO()");
}
}
/**
* connects to supplied host using callback. Do only use this method if you
* instantiate {@link SocketIO} using {@link #SocketIO()}.
*
* @param url
* the url
* @param callback
* the callback
*/
public void connect(URL url, IOCallback callback) {
if (setAndConnect(url, callback) == false) {
if (url == null || callback == null)
throw new RuntimeException("url and callback may not be null.");
else
throw new RuntimeException(
"connect(URL, IOCallback) can only be invoked after SocketIO()");
}
}
/**
* connects to an already set host. Do only use this method if you
* instantiate {@link SocketIO} using {@link #SocketIO(String)} or
* {@link #SocketIO(URL)}.
*
* @param callback
* the callback
*/
public void connect(IOCallback callback) {
if (setAndConnect(null, callback) == false) {
if (callback == null)
throw new RuntimeException("callback may not be null.");
else if (this.url == null)
throw new RuntimeException(
"connect(IOCallback) can only be invoked after SocketIO(String) or SocketIO(URL)");
}
}
/**
* Sets url and callback and initiates connecting if both are present
*
* @param url
* the url
* @param callback
* the callback
* @return true if connecting has been initiated, false if not
*/
private boolean setAndConnect(URL url, IOCallback callback) {
if(this.connection != null)
throw new RuntimeException("You can connect your SocketIO instance only once. Use a fresh instance instead.");
if ((this.url != null && url != null)
|| (this.callback != null && callback != null))
return false;
if (url != null) {
this.url = url;
}
if (callback != null) {
this.callback = callback;
}
if (this.callback != null && this.url != null) {
final String origin = this.url.getProtocol() + "://"
+ this.url.getAuthority();
this.namespace = this.url.getPath();
if (this.namespace.equals("/")) {
this.namespace = "";
}
this.connection = IOConnection.register(origin, this);
return true;
}
return false;
}
/**
* Emits an event to the Socket.IO server. If the connection is not
* established, the call will be buffered and sent as soon as it is
* possible.
*
* @param event
* the event name
* @param args
* arguments. can be any argument {@link org.json.JSONArray#put(Object)} can take.
*/
public void emit(final String event, final Object... args) {
this.connection.emit(this, event, null, args);
}
/**
* Emits an event to the Socket.IO server. If the connection is not
* established, the call will be buffered and sent as soon as it is
* possible.
*
* @param event
* the event name
* @param ack
* an acknowledge implementation
* @param args
* arguments. can be any argument {@link org.json.JSONArray#put(Object)} can take.
*/
public void emit(final String event, IOAcknowledge ack,
final Object... args) {
this.connection.emit(this, event, ack, args);
}
/**
* Gets the callback. Internally used.
*
* @return the callback
*/
public IOCallback getCallback() {
return this.callback;
}
/**
* Gets the namespace. Internally used.
*
* @return the namespace
*/
public String getNamespace() {
return this.namespace;
}
/**
* Send JSON data to the Socket.io server.
*
* @param json
* the JSON object
*/
public void send(final JSONObject json) {
this.connection.send(this, null, json);
}
/**
* Send JSON data to the Socket.io server.
*
* @param ack
* an acknowledge implementation
* @param json
* the JSON object
*/
public void send(IOAcknowledge ack, final JSONObject json) {
this.connection.send(this, ack, json);
}
/**
* Send String data to the Socket.io server.
*
* @param message
* the message String
*/
public void send(final String message) {
this.connection.send(this, null, message);
}
/**
* Send JSON data to the Socket.io server.
*
* @param ack
* an acknowledge implementation
* @param message
* the message String
*/
public void send(IOAcknowledge ack, final String message) {
this.connection.send(this, ack, message);
}
/**
* Disconnect the socket.
*/
public void disconnect() {
this.connection.unregister(this);
}
/**
* Triggers the transport to reconnect.
*
* This had become useful on some android devices which do not shut down
* tcp-connections when switching from HSDPA to Wifi
*/
public void reconnect() {
this.connection.reconnect();
}
/**
* Returns, if a connection is established at the moment
*
* @return true if a connection is established, false if the transport is
* not connected or currently connecting
*/
public boolean isConnected() {
return this.connection != null && this.connection.isConnected();
}
/**
* Returns the name of the used transport
*
* @return the name of the currently used transport
*/
public String getTransport() {
IOTransport transport = this.connection.getTransport();
return transport != null ? transport.getName() : null;
}
/**
* Returns the headers used while handshaking. These Properties are not
* necessarily the ones set by {@link #addHeader(String, String)} or
* {@link #SocketIO(String, Properties)} but the ones used for the
* handshake.
*
* @return the headers used while handshaking
*/
public Properties getHeaders() {
return headers;
}
/**
* Sets the headers used while handshaking. Internally used. Use
* {@link #SocketIO(String, Properties)} or
* {@link #addHeader(String, String)} instead.
*
* @param headers
* the headers used while handshaking
*/
void setHeaders(Properties headers) {
this.headers = headers;
}
/**
* Adds an header to the {@link #headers}
* @return SocketIO.this for daisy chaining.
*/
public SocketIO addHeader(String key, String value) {
if (this.connection != null)
throw new RuntimeException(
"You may only set headers before connecting.\n"
+ " Try to use new SocketIO().addHeader(key, value).connect(host, callback) "
+ "instead of SocketIO(host, callback).addHeader(key, value)");
this.headers.setProperty(key, value);
return this;
}
/**
* Adds an header for connecting
* @return SocketIO.this for daisy chaining.
*/
public SocketIO addWebsocketHeader(String key, String value) {
if (this.connection != null)
throw new RuntimeException(
"You may only set headers before connecting.\n"
+ " Try to use new SocketIO().addHeader(key, value).connect(host, callback) "
+ "instead of SocketIO(host, callback).addHeader(key, value)");
this.websocketHeaders.put(key, value);
return this;
}
/**
* Returns the header value
*
* @return the header value or {@code null} if not present
*/
public String getHeader(String key) {
if (this.headers.contains(key))
return this.headers.getProperty(key);
return null;
}
/**
* Returns the headers used while connecting.
*
* @return the headers used while connecting
*/
public Map<String, String> getWebsocketHeaders() {
return websocketHeaders;
}
}