use of org.atmosphere.websocket.WebSocketEventListenerAdapter in project atmosphere by Atmosphere.
the class WebSocketProcessorTest method inClosePhaseTest.
@Test
public void inClosePhaseTest() throws IOException, ServletException, ExecutionException, InterruptedException {
ByteArrayOutputStream b = new ByteArrayOutputStream();
final WebSocket w = new ArrayBaseWebSocket(b);
final WebSocketProcessor processor = WebSocketProcessorFactory.getDefault().getWebSocketProcessor(framework);
final AtomicBoolean closed = new AtomicBoolean();
framework.addAtmosphereHandler("/*", new AtmosphereHandler() {
@Override
public void onRequest(final AtmosphereResource resource) throws IOException {
resource.addEventListener(new WebSocketEventListenerAdapter() {
@Override
public void onClose(WebSocketEvent event) {
closed.set(true);
}
});
AtmosphereResourceEventImpl.class.cast(resource.getAtmosphereResourceEvent()).isClosedByClient(true);
AsynchronousProcessor.class.cast(framework.getAsyncSupport()).completeLifecycle(resource, false);
processor.close(w, 1005);
}
@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));
assertFalse(closed.get());
}
use of org.atmosphere.websocket.WebSocketEventListenerAdapter in project atmosphere by Atmosphere.
the class WebSocketProcessorTest method inClosePhaseTest.
@Test
public void inClosePhaseTest() throws IOException, ServletException, ExecutionException, InterruptedException {
ByteArrayOutputStream b = new ByteArrayOutputStream();
final WebSocket w = new ArrayBaseWebSocket(b);
final WebSocketProcessor processor = WebSocketProcessorFactory.getDefault().getWebSocketProcessor(framework);
final AtomicBoolean closed = new AtomicBoolean();
framework.addAtmosphereHandler("/*", new AtmosphereHandler() {
@Override
public void onRequest(final AtmosphereResource resource) throws IOException {
resource.addEventListener(new WebSocketEventListenerAdapter() {
@Override
public void onClose(WebSocketEvent event) {
closed.set(true);
}
});
AtmosphereResourceEventImpl.class.cast(resource.getAtmosphereResourceEvent()).isClosedByClient(true);
AsynchronousProcessor.class.cast(framework.getAsyncSupport()).completeLifecycle(resource, false);
processor.close(w, 1005);
}
@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));
assertFalse(closed.get());
}
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));
}
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));
}
use of org.atmosphere.websocket.WebSocketEventListenerAdapter in project scheduling by ow2-proactive.
the class SchedulerStateRest method publish.
/**
* Accepts an {@link EventSubscription} instance which specifies the types
* of SchedulerEvents which interest the client. When such Scheduler event
* occurs, it will be communicated to the client in the form of
* {@link EventNotification} utilizing the WebSocket channel initialized
* previously.
*/
@POST
@Path("/events")
@Produces("application/json")
public EventNotification publish(@Context HttpServletRequest req, EventSubscription subscription) throws RestException {
HttpSession session = req.getSession();
String broadcasterId = (String) session.getAttribute(ATM_BROADCASTER_ID);
final SchedulerProxyUserInterface scheduler = checkAccess(broadcasterId);
SchedulerEventBroadcaster eventListener = new SchedulerEventBroadcaster(broadcasterId);
try {
final SchedulerEventBroadcaster activedEventListener = PAActiveObject.turnActive(eventListener);
scheduler.addEventListener(activedEventListener, subscription.isMyEventsOnly(), EventUtil.toSchedulerEvents(subscription.getEvents()));
AtmosphereResource atmResource = getAtmosphereResourceFactory().find((String) session.getAttribute(ATM_RESOURCE_ID));
atmResource.addEventListener(new WebSocketEventListenerAdapter() {
@Override
public void onDisconnect(@SuppressWarnings("rawtypes") WebSocketEvent event) {
try {
logger.info("#### websocket disconnected remove listener ####");
scheduler.removeEventListener();
} catch (Exception e) {
logger.error(e);
}
PAActiveObject.terminateActiveObject(activedEventListener, true);
}
});
} catch (SchedulerException e) {
throw RestException.wrapExceptionToRest(e);
} catch (ActiveObjectCreationException | NodeException e) {
throw new RuntimeException(e);
}
return new EventNotification(EventNotification.Action.NONE, null, null);
}
Aggregations