Skip to content

Commit 5116ff5

Browse files
author
Animesh Kumar
committedFeb 12, 2011
added readyState support
1 parent a06585a commit 5116ff5

File tree

3 files changed

+50
-10
lines changed

3 files changed

+50
-10
lines changed
 

‎assets/www/js/websocket.js

92 Bytes
Binary file not shown.

‎src/com/strumsoft/websocket/phonegap/WebSocket.java

+50-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*
22
* Copyright (c) 2010 Nathan Rajlich (https://github.com/TooTallNate)
33
* Copyright (c) 2010 Animesh Kumar (https://github.com/anismiles)
4+
* Copyright (c) 2010 Strumsoft (https://strumsoft.com)
45
*
56
* Permission is hereby granted, free of charge, to any person
67
* obtaining a copy of this software and associated documentation
@@ -24,7 +25,7 @@
2425
* OTHER DEALINGS IN THE SOFTWARE.
2526
*
2627
*/
27-
package com.strumsoft.websocket.phonegap;
28+
package com.strumsoft.phonegap.websocket;
2829

2930
import java.io.IOException;
3031
import java.io.UnsupportedEncodingException;
@@ -64,7 +65,24 @@ public enum Draft {
6465
DRAFT75, DRAFT76
6566
}
6667

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+
6886
/**
6987
* An empty string
7088
*/
@@ -110,7 +128,7 @@ public enum Draft {
110128
*/
111129
public static final byte DATA_END_OF_FRAME = (byte) 0xFF;
112130

113-
////////////////// INSTANCE Variables
131+
// //////////////// INSTANCE Variables
114132
/**
115133
* The WebView instance from Phonegap DroidGap
116134
*/
@@ -183,6 +201,10 @@ public enum Draft {
183201
* Key3 used in handshake
184202
*/
185203
private byte[] key3 = null;
204+
/**
205+
* The readyState attribute represents the state of the connection.
206+
*/
207+
private int readyState = WEBSOCKET_STATE_CONNECTING;
186208

187209
/**
188210
* Constructor.
@@ -243,6 +265,8 @@ public void run() {
243265
* Closes connection with server
244266
*/
245267
public void close() {
268+
this.readyState = WebSocket.WEBSOCKET_STATE_CLOSING;
269+
246270
// close socket channel
247271
try {
248272
this.socketChannel.close();
@@ -251,9 +275,11 @@ public void close() {
251275
}
252276
this.running = false;
253277
selector.wakeup();
254-
278+
255279
// fire onClose method
256280
this.onClose();
281+
282+
this.readyState = WebSocket.WEBSOCKET_STATE_CLOSED;
257283
}
258284

259285
/**
@@ -264,7 +290,11 @@ public void close() {
264290
*/
265291
public void send(String text) {
266292
try {
267-
_send(text);
293+
if(this.readyState == WEBSOCKET_STATE_OPEN){
294+
_send(text);
295+
} else {
296+
this.onError(new NotYetConnectedException());
297+
}
268298
} catch (IOException e) {
269299
this.onError(e);
270300
}
@@ -298,10 +328,20 @@ public String getId() {
298328
}
299329

300330
/**
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.
302340
*
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
305345
* @return
306346
*/
307347
private String buildJavaScriptData(String event, String msg) {
@@ -350,7 +390,7 @@ private boolean _send(String text) throws IOException {
350390

351391
// actual connection logic
352392
private void _connect() throws IOException {
353-
393+
this.readyState = WEBSOCKET_STATE_CONNECTING;
354394
socketChannel = SocketChannel.open();
355395
socketChannel.configureBlocking(false);
356396
socketChannel.connect(new InetSocketAddress(uri.getHost(), port));
@@ -564,6 +604,7 @@ private void _readHandshake(byte[] handShakeBody) throws IOException, NoSuchAlgo
564604
}
565605

566606
if (isConnectionReady) {
607+
this.readyState = WEBSOCKET_STATE_OPEN;
567608
// fire onOpen method
568609
this.onOpen();
569610
} else {
@@ -604,4 +645,3 @@ private String _randomKey() {
604645
return key;
605646
}
606647
}
607-
Binary file not shown.

0 commit comments

Comments
 (0)
Failed to load comments.