io.undertow.websockets.spi.AsyncWebSocketHttpServerExchange Java Examples

The following examples show how to use io.undertow.websockets.spi.AsyncWebSocketHttpServerExchange. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WebSocketHandler.java    From core-ng-project with Apache License 2.0 5 votes vote down vote up
public void handle(HttpServerExchange exchange, RequestImpl request, ActionLog actionLog) {
    String path = exchange.getRequestPath();
    String action = "ws:" + path;
    actionLog.action(action + ":open");

    ChannelHandler handler = handlers.get(path);
    if (handler == null) throw new NotFoundException("not found, path=" + path, "PATH_NOT_FOUND");

    request.session = loadSession(request, actionLog);  // load session as late as possible, so for sniffer/scan request with sessionId, it won't call redis every time even for 404/405

    var webSocketExchange = new AsyncWebSocketHttpServerExchange(exchange, channels);
    exchange.upgradeChannel((connection, httpServerExchange) -> {
        WebSocketChannel channel = handshake.createChannel(webSocketExchange, connection, webSocketExchange.getBufferPool());
        try {
            var wrapper = new ChannelImpl(channel, context, handler);
            wrapper.action = action;
            wrapper.clientIP = request.clientIP();
            wrapper.refId = actionLog.id;   // with ws, correlationId and refId are same as parent http action id
            actionLog.context("channel", wrapper.id);
            channel.setAttribute(CHANNEL_KEY, wrapper);
            channel.addCloseTask(channelCloseListener);
            context.add(wrapper);

            handler.listener.onConnect(request, wrapper);
            actionLog.context("room", wrapper.rooms.toArray()); // may join room onConnect
            channel.getReceiveSetter().set(messageListener);
            channel.resumeReceives();

            channels.add(channel);
        } catch (Throwable e) {
            // upgrade is handled by io.undertow.server.protocol.http.HttpReadListener.exchangeComplete, and it catches all exceptions during onConnect
            logManager.logError(e);
            IoUtils.safeClose(connection);
        }
    });
    handshake.handshake(webSocketExchange);
}