未指定升级时在 WebSocketServerProtocolHandler 之后继续管道?

Continue pipeline after WebSocketServerProtocolHandler when no upgrade specified?

我在根路径上有 WebSocketServerProtocolHandler 处理程序,我也在那里接受常规 HTTP 请求。但是,WebSocketServerProtocolHandler 不会让我使用我的 HTTP 请求,因为它假设一切都是网络套接字并响应:

not a WebSocket handshake request: missing upgrade

当不需要升级到网络套接字时,我可以在 WebSocketServerProtocolHandler 之后继续执行管道吗?换句话说,我需要 HTTP 和 WebSockets 都在同一个地址上运行。

是的,我可能 copy/paste 自己做 WebSocketServerProtocolHandler,但是有更好的方法吗?

WebSocketServerProtocolHander 的 javadoc 让您参考 io.netty.example.http.websocketx.html5.WebSocketServer 示例。然而,这里发生的事情可能并不十分明显。

如果您查看 WebSocketServerInitializer you can see that by default it sets up a fairly standard HTTP pipeline. This is because, as you know, the upgrade request is a HTTP request. The magic happens in the handleHttpRequest method of WebSocketServerHandler. It falls through to line 96 and assumes it's an upgrade request (you might want to actually check). It creates a WebSocketServerHandshaker and starts the handshake. The trick is that it automatically reconfigures the pipeline to handle web socket traffic so you don't have to. Take a look at the handshake method in WebSocketServerHandshaker 的源代码以了解发生了什么。