@@ -282,6 +282,7 @@ public void run() {
282
282
try {
283
283
_connect ();
284
284
} catch (IOException e ) {
285
+ e .printStackTrace ();
285
286
this .onError (e );
286
287
}
287
288
}
@@ -321,7 +322,7 @@ public void send(String text) {
321
322
} else {
322
323
this .onError (new NotYetConnectedException ());
323
324
}
324
- } catch (IOException e ) {
325
+ } catch (Exception e ) {
325
326
this .onError (e );
326
327
}
327
328
}
@@ -333,21 +334,41 @@ public void send(String text) {
333
334
* Message from websocket server
334
335
*/
335
336
public void onMessage (String msg ) {
337
+ final String data = msg ;
336
338
Log .v ("websocket" , "Received a message: " + msg );
337
- appView .loadUrl (buildJavaScriptData (EVENT_ON_MESSAGE , msg ));
339
+ appView .post (new Runnable () {
340
+ public void run () {
341
+ appView .loadUrl (buildJavaScriptData (EVENT_ON_MESSAGE , data ));
342
+ }
343
+ });
338
344
}
339
345
340
346
public void onOpen () {
341
- appView .loadUrl (buildJavaScriptData (EVENT_ON_OPEN , BLANK_MESSAGE ));
347
+ Log .v ("websocket" , "Connected!" );
348
+ appView .post (new Runnable () {
349
+ public void run () {
350
+ appView .loadUrl (buildJavaScriptData (EVENT_ON_MESSAGE , BLANK_MESSAGE ));
351
+ }
352
+ });
342
353
}
343
354
344
355
public void onClose () {
345
- appView .loadUrl (buildJavaScriptData (EVENT_ON_CLOSE , BLANK_MESSAGE ));
356
+ appView .post (new Runnable () {
357
+ public void run () {
358
+ appView .loadUrl (buildJavaScriptData (EVENT_ON_MESSAGE , BLANK_MESSAGE ));
359
+ }
360
+ });
346
361
}
347
362
348
363
public void onError (Throwable t ) {
349
- String msg = t .getMessage ();
350
- appView .loadUrl (buildJavaScriptData (EVENT_ON_ERROR , msg ));
364
+ final String msg = t .getMessage ();
365
+ Log .v ("websocket" , "Error: " + msg );
366
+ t .printStackTrace ();
367
+ appView .post (new Runnable () {
368
+ public void run () {
369
+ appView .loadUrl (buildJavaScriptData (EVENT_ON_MESSAGE , msg ));
370
+ }
371
+ });
351
372
}
352
373
353
374
public String getId () {
@@ -372,11 +393,12 @@ public int getReadyState() {
372
393
* @return
373
394
*/
374
395
private String buildJavaScriptData (String event , String msg ) {
375
- String b64EncodedMsg ;
396
+ String b64EncodedMsg = "Error!" ;
376
397
try {
377
- b64EncodedMsg = Base64 .encodeBytes (msg .getBytes (UTF8_CHARSET ));
398
+ if (msg != null ) {
399
+ b64EncodedMsg = Base64 .encodeBytes (msg .getBytes (UTF8_CHARSET ));
400
+ }
378
401
} catch (Exception e ) {
379
- b64EncodedMsg = "Base64 Encoding Error!" ;
380
402
e .printStackTrace ();
381
403
}
382
404
String _d = "javascript:WebSocket." + event + "(" + "{" + "\" _target\" :\" " + id + "\" ,"
0 commit comments