1
1
/*
2
2
* Copyright (c) 2010 Nathan Rajlich (https://github.com/TooTallNate)
3
3
* Copyright (c) 2010 Animesh Kumar (https://github.com/anismiles)
4
+ * Copyright (c) 2010 Strumsoft (https://strumsoft.com)
4
5
*
5
6
* Permission is hereby granted, free of charge, to any person
6
7
* obtaining a copy of this software and associated documentation
24
25
* OTHER DEALINGS IN THE SOFTWARE.
25
26
*
26
27
*/
27
- package com .strumsoft .websocket . phonegap ;
28
+ package com .strumsoft .phonegap . websocket ;
28
29
29
30
import java .io .IOException ;
30
31
import java .io .UnsupportedEncodingException ;
@@ -64,7 +65,24 @@ public enum Draft {
64
65
DRAFT75 , DRAFT76
65
66
}
66
67
67
- ////////////////// CONSTANT
68
+ // //////////////// CONSTANT
69
+ /**
70
+ * The connection has not yet been established.
71
+ */
72
+ public final static int WEBSOCKET_STATE_CONNECTING = 0 ;
73
+ /**
74
+ * The WebSocket connection is established and communication is possible.
75
+ */
76
+ public final static int WEBSOCKET_STATE_OPEN = 1 ;
77
+ /**
78
+ * The connection is going through the closing handshake.
79
+ */
80
+ public final static int WEBSOCKET_STATE_CLOSING = 2 ;
81
+ /**
82
+ * The connection has been closed or could not be opened.
83
+ */
84
+ public final static int WEBSOCKET_STATE_CLOSED = 3 ;
85
+
68
86
/**
69
87
* An empty string
70
88
*/
@@ -110,7 +128,7 @@ public enum Draft {
110
128
*/
111
129
public static final byte DATA_END_OF_FRAME = (byte ) 0xFF ;
112
130
113
- ////////////////// INSTANCE Variables
131
+ // //////////////// INSTANCE Variables
114
132
/**
115
133
* The WebView instance from Phonegap DroidGap
116
134
*/
@@ -183,6 +201,10 @@ public enum Draft {
183
201
* Key3 used in handshake
184
202
*/
185
203
private byte [] key3 = null ;
204
+ /**
205
+ * The readyState attribute represents the state of the connection.
206
+ */
207
+ private int readyState = WEBSOCKET_STATE_CONNECTING ;
186
208
187
209
/**
188
210
* Constructor.
@@ -243,6 +265,8 @@ public void run() {
243
265
* Closes connection with server
244
266
*/
245
267
public void close () {
268
+ this .readyState = WebSocket .WEBSOCKET_STATE_CLOSING ;
269
+
246
270
// close socket channel
247
271
try {
248
272
this .socketChannel .close ();
@@ -251,9 +275,11 @@ public void close() {
251
275
}
252
276
this .running = false ;
253
277
selector .wakeup ();
254
-
278
+
255
279
// fire onClose method
256
280
this .onClose ();
281
+
282
+ this .readyState = WebSocket .WEBSOCKET_STATE_CLOSED ;
257
283
}
258
284
259
285
/**
@@ -264,7 +290,11 @@ public void close() {
264
290
*/
265
291
public void send (String text ) {
266
292
try {
267
- _send (text );
293
+ if (this .readyState == WEBSOCKET_STATE_OPEN ){
294
+ _send (text );
295
+ } else {
296
+ this .onError (new NotYetConnectedException ());
297
+ }
268
298
} catch (IOException e ) {
269
299
this .onError (e );
270
300
}
@@ -298,10 +328,20 @@ public String getId() {
298
328
}
299
329
300
330
/**
301
- * Builds text for javascript engine to invoke proper event method with proper data.
331
+ * @return the readyState
332
+ */
333
+ public int getReadyState () {
334
+ return readyState ;
335
+ }
336
+
337
+ /**
338
+ * Builds text for javascript engine to invoke proper event method with
339
+ * proper data.
302
340
*
303
- * @param event websocket event (onOpen, onMessage etc.)
304
- * @param msg Text message received from websocket server
341
+ * @param event
342
+ * websocket event (onOpen, onMessage etc.)
343
+ * @param msg
344
+ * Text message received from websocket server
305
345
* @return
306
346
*/
307
347
private String buildJavaScriptData (String event , String msg ) {
@@ -350,7 +390,7 @@ private boolean _send(String text) throws IOException {
350
390
351
391
// actual connection logic
352
392
private void _connect () throws IOException {
353
-
393
+ this . readyState = WEBSOCKET_STATE_CONNECTING ;
354
394
socketChannel = SocketChannel .open ();
355
395
socketChannel .configureBlocking (false );
356
396
socketChannel .connect (new InetSocketAddress (uri .getHost (), port ));
@@ -564,6 +604,7 @@ private void _readHandshake(byte[] handShakeBody) throws IOException, NoSuchAlgo
564
604
}
565
605
566
606
if (isConnectionReady ) {
607
+ this .readyState = WEBSOCKET_STATE_OPEN ;
567
608
// fire onOpen method
568
609
this .onOpen ();
569
610
} else {
@@ -604,4 +645,3 @@ private String _randomKey() {
604
645
return key ;
605
646
}
606
647
}
607
-
0 commit comments