use of org.atmosphere.websocket.WebSocket 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));
}
use of org.atmosphere.websocket.WebSocket in project atmosphere by Atmosphere.
the class WebSocketProcessorTest 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);
framework.addAtmosphereHandler("/*", new AtmosphereHandler() {
@Override
public void onRequest(AtmosphereResource resource) throws IOException {
resource.getBroadcaster().addAtmosphereResource(resource.suspend());
resource.getResponse().write(resource.getRequest().getReader().readLine());
}
@Override
public void onStateChange(AtmosphereResourceEvent event) throws IOException {
event.getResource().write(event.getMessage().toString().getBytes());
}
@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");
framework.getBroadcasterFactory().lookup("/*").broadcast("yoBroadcast").get();
assertEquals(b.toString(), "yoCometyoWebSocketyoBroadcast");
}
use of org.atmosphere.websocket.WebSocket 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());
}
use of org.atmosphere.websocket.WebSocket in project atmosphere by Atmosphere.
the class WebSocketHandlerTest method invalidPathHandler.
@Test
public void invalidPathHandler() throws IOException, ServletException, ExecutionException, InterruptedException {
ByteArrayOutputStream b = new ByteArrayOutputStream();
final WebSocket w = new ArrayBaseWebSocket(b);
final WebSocketProcessor processor = WebSocketProcessorFactory.getDefault().getWebSocketProcessor(framework);
registerWebSocketHandler("/a", new WebSocketProcessor.WebSocketHandlerProxy(new EchoHandler()));
AtmosphereRequest request = new AtmosphereRequestImpl.Builder().destroyable(false).body("yoComet").pathInfo("/abcd").build();
try {
processor.open(w, request, AtmosphereResponseImpl.newInstance(framework.getAtmosphereConfig(), request, w));
fail();
} catch (Exception ex) {
assertEquals(ex.getClass(), AtmosphereMappingException.class);
}
}
use of org.atmosphere.websocket.WebSocket in project atmosphere by Atmosphere.
the class WebSocketHandlerTest method testInjection.
@Test
public void testInjection() throws IOException, ServletException, ExecutionException, InterruptedException {
EchoHandler e = new EchoHandler();
ByteArrayOutputStream b = new ByteArrayOutputStream();
final WebSocket w = new ArrayBaseWebSocket(b);
final WebSocketProcessor processor = WebSocketProcessorFactory.getDefault().getWebSocketProcessor(framework);
registerWebSocketHandler("/*", new WebSocketProcessor.WebSocketHandlerProxy(e));
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");
assertNotNull(e.request);
}
Aggregations