Search in sources :

Example 31 with WebSocket

use of org.atmosphere.websocket.WebSocket in project camel by apache.

the class MemoryWebSocketStoreTest method testAddAndRemove.

@Test
public void testAddAndRemove() throws Exception {
    MemoryWebSocketStore store = new MemoryWebSocketStore();
    WebSocket webSocket1 = EasyMock.createMock(WebSocket.class);
    WebSocket webSocket2 = EasyMock.createMock(WebSocket.class);
    String connectionKey1 = UUID.randomUUID().toString();
    String connectionKey2 = UUID.randomUUID().toString();
    store.addWebSocket(connectionKey1, webSocket1);
    verifyGet(store, connectionKey1, webSocket1, true);
    assertEquals(1, store.getAllWebSockets().size());
    store.addWebSocket(connectionKey2, webSocket2);
    verifyGet(store, connectionKey2, webSocket2, true);
    verifyGet(store, connectionKey1, webSocket1, true);
    assertEquals(2, store.getAllWebSockets().size());
    store.removeWebSocket(connectionKey1);
    verifyGet(store, connectionKey1, webSocket1, false);
    store.removeWebSocket(webSocket2);
    verifyGet(store, connectionKey2, webSocket2, false);
    assertEquals(0, store.getAllWebSockets().size());
}
Also used : WebSocket(org.atmosphere.websocket.WebSocket) Test(org.junit.Test)

Example 32 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 33 with WebSocket

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

the class WebSocketProcessorTest method onCloseAtmosphereRequestAttribute.

@Test
public void onCloseAtmosphereRequestAttribute() throws IOException, ServletException, ExecutionException, InterruptedException {
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    final WebSocket w = new ArrayBaseWebSocket(b);
    final WebSocketProcessor processor = WebSocketProcessorFactory.getDefault().getWebSocketProcessor(framework);
    final AtomicReference<String> uuid = new AtomicReference<String>();
    framework.addAtmosphereHandler("/*", new AtmosphereHandler() {

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

                @Override
                public void onClose(WebSocketEvent event) {
                    uuid.set((String) event.webSocket().resource().getRequest().getAttribute(SUSPENDED_ATMOSPHERE_RESOURCE_UUID));
                }
            });
        }

        @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));
    processor.invokeWebSocketProtocol(w, "yoWebSocket");
    processor.notifyListener(w, new WebSocketEventListener.WebSocketEvent("Close", WebSocketEventListener.WebSocketEvent.TYPE.CLOSE, w));
    assertNotNull(uuid.get());
    assertEquals(uuid.get(), request.getAttribute(SUSPENDED_ATMOSPHERE_RESOURCE_UUID));
}
Also used : WebSocketEventListenerAdapter(org.atmosphere.websocket.WebSocketEventListenerAdapter) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) WebSocketEventListener(org.atmosphere.websocket.WebSocketEventListener) WebSocket(org.atmosphere.websocket.WebSocket) WebSocketProcessor(org.atmosphere.websocket.WebSocketProcessor) Test(org.testng.annotations.Test)

Example 34 with WebSocket

use of org.atmosphere.websocket.WebSocket 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)

Example 35 with WebSocket

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

the class WebSocketProcessorTest method encodeURLProxyTest.

@Test
public void encodeURLProxyTest() throws IOException, ServletException, ExecutionException, InterruptedException {
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    final WebSocket w = new ArrayBaseWebSocket(b);
    final WebSocketProcessor processor = WebSocketProcessorFactory.getDefault().getWebSocketProcessor(framework);
    final AtomicReference<String> url = new AtomicReference<String>();
    framework.addAtmosphereHandler("/*", new AtmosphereHandler() {

        @Override
        public void onRequest(AtmosphereResource resource) throws IOException {
            url.set(resource.getResponse().encodeRedirectURL("http://127.0.0.1:8080"));
        }

        @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));
    processor.invokeWebSocketProtocol(w, "yoWebSocket");
    assertEquals(url.get(), "http://127.0.0.1:8080");
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) WebSocket(org.atmosphere.websocket.WebSocket) WebSocketProcessor(org.atmosphere.websocket.WebSocketProcessor) Test(org.testng.annotations.Test)

Aggregations

WebSocket (org.atmosphere.websocket.WebSocket)47 ByteArrayOutputStream (java.io.ByteArrayOutputStream)41 WebSocketProcessor (org.atmosphere.websocket.WebSocketProcessor)41 Test (org.testng.annotations.Test)39 IOException (java.io.IOException)25 AtomicReference (java.util.concurrent.atomic.AtomicReference)10 WebSocketEventListenerAdapter (org.atmosphere.websocket.WebSocketEventListenerAdapter)6 AtmosphereRequest (org.atmosphere.cpr.AtmosphereRequest)5 HashMap (java.util.HashMap)4 ExecutionException (java.util.concurrent.ExecutionException)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 WebSocketEventListener (org.atmosphere.websocket.WebSocketEventListener)4 Reader (java.io.Reader)3 ServletException (jakarta.servlet.ServletException)2 Cookie (jakarta.servlet.http.Cookie)2 HashSet (java.util.HashSet)2 ServletException (javax.servlet.ServletException)2 Cookie (javax.servlet.http.Cookie)2 AtmosphereRequestImpl (org.atmosphere.cpr.AtmosphereRequestImpl)2 WebSocketHandlerAdapter (org.atmosphere.websocket.WebSocketHandlerAdapter)2