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);
}
}
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());
}
Aggregations