Search in sources :

Example 1 with WebSocketHandlerAdapter

use of org.atmosphere.websocket.WebSocketHandlerAdapter in project atmosphere by Atmosphere.

the class WebSocketProcessorTest method basicProgrammaticAPIWorkflow.

@Test
public void basicProgrammaticAPIWorkflow() throws IOException, ServletException, ExecutionException, InterruptedException {
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    final WebSocket w = new ArrayBaseWebSocket(b);
    final WebSocketProcessor processor = WebSocketProcessorFactory.getDefault().getWebSocketProcessor(framework);
    framework.addWebSocketHandler("/*", new WebSocketProcessor.WebSocketHandlerProxy(new WebSocketHandlerAdapter() {

        @Override
        public void onTextMessage(WebSocket webSocket, String data) throws IOException {
            webSocket.write(data);
        }

        @Override
        public void onOpen(WebSocket webSocket) throws IOException {
            webSocket.write(webSocket.resource().getRequest().getReader().readLine());
        }
    }));
    AtmosphereRequest request = new AtmosphereRequestImpl.Builder().destroyable(false).body("yoComet").pathInfo("/a").build();
    processor.open(w, request, AtmosphereResponseImpl.newInstance(framework.getAtmosphereConfig(), request, w));
    processor.invokeWebSocketProtocol(w, "yoWebSocket");
    framework.getBroadcasterFactory().lookup("/*").broadcast("yoBroadcast").get();
    assertEquals(b.toString(), "yoCometyoWebSocketyoBroadcast");
}
Also used : WebSocketHandlerAdapter(org.atmosphere.websocket.WebSocketHandlerAdapter) WebSocketProcessor(org.atmosphere.websocket.WebSocketProcessor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) WebSocket(org.atmosphere.websocket.WebSocket) Test(org.testng.annotations.Test)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 WebSocket (org.atmosphere.websocket.WebSocket)1 WebSocketHandlerAdapter (org.atmosphere.websocket.WebSocketHandlerAdapter)1 WebSocketProcessor (org.atmosphere.websocket.WebSocketProcessor)1 Test (org.testng.annotations.Test)1