Search in sources :

Example 1 with WebSocketPayload

use of org.apache.wicket.protocol.ws.api.event.WebSocketPayload in project wicket by apache.

the class AbstractWebSocketProcessor method broadcastMessage.

/**
 * Exports the Wicket thread locals and broadcasts the received message from the client to all
 * interested components and behaviors in the page with id {@code #pageId}
 * <p>
 *     Note: ConnectedMessage and ClosedMessage messages are notification-only. I.e. whatever the
 *     components/behaviors write in the WebSocketRequestHandler will be ignored because the protocol
 *     doesn't expect response from the user.
 * </p>
 *
 * @param message
 *      the message to broadcast
 */
public final void broadcastMessage(final IWebSocketMessage message) {
    IKey key = getRegistryKey();
    IWebSocketConnection connection = connectionRegistry.getConnection(application, sessionId, key);
    if (connection != null && (connection.isOpen() || message instanceof ClosedMessage)) {
        Application oldApplication = ThreadContext.getApplication();
        Session oldSession = ThreadContext.getSession();
        RequestCycle oldRequestCycle = ThreadContext.getRequestCycle();
        WebResponse webResponse = webSocketSettings.newWebSocketResponse(connection);
        try {
            WebSocketRequestMapper requestMapper = new WebSocketRequestMapper(application.getRootRequestMapper());
            RequestCycle requestCycle = createRequestCycle(requestMapper, webResponse);
            ThreadContext.setRequestCycle(requestCycle);
            ThreadContext.setApplication(application);
            Session session;
            if (oldSession == null || message instanceof IWebSocketPushMessage) {
                ISessionStore sessionStore = application.getSessionStore();
                session = sessionStore.lookup(webRequest);
                ThreadContext.setSession(session);
            } else {
                session = oldSession;
            }
            IPageManager pageManager = session.getPageManager();
            Page page = getPage(pageManager);
            if (page != null) {
                WebSocketRequestHandler requestHandler = webSocketSettings.newWebSocketRequestHandler(page, connection);
                WebSocketPayload payload = createEventPayload(message, requestHandler);
                if (!(message instanceof ConnectedMessage || message instanceof ClosedMessage || message instanceof AbortedMessage)) {
                    requestCycle.scheduleRequestHandlerAfterCurrent(requestHandler);
                }
                IRequestHandler broadcastingHandler = new WebSocketMessageBroadcastHandler(pageId, resourceName, payload);
                requestMapper.setHandler(broadcastingHandler);
                requestCycle.processRequestAndDetach();
            } else {
                LOG.debug("Page with id '{}' has been expired. No message will be broadcast!", pageId);
            }
        } catch (Exception x) {
            LOG.error("An error occurred during processing of a WebSocket message", x);
        } finally {
            try {
                webResponse.close();
            } finally {
                ThreadContext.setApplication(oldApplication);
                ThreadContext.setRequestCycle(oldRequestCycle);
                ThreadContext.setSession(oldSession);
            }
        }
    } else {
        LOG.debug("Either there is no connection({}) or it is closed.", connection);
    }
}
Also used : IPageManager(org.apache.wicket.page.IPageManager) WebResponse(org.apache.wicket.request.http.WebResponse) ISessionStore(org.apache.wicket.session.ISessionStore) IRequestHandler(org.apache.wicket.request.IRequestHandler) RequestCycle(org.apache.wicket.request.cycle.RequestCycle) ClosedMessage(org.apache.wicket.protocol.ws.api.message.ClosedMessage) Page(org.apache.wicket.Page) WebPage(org.apache.wicket.markup.html.WebPage) WebSocketPayload(org.apache.wicket.protocol.ws.api.event.WebSocketPayload) IWebSocketPushMessage(org.apache.wicket.protocol.ws.api.message.IWebSocketPushMessage) ConnectedMessage(org.apache.wicket.protocol.ws.api.message.ConnectedMessage) AbortedMessage(org.apache.wicket.protocol.ws.api.message.AbortedMessage) IKey(org.apache.wicket.protocol.ws.api.registry.IKey) Application(org.apache.wicket.Application) WebApplication(org.apache.wicket.protocol.http.WebApplication) HttpSession(javax.servlet.http.HttpSession) Session(org.apache.wicket.Session)

Example 2 with WebSocketPayload

use of org.apache.wicket.protocol.ws.api.event.WebSocketPayload in project wicket by apache.

the class WebSocketBehavior method onEvent.

