Search in sources :

Example 6 with WebSocketEventListenerAdapter

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

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

WebSocketEventListenerAdapter (org.atmosphere.websocket.WebSocketEventListenerAdapter)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 IOException (java.io.IOException)6 WebSocket (org.atmosphere.websocket.WebSocket)6 WebSocketProcessor (org.atmosphere.websocket.WebSocketProcessor)6 Test (org.testng.annotations.Test)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 WebSocketEventListener (org.atmosphere.websocket.WebSocketEventListener)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 URISyntaxException (java.net.URISyntaxException)1 KeyException (java.security.KeyException)1 LoginException (javax.security.auth.login.LoginException)1 HttpSession (javax.servlet.http.HttpSession)1 AtmosphereResource (org.atmosphere.cpr.AtmosphereResource)1 ActiveObjectCreationException (org.objectweb.proactive.ActiveObjectCreationException)1 NodeException (org.objectweb.proactive.core.node.NodeException)1 SchedulerProxyUserInterface (org.ow2.proactive.scheduler.common.util.SchedulerProxyUserInterface)1 LogForwardingException (org.ow2.proactive.scheduler.common.util.logforwarder.LogForwardingException)1 EventNotification (org.ow2.proactive_grid_cloud_portal.scheduler.dto.eventing.EventNotification)1