Search in sources :

Example 26 with WebSocket

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

the class WebsocketProducer method process.

@Override
public void process(Exchange exchange) throws Exception {
    Message in = exchange.getIn();
    //TODO support binary data
    Object message = in.getBody();
    if (message == null) {
        LOG.debug("Ignoring a null message");
        return;
    }
    if (!(message instanceof String || message instanceof byte[] || message instanceof Reader || message instanceof InputStream)) {
        // fallback to use String
        if (LOG.isInfoEnabled()) {
            LOG.info("Using String for unexpected message type {} ", message.getClass());
        }
        message = in.getBody(String.class);
    }
    // special conversion for Reader and InputStream for now 
    if (message instanceof Reader) {
        message = in.getBody(String.class);
    } else if (message instanceof InputStream) {
        message = in.getBody(byte[].class);
    }
    log.debug("Sending to {}", message);
    if (getEndpoint().isSendToAll()) {
        log.debug("Sending to all -> {}", message);
        //TODO consider using atmosphere's broadcast or a more configurable async send
        for (final WebSocket websocket : getEndpoint().getWebSocketStore().getAllWebSockets()) {
            sendMessage(websocket, message);
        }
    } else if (in.getHeader(WebsocketConstants.CONNECTION_KEY_LIST) != null) {
        List<String> connectionKeyList = in.getHeader(WebsocketConstants.CONNECTION_KEY_LIST, List.class);
        messageDistributor(connectionKeyList, message);
    } else {
        String connectionKey = in.getHeader(WebsocketConstants.CONNECTION_KEY, String.class);
        messageDistributor(Arrays.asList(connectionKey), message);
    }
}
Also used : Message(org.apache.camel.Message) InputStream(java.io.InputStream) Reader(java.io.Reader) ArrayList(java.util.ArrayList) List(java.util.List) WebSocket(org.atmosphere.websocket.WebSocket)

Example 27 with WebSocket

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

the class MemoryWebSocketStoreTest method testAddAndRemove.

@Test
public void testAddAndRemove() throws Exception {
    MemoryWebSocketStore store = new MemoryWebSocketStore();
    WebSocket webSocket1 = EasyMock.createMock(WebSocket.class);
    WebSocket webSocket2 = EasyMock.createMock(WebSocket.class);
    String connectionKey1 = UUID.randomUUID().toString();
    String connectionKey2 = UUID.randomUUID().toString();
    store.addWebSocket(connectionKey1, webSocket1);
    verifyGet(store, connectionKey1, webSocket1, true);
    assertEquals(1, store.getAllWebSockets().size());
    store.addWebSocket(connectionKey2, webSocket2);
    verifyGet(store, connectionKey2, webSocket2, true);
    verifyGet(store, connectionKey1, webSocket1, true);
    assertEquals(2, store.getAllWebSockets().size());
    store.removeWebSocket(connectionKey1);
    verifyGet(store, connectionKey1, webSocket1, false);
    store.removeWebSocket(webSocket2);
    verifyGet(store, connectionKey2, webSocket2, false);
    assertEquals(0, store.getAllWebSockets().size());
}
Also used : WebSocket(org.atmosphere.websocket.WebSocket) Test(org.junit.Test)

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