@Override
public void onEvent(Component component, IEvent<?> event) {
    super.onEvent(component, event);
    Object payload = event.getPayload();
    if (payload instanceof WebSocketPayload) {
        WebSocketPayload<?> wsPayload = (WebSocketPayload<?>) payload;
        WebSocketRequestHandler webSocketHandler = wsPayload.getHandler();
        if (payload instanceof WebSocketTextPayload) {
            WebSocketTextPayload textPayload = (WebSocketTextPayload) payload;
            TextMessage data = textPayload.getMessage();
            onMessage(webSocketHandler, data);
        } else if (wsPayload instanceof WebSocketBinaryPayload) {
            WebSocketBinaryPayload binaryPayload = (WebSocketBinaryPayload) wsPayload;
            BinaryMessage binaryData = binaryPayload.getMessage();
            onMessage(webSocketHandler, binaryData);
        } else if (wsPayload instanceof WebSocketConnectedPayload) {
            WebSocketConnectedPayload connectedPayload = (WebSocketConnectedPayload) wsPayload;
            ConnectedMessage message = connectedPayload.getMessage();
            onConnect(message);
        } else if (wsPayload instanceof WebSocketClosedPayload) {
            WebSocketClosedPayload closedPayload = (WebSocketClosedPayload) wsPayload;
            ClosedMessage message = closedPayload.getMessage();
            onClose(message);
        } else if (wsPayload instanceof WebSocketErrorPayload) {
            WebSocketErrorPayload errorPayload = (WebSocketErrorPayload) wsPayload;
            ErrorMessage message = errorPayload.getMessage();
            onError(webSocketHandler, message);
        } else if (wsPayload instanceof WebSocketAbortedPayload) {
            WebSocketAbortedPayload abortedPayload = (WebSocketAbortedPayload) wsPayload;
            AbortedMessage message = abortedPayload.getMessage();
            onAbort(message);
        } else if (wsPayload instanceof WebSocketPushPayload) {
            WebSocketPushPayload pushPayload = (WebSocketPushPayload) wsPayload;
            IWebSocketPushMessage message = pushPayload.getMessage();
            onPush(webSocketHandler, message);
        }
    }
}
Also used : ClosedMessage(org.apache.wicket.protocol.ws.api.message.ClosedMessage) WebSocketAbortedPayload(org.apache.wicket.protocol.ws.api.event.WebSocketAbortedPayload) WebSocketPayload(org.apache.wicket.protocol.ws.api.event.WebSocketPayload) WebSocketConnectedPayload(org.apache.wicket.protocol.ws.api.event.WebSocketConnectedPayload) WebSocketTextPayload(org.apache.wicket.protocol.ws.api.event.WebSocketTextPayload) IWebSocketPushMessage(org.apache.wicket.protocol.ws.api.message.IWebSocketPushMessage) BinaryMessage(org.apache.wicket.protocol.ws.api.message.BinaryMessage) ConnectedMessage(org.apache.wicket.protocol.ws.api.message.ConnectedMessage) AbortedMessage(org.apache.wicket.protocol.ws.api.message.AbortedMessage) WebSocketBinaryPayload(org.apache.wicket.protocol.ws.api.event.WebSocketBinaryPayload) WebSocketPushPayload(org.apache.wicket.protocol.ws.api.event.WebSocketPushPayload) ErrorMessage(org.apache.wicket.protocol.ws.api.message.ErrorMessage) TextMessage(org.apache.wicket.protocol.ws.api.message.TextMessage) WebSocketErrorPayload(org.apache.wicket.protocol.ws.api.event.WebSocketErrorPayload) WebSocketClosedPayload(org.apache.wicket.protocol.ws.api.event.WebSocketClosedPayload)

Aggregations

WebSocketPayload (org.apache.wicket.protocol.ws.api.event.WebSocketPayload)2 AbortedMessage (org.apache.wicket.protocol.ws.api.message.AbortedMessage)2 ClosedMessage (org.apache.wicket.protocol.ws.api.message.ClosedMessage)2 ConnectedMessage (org.apache.wicket.protocol.ws.api.message.ConnectedMessage)2 IWebSocketPushMessage (org.apache.wicket.protocol.ws.api.message.IWebSocketPushMessage)2 HttpSession (javax.servlet.http.HttpSession)1 Application (org.apache.wicket.Application)1 Page (org.apache.wicket.Page)1 Session (org.apache.wicket.Session)1 WebPage (org.apache.wicket.markup.html.WebPage)1 IPageManager (org.apache.wicket.page.IPageManager)1 WebApplication (org.apache.wicket.protocol.http.WebApplication)1 WebSocketAbortedPayload (org.apache.wicket.protocol.ws.api.event.WebSocketAbortedPayload)1 WebSocketBinaryPayload (org.apache.wicket.protocol.ws.api.event.WebSocketBinaryPayload)1 WebSocketClosedPayload (org.apache.wicket.protocol.ws.api.event.WebSocketClosedPayload)1 WebSocketConnectedPayload (org.apache.wicket.protocol.ws.api.event.WebSocketConnectedPayload)1 WebSocketErrorPayload (org.apache.wicket.protocol.ws.api.event.WebSocketErrorPayload)1 WebSocketPushPayload (org.apache.wicket.protocol.ws.api.event.WebSocketPushPayload)1 WebSocketTextPayload (org.apache.wicket.protocol.ws.api.event.WebSocketTextPayload)1 BinaryMessage (org.apache.wicket.protocol.ws.api.message.BinaryMessage)1