-
-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Send message to all connected web socket clients fix #645
- Loading branch information
Showing
6 changed files
with
395 additions
and
30 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
coverage-report/src/test/java/org/jooby/issues/Issue645.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package org.jooby.issues; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.util.Arrays; | ||
import java.util.LinkedList; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.jooby.test.ServerFeature; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import com.ning.http.client.AsyncHttpClient; | ||
import com.ning.http.client.AsyncHttpClientConfig; | ||
import com.ning.http.client.ws.WebSocket; | ||
import com.ning.http.client.ws.WebSocketTextListener; | ||
import com.ning.http.client.ws.WebSocketUpgradeHandler; | ||
|
||
public class Issue645 extends ServerFeature { | ||
|
||
{ | ||
ws("/ws", (ws) -> { | ||
|
||
ws.onMessage(message -> { | ||
System.out.println(Thread.currentThread()); | ||
ws.broadcast("=" + message.value(), () -> { | ||
System.out.println(Thread.currentThread()); | ||
ws.close(); | ||
}); | ||
}); | ||
}); | ||
|
||
} | ||
|
||
private AsyncHttpClient client; | ||
|
||
@Before | ||
public void before() { | ||
client = new AsyncHttpClient(new AsyncHttpClientConfig.Builder().build()); | ||
} | ||
|
||
@After | ||
public void after() { | ||
client.close(); | ||
} | ||
|
||
@Test | ||
public void sendText() throws Exception { | ||
LinkedList<String> messages = new LinkedList<>(); | ||
|
||
CountDownLatch latch = new CountDownLatch(1); | ||
|
||
client.prepareGet(ws("ws").toString()) | ||
.execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener( | ||
new WebSocketTextListener() { | ||
|
||
@Override | ||
public void onMessage(final String message) { | ||
messages.add(message); | ||
latch.countDown(); | ||
} | ||
|
||
@Override | ||
public void onOpen(final WebSocket websocket) { | ||
websocket.sendMessage("hey!"); | ||
} | ||
|
||
@Override | ||
public void onClose(final WebSocket websocket) { | ||
} | ||
|
||
@Override | ||
public void onError(final Throwable t) { | ||
} | ||
}).build()) | ||
.get(); | ||
if (latch.await(1L, TimeUnit.SECONDS)) { | ||
assertEquals(Arrays.asList("=hey!"), messages); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.