Python中文网

一个关于 编程问题的解答网站.

有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在netty WebSocket中跨ChannelHandlerContext识别用户

我不知道如何识别用户,因为LoggingHandler和SimpleChannelInboundHandler中的ChannelHandlerContext不同:

class WebSocketServerInitializer extends ChannelInitializer<SocketChannel> {
@Override
    public void initChannel(SocketChannel ch) throws Exception {                

            ChannelPipeline pipeline = ch.pipeline();

            pipeline.addLast(new HttpServerCodec());
            pipeline.addLast(new HttpObjectAggregator(65536));
            pipeline.addLast(new WebSocketServerProtocolHandler(WEBSOCKET_PATH, null, true));
            pipeline.addLast(new WebSocketIndexPageHandler(WEBSOCKET_PATH));
            pipeline.addLast(new WebSocketFrameHandler());
            pipeline.addLast(new SessionManagerAxx());   

    }
}

日志处理程序:

class SessionManagerAxx extends LoggingHandler {

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {

            if (evt.equals(ServerHandshakeStateEvent.HANDSHAKE_COMPLETE)) {
                   ///add the user
                    Clients.getInstance().addNewClient(ctx);
            }

            super.userEventTriggered(ctx, evt);
    }
}

框架处理程序:

class WebSocketFrameHandler extends SimpleChannelInboundHandler<WebSocketFrame> {

    private static final Logger logger = LoggerFactory.getLogger(WebSocketFrameHandler.class);

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, WebSocketFrame frame) throws Exception {
         Clients.getInstance().getClient(ctx);
        //Handle user msg
    }
}

共 (0) 个答案