44
44
import java .util .concurrent .BlockingQueue ;
45
45
import java .util .concurrent .LinkedBlockingQueue ;
46
46
47
+ import android .util .Log ;
47
48
import android .webkit .WebView ;
48
49
49
50
/**
@@ -247,17 +248,42 @@ protected WebSocket(WebView appView, URI uri, Draft draft, String id) {
247
248
// //////////////////////////////////////////////////////////////////////////////////////
248
249
/**
249
250
* Starts a new Thread and connects to server
251
+ *
252
+ * @throws IOException
250
253
*/
251
- public void connect () {
254
+ public Thread connect () throws IOException {
252
255
this .running = true ;
253
- (new Thread (this )).start ();
256
+ this .readyState = WEBSOCKET_STATE_CONNECTING ;
257
+ // open socket
258
+ socketChannel = SocketChannel .open ();
259
+ socketChannel .configureBlocking (false );
260
+ // set address
261
+ socketChannel .connect (new InetSocketAddress (uri .getHost (), port ));
262
+ // start a thread to make connection
263
+
264
+ // More info:
265
+ // http://groups.google.com/group/android-developers/browse_thread/thread/45a8b53e9bf60d82
266
+ // http://stackoverflow.com/questions/2879455/android-2-2-and-bad-address-family-on-socket-connect
267
+ System .setProperty ("java.net.preferIPv4Stack" , "true" );
268
+ System .setProperty ("java.net.preferIPv6Addresses" , "false" );
269
+
270
+ selector = Selector .open ();
271
+ socketChannel .register (selector , SelectionKey .OP_CONNECT );
272
+ Log .v ("websocket" , "Starting a new thread to manage data reading/writing" );
273
+
274
+ Thread th = new Thread (this );
275
+ th .start ();
276
+ // return thread object for explicit closing, if needed
277
+ return th ;
254
278
}
255
279
256
280
public void run () {
257
- try {
258
- _connect ();
259
- } catch (IOException e ) {
260
- this .onError (e );
281
+ while (this .running ) {
282
+ try {
283
+ _connect ();
284
+ } catch (IOException e ) {
285
+ this .onError (e );
286
+ }
261
287
}
262
288
}
263
289
@@ -266,7 +292,7 @@ public void run() {
266
292
*/
267
293
public void close () {
268
294
this .readyState = WebSocket .WEBSOCKET_STATE_CLOSING ;
269
-
295
+
270
296
// close socket channel
271
297
try {
272
298
this .socketChannel .close ();
@@ -275,10 +301,10 @@ public void close() {
275
301
}
276
302
this .running = false ;
277
303
selector .wakeup ();
278
-
304
+
279
305
// fire onClose method
280
306
this .onClose ();
281
-
307
+
282
308
this .readyState = WebSocket .WEBSOCKET_STATE_CLOSED ;
283
309
}
284
310
@@ -290,7 +316,7 @@ public void close() {
290
316
*/
291
317
public void send (String text ) {
292
318
try {
293
- if (this .readyState == WEBSOCKET_STATE_OPEN ){
319
+ if (this .readyState == WEBSOCKET_STATE_OPEN ) {
294
320
_send (text );
295
321
} else {
296
322
this .onError (new NotYetConnectedException ());
@@ -390,45 +416,31 @@ private boolean _send(String text) throws IOException {
390
416
391
417
// actual connection logic
392
418
private void _connect () throws IOException {
393
- this .readyState = WEBSOCKET_STATE_CONNECTING ;
394
- socketChannel = SocketChannel .open ();
395
- socketChannel .configureBlocking (false );
396
- socketChannel .connect (new InetSocketAddress (uri .getHost (), port ));
397
-
398
- // More info:
399
- // http://groups.google.com/group/android-developers/browse_thread/thread/45a8b53e9bf60d82
400
- // http://stackoverflow.com/questions/2879455/android-2-2-and-bad-address-family-on-socket-connect
401
- System .setProperty ("java.net.preferIPv4Stack" , "true" );
402
- System .setProperty ("java.net.preferIPv6Addresses" , "false" );
419
+ // Continuous loop that is only supposed to end when "close" is called.
403
420
404
- selector = Selector .open ();
405
- socketChannel .register (selector , SelectionKey .OP_CONNECT );
421
+ selector .select ();
422
+ Set <SelectionKey > keys = selector .selectedKeys ();
423
+ Iterator <SelectionKey > i = keys .iterator ();
406
424
407
- // Continuous loop that is only supposed to end when "close" is called.
408
- while (this .running ) {
409
- selector .select ();
410
- Set <SelectionKey > keys = selector .selectedKeys ();
411
- Iterator <SelectionKey > i = keys .iterator ();
412
-
413
- while (i .hasNext ()) {
414
- SelectionKey key = i .next ();
415
- i .remove ();
416
- if (key .isConnectable ()) {
417
- if (socketChannel .isConnectionPending ()) {
418
- socketChannel .finishConnect ();
419
- }
420
- socketChannel .register (selector , SelectionKey .OP_READ );
421
- _writeHandshake ();
425
+ while (i .hasNext ()) {
426
+ SelectionKey key = i .next ();
427
+ i .remove ();
428
+ if (key .isConnectable ()) {
429
+ if (socketChannel .isConnectionPending ()) {
430
+ socketChannel .finishConnect ();
422
431
}
423
- if (key .isReadable ()) {
424
- try {
425
- _read ();
426
- } catch (NoSuchAlgorithmException nsa ) {
427
- this .onError (nsa );
428
- }
432
+ socketChannel .register (selector , SelectionKey .OP_READ );
433
+ _writeHandshake ();
434
+ }
435
+ if (key .isReadable ()) {
436
+ try {
437
+ _read ();
438
+ } catch (NoSuchAlgorithmException nsa ) {
439
+ this .onError (nsa );
429
440
}
430
441
}
431
442
}
443
+
432
444
}
433
445
434
446
private void _writeHandshake () throws IOException {
0 commit comments