Search in sources :

Example 21 with WebSocket

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

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

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

Example 24 with WebSocket

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

the class ManagedAtmosphereHandlerTest method testWebSocketFactory.

@Test
public void testWebSocketFactory() throws IOException, ServletException {
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    final WebSocket w = new ArrayBaseWebSocket(b);
    final WebSocketProcessor processor = WebSocketProcessorFactory.getDefault().getWebSocketProcessor(framework);
    AtmosphereRequest request = new AtmosphereRequestImpl.Builder().destroyable(false).body("yoComet").pathInfo("/websocketfactory").build();
    processor.open(w, request, AtmosphereResponseImpl.newInstance(framework.getAtmosphereConfig(), request, w));
    assertNotNull(r.get());
    assertTrue(Interceptor.invoked);
}
Also used : 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 WebSocket

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

the class WebsocketProducer method getWebSocket.

private WebSocket getWebSocket(final String connectionKey, final List<String> notValidConnectionKeys) {
    WebSocket websocket;
    if (connectionKey == null) {
        throw new IllegalArgumentException("Failed to send message to single connection; connection key is not set.");
    } else {
        websocket = getEndpoint().getWebSocketStore().getWebSocket(connectionKey);
        if (websocket == null) {
            //collect for call back to handle not sent message(s) to guaranty delivery
            notValidConnectionKeys.add(connectionKey);
            log.debug("Failed to send message to single connection; connetion key is not valid. {}", connectionKey);
        }
    }
    return websocket;
}
Also used : WebSocket(org.atmosphere.websocket.WebSocket)

Aggregations

WebSocket (org.atmosphere.websocket.WebSocket)27 ByteArrayOutputStream (java.io.ByteArrayOutputStream)22 WebSocketProcessor (org.atmosphere.websocket.WebSocketProcessor)22 Test (org.testng.annotations.Test)21 IOException (java.io.IOException)12 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 AtmosphereRequest (org.atmosphere.runtime.AtmosphereRequest)4 WebSocketEventListenerAdapter (org.atmosphere.websocket.WebSocketEventListenerAdapter)3 Reader (java.io.Reader)2 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 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1