Search in sources :

Example 1 with WebSocket

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

the class TrackMessageSizeInterceptorTest method testTrackMessageSize.

private void testTrackMessageSize(boolean enabled, AtmosphereInterceptor icp, String expected) throws Exception {
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    final WebSocket w = new ArrayBaseWebSocket(b);
    final WebSocketProcessor processor = WebSocketProcessorFactory.getDefault().getWebSocketProcessor(framework);
    framework.interceptor(icp);
    framework.addAtmosphereHandler("/*", new AtmosphereHandler() {

        @Override
        public void onRequest(AtmosphereResource resource) throws IOException {
            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() {
        }
    });
    Map<String, String> reqheaders = new HashMap<String, String>();
    if (enabled) {
        reqheaders.put(HeaderConfig.X_ATMOSPHERE_TRACKMESSAGESIZE, "true");
    }
    AtmosphereRequest request = new AtmosphereRequestImpl.Builder().destroyable(false).headers(reqheaders).body("yoComet").pathInfo("/a").build();
    processor.open(w, request, AtmosphereResponseImpl.newInstance(framework.getAtmosphereConfig(), request, w));
    processor.invokeWebSocketProtocol(w, "yoWebSocket");
    assertEquals(b.toString(), expected);
}
Also used : HashMap(java.util.HashMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) WebSocket(org.atmosphere.websocket.WebSocket) WebSocketProcessor(org.atmosphere.websocket.WebSocketProcessor)

Example 2 with WebSocket

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

the class WebSocketProcessorTest 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);
    framework.addAtmosphereHandler("/*", new AtmosphereHandler() {

        @Override
        public void onRequest(AtmosphereResource resource) throws IOException {
            resource.getBroadcaster().addAtmosphereResource(resource.suspend());
            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(), "yoCometyoWebSocketyoBroadcast");
}
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 3 with WebSocket

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

the class WebSocketStreamingHandlerTest 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 EchoHandler());
    registerWebSocketHandler("/b", new EchoHandler() {

        @Override
        public void onTextStream(WebSocket webSocket, Reader reader) throws IOException {
            webSocket.write("2" + drainReader(reader));
        }
    });
    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) Reader(java.io.Reader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) WebSocket(org.atmosphere.websocket.WebSocket) Test(org.testng.annotations.Test)

Example 4 with WebSocket

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

the class WebSocketStreamingHandlerTest method invalidPathHandler.

@Test
public void invalidPathHandler() 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 EchoHandler());
    AtmosphereRequest request = new AtmosphereRequestImpl.Builder().destroyable(false).body("yoComet").pathInfo("/abcd").build();
    try {
        processor.open(w, request, AtmosphereResponseImpl.newInstance(framework.getAtmosphereConfig(), request, w));
        fail();
    } catch (Exception ex) {
        assertEquals(ex.getClass(), AtmosphereMappingException.class);
    }
}
Also used : WebSocketProcessor(org.atmosphere.websocket.WebSocketProcessor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) WebSocket(org.atmosphere.websocket.WebSocket) Test(org.testng.annotations.Test)

Example 5 with WebSocket

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

the class WebSocketHandlerTest method invalidPathHandler.

@Test
public void invalidPathHandler() 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()));
    AtmosphereRequest request = new AtmosphereRequestImpl.Builder().destroyable(false).body("yoComet").pathInfo("/abcd").build();
    try {
        processor.open(w, request, AtmosphereResponseImpl.newInstance(framework.getAtmosphereConfig(), request, w));
        fail();
    } catch (Exception ex) {
        assertEquals(ex.getClass(), AtmosphereMappingException.class);
    }
}
Also used : WebSocketProcessor(org.atmosphere.websocket.WebSocketProcessor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) WebSocket(org.atmosphere.websocket.WebSocket) 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