Search in sources :

Example 1 with WebSocketConnection

use of act.xio.WebSocketConnection in project actframework by actframework.

the class UndertowWebSocketConnectionHandler method handle.

@Override
public void handle(final ActionContext context) {
    if (logger.isTraceEnabled()) {
        logger.trace("handle websocket connection request to %s", context.req().url());
    }
    final UndertowRequest req = (UndertowRequest) context.req();
    HttpServerExchange exchange = req.exchange();
    try {
        Handlers.websocket(new WebSocketConnectionCallback() {

            @Override
            public void onConnect(WebSocketHttpExchange exchange, WebSocketChannel channel) {
                final WebSocketConnection connection = new UndertowWebSocketConnection(channel, context.session());
                channel.setAttribute("act_conn", connection);
                connectionManager.registerNewConnection(connection, context);
                final WebSocketContext wsCtx = new WebSocketContext(req.url(), connection, connectionManager, context, connectionManager.app());
                if (logger.isTraceEnabled()) {
                    logger.trace("websocket context[%s] created for %s", connection.sessionId(), context.req().url());
                }
                channel.getReceiveSetter().set(new AbstractReceiveListener() {

                    @Override
                    protected void onFullTextMessage(WebSocketChannel channel, BufferedTextMessage message) throws IOException {
                        WebSocketContext.current(wsCtx);
                        String payload = message.getData();
                        if (logger.isTraceEnabled()) {
                            logger.trace("websocket message received: %s", payload);
                        }
                        wsCtx.messageReceived(payload);
                        invoke(wsCtx);
                    }

                    @Override
                    protected void onClose(WebSocketChannel webSocketChannel, StreamSourceFrameChannel channel) throws IOException {
                        if (logger.isTraceEnabled()) {
                            logger.trace("websocket closed: ", connection.sessionId());
                        }
                        WebSocketContext.current(wsCtx);
                        super.onClose(webSocketChannel, channel);
                        connection.destroy();
                        context.app().eventBus().emit(new WebSocketCloseEvent(wsCtx));
                    }
                });
                channel.resumeReceives();
                Act.eventBus().emit(new WebSocketConnectEvent(wsCtx));
            }
        }).handleRequest(exchange);
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw ActErrorResult.of(e);
    }
}
Also used : WebSocketCloseEvent(act.ws.WebSocketCloseEvent) WebSocketConnectEvent(act.ws.WebSocketConnectEvent) WebSocketConnection(act.xio.WebSocketConnection) WebSocketChannel(io.undertow.websockets.core.WebSocketChannel) IOException(java.io.IOException) BufferedTextMessage(io.undertow.websockets.core.BufferedTextMessage) IOException(java.io.IOException) WebSocketContext(act.ws.WebSocketContext) HttpServerExchange(io.undertow.server.HttpServerExchange) WebSocketHttpExchange(io.undertow.websockets.spi.WebSocketHttpExchange) StreamSourceFrameChannel(io.undertow.websockets.core.StreamSourceFrameChannel) AbstractReceiveListener(io.undertow.websockets.core.AbstractReceiveListener) WebSocketConnectionCallback(io.undertow.websockets.WebSocketConnectionCallback)

Example 2 with WebSocketConnection

use of act.xio.WebSocketConnection in project actframework by actframework.

the class WebSocketConnectionRegistry method accept.

/**
 * Accept a visitor to iterate through the connections attached to the key specified
 *
 * @param key the key
 * @param visitor the visitor
 */
public void accept(String key, $.Function<WebSocketConnection, ?> visitor) {
    ConcurrentMap<WebSocketConnection, WebSocketConnection> connections = registry.get(key);
    if (null == connections) {
        return;
    }
    if (!connections.isEmpty()) {
        List<WebSocketConnection> toBeCleared = null;
        for (WebSocketConnection conn : connections.keySet()) {
            if (conn.closed()) {
                if (null == toBeCleared) {
                    toBeCleared = new ArrayList<>();
                }
                toBeCleared.add(conn);
                continue;
            }
            visitor.apply(conn);
        }
        if (null != toBeCleared) {
            ConcurrentMap<WebSocketConnection, WebSocketConnection> originalCopy = registry.get(key);
            originalCopy.keySet().removeAll(toBeCleared);
        }
    }
}
Also used : WebSocketConnection(act.xio.WebSocketConnection)

Example 3 with WebSocketConnection

use of act.xio.WebSocketConnection in project actframework by actframework.

the class WebSocketConnectionRegistry method signIn.

/**
 * Sign in a group of connections to the registry by key
 * @param key the key
 * @param connections a collection of websocket connections
 */
public void signIn(String key, Collection<WebSocketConnection> connections) {
    if (connections.isEmpty()) {
        return;
    }
    Map<WebSocketConnection, WebSocketConnection> newMap = new HashMap<>();
    for (WebSocketConnection conn : connections) {
        newMap.put(conn, conn);
    }
    ConcurrentMap<WebSocketConnection, WebSocketConnection> bag = ensureConnectionList(key);
    bag.putAll(newMap);
}
Also used : WebSocketConnection(act.xio.WebSocketConnection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

WebSocketConnection (act.xio.WebSocketConnection)3 WebSocketCloseEvent (act.ws.WebSocketCloseEvent)1 WebSocketConnectEvent (act.ws.WebSocketConnectEvent)1 WebSocketContext (act.ws.WebSocketContext)1 HttpServerExchange (io.undertow.server.HttpServerExchange)1 WebSocketConnectionCallback (io.undertow.websockets.WebSocketConnectionCallback)1 AbstractReceiveListener (io.undertow.websockets.core.AbstractReceiveListener)1 BufferedTextMessage (io.undertow.websockets.core.BufferedTextMessage)1 StreamSourceFrameChannel (io.undertow.websockets.core.StreamSourceFrameChannel)1 WebSocketChannel (io.undertow.websockets.core.WebSocketChannel)1 WebSocketHttpExchange (io.undertow.websockets.spi.WebSocketHttpExchange)1 IOException (java.io.IOException)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1