Search in sources :

Example 21 with WebSocketProcessor

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

the class WebSocketStreamingHandlerTest method multipleWebSocketHandler.

@Test
public void multipleWebSocketHandler() 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());
    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");
    request = new AtmosphereRequestImpl.Builder().destroyable(false).body("b").pathInfo("/b").build();
    processor.open(w, request, AtmosphereResponseImpl.newInstance(framework.getAtmosphereConfig(), request, w));
    processor.invokeWebSocketProtocol(w, "b");
    // The WebSocketHandler is shared.
    assertEquals(b.toString(), "ab");
}
Also used : WebSocketProcessor(org.atmosphere.websocket.WebSocketProcessor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) WebSocket(org.atmosphere.websocket.WebSocket) Test(org.testng.annotations.Test)

Example 22 with WebSocketProcessor

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

the class WebSocketStreamingHandlerTest 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 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 23 with WebSocketProcessor

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

the class WebSocketHandlerServiceProcessor method handle.

@Override
public void handle(AtmosphereFramework framework, Class<WebSocketHandler> annotatedClass) {
    try {
        WebSocketHandlerService m = annotatedClass.getAnnotation(WebSocketHandlerService.class);
        atmosphereConfig(m.atmosphereConfig(), framework);
        framework.addAtmosphereHandler(m.path(), AtmosphereFramework.REFLECTOR_ATMOSPHEREHANDLER).initWebSocket();
        framework.setDefaultBroadcasterClassName(m.broadcaster().getName());
        filters(m.broadcastFilters(), framework);
        final LinkedList<AtmosphereInterceptor> l = new LinkedList<AtmosphereInterceptor>();
        AtmosphereInterceptor aa = listeners(m.listeners(), framework);
        if (aa != null) {
            l.add(aa);
        }
        AnnotationUtil.interceptorsForHandler(framework, Arrays.asList(m.interceptors()), l);
        framework.setBroadcasterCacheClassName(m.broadcasterCache().getName());
        WebSocketProcessor p = WebSocketProcessorFactory.getDefault().getWebSocketProcessor(framework);
        framework.addAtmosphereHandler(m.path(), REFLECTOR_ATMOSPHEREHANDLER, l);
        p.registerWebSocketHandler(m.path(), new WebSocketProcessor.WebSocketHandlerProxy(broadcasterClass(framework, m.broadcaster()), framework.newClassInstance(WebSocketHandler.class, annotatedClass)));
    } catch (Throwable e) {
        logger.warn("", e);
    }
}
Also used : AtmosphereInterceptor(org.atmosphere.runtime.AtmosphereInterceptor) WebSocketHandlerService(org.atmosphere.config.service.WebSocketHandlerService) WebSocketProcessor(org.atmosphere.websocket.WebSocketProcessor) LinkedList(java.util.LinkedList)

Example 24 with WebSocketProcessor

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

the class PathTest method testSingletonManagedWebSocketPathMessage.

@Test
public void testSingletonManagedWebSocketPathMessage() throws IOException, ServletException {
    instanceCount = 0;
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    final WebSocket w = new ArrayBaseWebSocket(b);
    final WebSocketProcessor processor = WebSocketProcessorFactory.getDefault().getWebSocketProcessor(framework);
    AtmosphereRequest request = new AtmosphereRequestImpl.Builder().pathInfo("/singleton/ws/bar").method("GET").build();
    processor.open(w, request, AtmosphereResponseImpl.newInstance(framework.getAtmosphereConfig(), request, w));
    assertEquals(instanceCount, 0);
    assertNotNull(r.get());
    assertEquals(MyInterceptor.invokationCount, 1);
    assertEquals(r.get(), "/singleton/ws/bar");
}
Also used : AtmosphereRequestImpl(org.atmosphere.runtime.AtmosphereRequestImpl) AtmosphereRequest(org.atmosphere.runtime.AtmosphereRequest) WebSocketProcessor(org.atmosphere.websocket.WebSocketProcessor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) WebSocket(org.atmosphere.websocket.WebSocket) Test(org.testng.annotations.Test)

Example 25 with WebSocketProcessor

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

the class PathTest method testManagedWebSocketPathMessage.

@Test
public void testManagedWebSocketPathMessage() throws IOException, ServletException {
    instanceCount = 0;
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    final WebSocket w = new ArrayBaseWebSocket(b);
    final WebSocketProcessor processor = WebSocketProcessorFactory.getDefault().getWebSocketProcessor(framework);
    AtmosphereRequest request = new AtmosphereRequestImpl.Builder().pathInfo("/ws/bar").method("GET").build();
    processor.open(w, request, AtmosphereResponseImpl.newInstance(framework.getAtmosphereConfig(), request, w));
    assertEquals(instanceCount, 1);
    assertNotNull(r.get());
    assertEquals(r.get(), "/ws/bar");
}
Also used : AtmosphereRequestImpl(org.atmosphere.runtime.AtmosphereRequestImpl) AtmosphereRequest(org.atmosphere.runtime.AtmosphereRequest) WebSocketProcessor(org.atmosphere.websocket.WebSocketProcessor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) WebSocket(org.atmosphere.websocket.WebSocket) Test(org.testng.annotations.Test)

Aggregations

WebSocketProcessor (org.atmosphere.websocket.WebSocketProcessor)26 ByteArrayOutputStream (java.io.ByteArrayOutputStream)22 WebSocket (org.atmosphere.websocket.WebSocket)22 Test (org.testng.annotations.Test)21 IOException (java.io.IOException)12 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 AtmosphereRequest (org.atmosphere.runtime.AtmosphereRequest)3 DefaultWebSocketProcessor (org.atmosphere.websocket.DefaultWebSocketProcessor)3 WebSocketEventListenerAdapter (org.atmosphere.websocket.WebSocketEventListenerAdapter)3 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 Reader (java.io.Reader)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 ExecutorService (java.util.concurrent.ExecutorService)1