Search in sources :

Example 11 with WebSocket

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

the class WebSocketProcessorTest method basicBackwardCompatbileWorkflow.

@Test
public void basicBackwardCompatbileWorkflow() throws Throwable {
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    final WebSocket w = new ArrayBaseWebSocket(b);
    final WebSocketProcessor processor = WebSocketProcessorFactory.getDefault().getWebSocketProcessor(framework);
    framework.getBroadcasterFactory().remove("/*");
    framework.addInitParameter(ApplicationConfig.BACKWARD_COMPATIBLE_WEBSOCKET_BEHAVIOR, "true").addAtmosphereHandler("/*", new AtmosphereHandler() {

        @Override
        public void onRequest(AtmosphereResource resource) throws IOException {
            resource.getBroadcaster().addAtmosphereResource(resource);
            resource.getResponse().write(resource.getRequest().getReader().readLine());
        }

        @Override
        public void onStateChange(AtmosphereResourceEvent event) throws IOException {
            event.getResource().write(event.getMessage().toString().getBytes());
        }

        @Override
        public void destroy() {
        }
    });
    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(), "yoCometyoWebSocketyoBroadcastyoBroadcast");
}
Also used : WebSocketProcessor(org.atmosphere.websocket.WebSocketProcessor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) WebSocket(org.atmosphere.websocket.WebSocket) Test(org.testng.annotations.Test)

Example 12 with WebSocket

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

the class WebSocketHandlerTest method basicWorkflow.

@Test
public void basicWorkflow() throws IOException, ServletException, ExecutionException, InterruptedException {
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    final WebSocket w = new ArrayBaseWebSocket(b);
    final WebSocketProcessor processor = WebSocketProcessorFactory.getDefault().getWebSocketProcessor(framework);
    registerWebSocketHandler("/*", new WebSocketProcessor.WebSocketHandlerProxy(new EchoHandler()));
    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");
    assertEquals(b.toString(), "yoWebSocket");
}
Also used : WebSocketProcessor(org.atmosphere.websocket.WebSocketProcessor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) WebSocket(org.atmosphere.websocket.WebSocket) Test(org.testng.annotations.Test)

Example 13 with WebSocket

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

the class WebSocketHandlerTest method testInjection.

@Test
public void testInjection() throws IOException, ServletException, ExecutionException, InterruptedException {
    EchoHandler e = new EchoHandler();
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    final WebSocket w = new ArrayBaseWebSocket(b);
    final WebSocketProcessor processor = WebSocketProcessorFactory.getDefault().getWebSocketProcessor(framework);
    registerWebSocketHandler("/*", new WebSocketProcessor.WebSocketHandlerProxy(e));
    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");
    assertNotNull(e.request);
}
Also used : WebSocketProcessor(org.atmosphere.websocket.WebSocketProcessor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) WebSocket(org.atmosphere.websocket.WebSocket) Test(org.testng.annotations.Test)

Example 14 with WebSocket

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

the class WebSocketHandlerTest method multipleWebSocketAndHandler.

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

        @Override
        public void onTextMessage(WebSocket webSocket, String data) throws IOException {
            webSocket.write("2" + data);
        }
    }));
    AtmosphereRequest request = new AtmosphereRequestImpl.Builder().destroyable(false).body("a").pathInfo("/a").build();
    processor.open(w, request, AtmosphereResponseImpl.newInstance(framework.getAtmosphereConfig(), request, w));
    processor.invokeWebSocketProtocol(w, "a");
    assertEquals(b.toString(), "a");
    ByteArrayOutputStream b2 = new ByteArrayOutputStream();
    final WebSocket w2 = new ArrayBaseWebSocket(b2);
    request = new AtmosphereRequestImpl.Builder().destroyable(false).body("b").pathInfo("/b").build();
    processor.open(w2, request, AtmosphereResponseImpl.newInstance(framework.getAtmosphereConfig(), request, w));
    processor.invokeWebSocketProtocol(w2, "b");
    // The WebSocketHandler is shared.
    assertEquals(b2.toString(), "2b");
}
Also used : WebSocketProcessor(org.atmosphere.websocket.WebSocketProcessor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) WebSocket(org.atmosphere.websocket.WebSocket) Test(org.testng.annotations.Test)

Example 15 with WebSocket

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

the class WebSocketProcessorTest method inClosePhaseTest.

@Test
public void inClosePhaseTest() throws IOException, ServletException, ExecutionException, InterruptedException {
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    final WebSocket w = new ArrayBaseWebSocket(b);
    final WebSocketProcessor processor = WebSocketProcessorFactory.getDefault().getWebSocketProcessor(framework);
    final AtomicBoolean closed = new AtomicBoolean();
    framework.addAtmosphereHandler("/*", new AtmosphereHandler() {

        @Override
        public void onRequest(final AtmosphereResource resource) throws IOException {
            resource.addEventListener(new WebSocketEventListenerAdapter() {

                @Override
                public void onClose(WebSocketEvent event) {
                    closed.set(true);
                }
            });
            AtmosphereResourceEventImpl.class.cast(resource.getAtmosphereResourceEvent()).isClosedByClient(true);
            AsynchronousProcessor.class.cast(framework.getAsyncSupport()).completeLifecycle(resource, false);
            processor.close(w, 1005);
        }

        @Override
        public void onStateChange(AtmosphereResourceEvent event) throws IOException {
        }

        @Override
        public void destroy() {
        }
    });
    AtmosphereRequest request = new AtmosphereRequestImpl.Builder().destroyable(false).body("yoComet").pathInfo("/a").build();
    processor.open(w, request, AtmosphereResponseImpl.newInstance(framework.getAtmosphereConfig(), request, w));
    assertFalse(closed.get());
}
Also used : WebSocketEventListenerAdapter(org.atmosphere.websocket.WebSocketEventListenerAdapter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) WebSocket(org.atmosphere.websocket.WebSocket) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WebSocketProcessor(org.atmosphere.websocket.WebSocketProcessor) Test(org.testng.annotations.Test)

Aggregations

WebSocket (org.atmosphere.websocket.WebSocket)27 ByteArrayOutputStream (java.io.ByteArrayOutputStream)22 WebSocketProcessor (org.atmosphere.websocket.WebSocketProcessor)22 Test (org.testng.annotations.Test)21 IOException (java.io.IOException)12 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 AtmosphereRequest (org.atmosphere.runtime.AtmosphereRequest)4 WebSocketEventListenerAdapter (org.atmosphere.websocket.WebSocketEventListenerAdapter)3 Reader (java.io.Reader)2 HashMap (java.util.HashMap)2 ExecutionException (java.util.concurrent.ExecutionException)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 ServletException (javax.servlet.ServletException)2 Cookie (javax.servlet.http.Cookie)2 AtmosphereRequestImpl (org.atmosphere.runtime.AtmosphereRequestImpl)2 WebSocketEventListener (org.atmosphere.websocket.WebSocketEventListener)2 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1