Search in sources :

Example 16 with WebSocketProcessor

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

Example 17 with WebSocketProcessor

use of org.atmosphere.websocket.WebSocketProcessor 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 18 with WebSocketProcessor

use of org.atmosphere.websocket.WebSocketProcessor 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 19 with WebSocketProcessor

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

the class WebSocketProcessorTest method undetectedCloseWebSocketTest.

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

        @Override
        public void onRequest(AtmosphereResource resource) throws IOException {
            r.set(resource);
            resource.getBroadcaster().addAtmosphereResource(resource);
        }

        @Override
        public void onStateChange(AtmosphereResourceEvent event) throws IOException {
            Cookie[] c = event.getResource().getRequest().getCookies();
            cValue.set(c[0]);
        }

        @Override
        public void destroy() {
        }
    });
    Map<String, String> m = new HashMap<String, String>();
    m.put(HeaderConfig.X_ATMOSPHERE_TRANSPORT, HeaderConfig.WEBSOCKET_TRANSPORT);
    AtmosphereRequest request = new AtmosphereRequestImpl.Builder().headers(m).pathInfo("/a").build();
    request.setAttribute(FrameworkConfig.WEBSOCKET_MESSAGE, null);
    processor.open(w, request, AtmosphereResponseImpl.newInstance(framework.getAtmosphereConfig(), request, w));
    final AtomicBoolean dirtyDisconnect = new AtomicBoolean();
    request.setAttribute(SUSPENDED_ATMOSPHERE_RESOURCE_UUID, w.resource().uuid());
    m.put(HeaderConfig.X_ATMOSPHERE_TRANSPORT, HeaderConfig.WEBSOCKET_TRANSPORT);
    request.headers(m);
    AtmosphereResource dup = framework.getAtmosphereConfig().resourcesFactory().create(framework.config, w.resource().uuid(), request).suspend();
    w.resource(dup);
    dup.addEventListener(new AtmosphereResourceEventListenerAdapter.OnDisconnect() {

        @Override
        public void onDisconnect(AtmosphereResourceEvent event) {
            if (event.isCancelled())
                dirtyDisconnect.set(true);
        }
    });
    request.setAttribute(FrameworkConfig.WEBSOCKET_MESSAGE, null);
    processor.open(w, request, AtmosphereResponseImpl.newInstance(framework.getAtmosphereConfig(), request, w));
    r.get().getBroadcaster().broadcast("yo").get();
    assertTrue(dirtyDisconnect.get());
}
Also used : Cookie(javax.servlet.http.Cookie) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) 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)

Example 20 with WebSocketProcessor

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

the class WebSocketProcessorTest method onDisconnectAtmosphereRequestAttribute.

@Test
public void onDisconnectAtmosphereRequestAttribute() 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 onDisconnect(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("Disconnect", DISCONNECT, 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)

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