@@ -45,7 +45,10 @@ public void onClose(int i, String s) {
45
45
@ Override
46
46
public void onMessage (String s ) {
47
47
try {
48
- connection .sendMessage (s );
48
+ if (s .equals ("CLOSE" ))
49
+ connection .close ();
50
+ else
51
+ connection .sendMessage (s );
49
52
} catch (IOException e ) {
50
53
try {
51
54
connection .sendMessage ("FAIL" );
@@ -401,4 +404,53 @@ public void onError(Throwable t) {
401
404
c .close ();
402
405
}
403
406
}
407
+
408
+ @ Test (timeOut = 60000 )
409
+ public void echoTextAndThenClose () throws Throwable {
410
+ AsyncHttpClient c = getAsyncHttpClient (null );
411
+ try {
412
+ final CountDownLatch textLatch = new CountDownLatch (1 );
413
+ final CountDownLatch closeLatch = new CountDownLatch (1 );
414
+ final AtomicReference <String > text = new AtomicReference <String >("" );
415
+
416
+ final WebSocket websocket = c .prepareGet (getTargetUrl ()).execute (new WebSocketUpgradeHandler .Builder ().addWebSocketListener (new WebSocketTextListener () {
417
+
418
+ @ Override
419
+ public void onMessage (String message ) {
420
+ text .set (text .get () + message );
421
+ textLatch .countDown ();
422
+ }
423
+
424
+ @ Override
425
+ public void onFragment (String fragment , boolean last ) {
426
+ }
427
+
428
+ @ Override
429
+ public void onOpen (com .ning .http .client .websocket .WebSocket websocket ) {
430
+ }
431
+
432
+ @ Override
433
+ public void onClose (com .ning .http .client .websocket .WebSocket websocket ) {
434
+ closeLatch .countDown ();
435
+ }
436
+
437
+ @ Override
438
+ public void onError (Throwable t ) {
439
+ t .printStackTrace ();
440
+ closeLatch .countDown ();
441
+ }
442
+ }).build ()).get ();
443
+
444
+ websocket .sendTextMessage ("ECHO" );
445
+ textLatch .await ();
446
+
447
+ websocket .sendTextMessage ("CLOSE" );
448
+ closeLatch .await ();
449
+
450
+ assertEquals (text .get (), "ECHO" );
451
+ } finally {
452
+ c .close ();
453
+ }
454
+ }
455
+
404
456
}
0 commit comments