Search in sources :

Example 1 with AtmosphereResourceEventListener

use of org.atmosphere.runtime.AtmosphereResourceEventListener in project atmosphere by Atmosphere.

the class DefaultWebSocketProcessor method notifyListener.

@Override
public void notifyListener(WebSocket webSocket, WebSocketEventListener.WebSocketEvent event) {
    AtmosphereResource resource = webSocket.resource();
    if (resource == null)
        return;
    AtmosphereResourceImpl r = AtmosphereResourceImpl.class.cast(resource);
    for (AtmosphereResourceEventListener l : r.atmosphereResourceEventListener()) {
        if (WebSocketEventListener.class.isAssignableFrom(l.getClass())) {
            try {
                switch(event.type()) {
                    case CONNECT:
                        WebSocketEventListener.class.cast(l).onConnect(event);
                        break;
                    case DISCONNECT:
                        WebSocketEventListener.class.cast(l).onDisconnect(event);
                        onDisconnect(event, l);
                        break;
                    case CONTROL:
                        WebSocketEventListener.class.cast(l).onControl(event);
                        break;
                    case MESSAGE:
                        WebSocketEventListener.class.cast(l).onMessage(event);
                        break;
                    case HANDSHAKE:
                        WebSocketEventListener.class.cast(l).onHandshake(event);
                        break;
                    case CLOSE:
                        boolean isClosedByClient = r.getAtmosphereResourceEvent().isClosedByClient();
                        l.onDisconnect(new AtmosphereResourceEventImpl(r, !isClosedByClient, false, isClosedByClient, null));
                        onDisconnect(event, l);
                        WebSocketEventListener.class.cast(l).onClose(event);
                        break;
                }
            } catch (Throwable t) {
                logger.debug("Listener error {}", t);
                try {
                    WebSocketEventListener.class.cast(l).onThrowable(new AtmosphereResourceEventImpl(r, false, false, t));
                } catch (Throwable t2) {
                    logger.warn("Listener error {}", t2);
                }
            }
        } else {
            switch(event.type()) {
                case CLOSE:
                    boolean isClosedByClient = r.getAtmosphereResourceEvent().isClosedByClient();
                    l.onDisconnect(new AtmosphereResourceEventImpl(r, !isClosedByClient, false, isClosedByClient, null));
                    break;
            }
        }
    }
}
Also used : AtmosphereResourceEventListener(org.atmosphere.runtime.AtmosphereResourceEventListener) AtmosphereResource(org.atmosphere.runtime.AtmosphereResource) AtmosphereResourceImpl(org.atmosphere.runtime.AtmosphereResourceImpl) AtmosphereResourceEventImpl(org.atmosphere.runtime.AtmosphereResourceEventImpl)

Aggregations

AtmosphereResource (org.atmosphere.runtime.AtmosphereResource)1 AtmosphereResourceEventImpl (org.atmosphere.runtime.AtmosphereResourceEventImpl)1 AtmosphereResourceEventListener (org.atmosphere.runtime.AtmosphereResourceEventListener)1 AtmosphereResourceImpl (org.atmosphere.runtime.AtmosphereResourceImpl)1