Netty:如何在仍然支持本机套接字的同时添加 websocket 握手和框架

Netty: How to add websocket handshake and framing while still supporting native socket

对我来说,Netty 4 似乎没有开箱即用的混合 websocket/native 套接字支持。我在我的服务器上使用自定义二进制协议,它应该同时支持本机和 websocket在同一个端口。这是我在 ServerInitializer:

中尝试的
@Override
public void initChannel(SocketChannel ch) {

    System.out.println("channel initialized");

    ChannelPipeline pipeline = ch.pipeline();

    pipeline.addLast(new HttpServerCodec());
    pipeline.addLast(new HttpObjectAggregator(65536));

    // client decoders cannot be singleton....
    pipeline.addLast(new WebSocketDecoder(), new ClientCommandDecoder());

    pipeline.addLast(this.webSocketEncoder, this.serverCommandEncoder);

    pipeline.addLast(this.roomHandler);
}

WebSocketDecoder 取自示例,但是它似乎使用了一个仅处理 FullHttpRequest 的握手器,它使用 HttpObjectAggregator 是强制性的。

但是,如果不是 HTTP 请求,HttpServerCodecHttpObjectAggregator 似乎都不会传递输入数据。所以这就是我想知道的:

  1. 如果输入数据不是网络套接字而是本机,我可以编写给定 类 的自定义实现并覆盖逻辑以传递输入数据吗
  2. 或者我能以某种方式检测输入数据是否来自 websocket 并转向两个不同的流(一个支持 HTTP,另一个不支持)

您需要根据您的输入即时调整管道。

请查看我们的 PortUnification 示例...