【发布时间】:2019-02-16 16:16:30
【问题描述】:
我正在使用 Angular 7 webapp 中的 websocket。这个想法是让“节点”为网络应用程序提供服务 - 例如地址:
http://localhost:4200/
和 websocket 在另一个由 Netty / Java 应用程序服务的地址和端口上。
在网络应用程序上,我导航到 websocket 的页面。有一个文本框用于输入 websocket 的地址和按钮:'Connect' - 将我们连接到 web-socket 服务器。例如测试这个地址可以正常工作:
ws://echo.websocket.org // works - we get a response
or
ws://echo.websocket.org:80 // works - we get a response
相反,我想让 Netty 服务器为 websocket 提供服务。我启动服务器 - 服务地址:
http://localhost:8081/
并且运行良好。 (我可以从单独的浏览器窗口和服务器在此地址和端口上提供的 websocket 页面进行连接)。
但现在我想从我的网络应用程序连接到 Netty 服务器 - localhost:4200/。所以,我为套接字连接提供了这个地址;
ws://localhost:8081/
然后点击“连接”。
Netty 服务器对此日志做出反应(所有行同时出现):
Feb 16, 2019 7:26:20 AM io.netty.handler.logging.LoggingHandler channelRead
INFO: [id: 0xfd77a1f7, L:/0:0:0:0:0:0:0:0:8081] READ: [id: 0x24ad1bc1, L:/127.0.0.1:8081 - R:/127.0.0.1:51928]
Feb 16, 2019 7:26:20 AM io.netty.handler.logging.LoggingHandler channelReadComplete
INFO: [id: 0xfd77a1f7, L:/0:0:0:0:0:0:0:0:8081] READ COMPLETE
Websocke channel Active:[id: 0x24ad1bc1, L:/127.0.0.1:8081 - R:/127.0.0.1:51928]
Websocke channel Inactive:[id: 0x24ad1bc1, L:/127.0.0.1:8081 ! R:/127.0.0.1:51928]
Websocke channel Unregistered:[id: 0x24ad1bc1, L:/127.0.0.1:8081 ! R:/127.0.0.1:51928]
这可能意味着通信正常,但是,Netty 不接受并且正在关闭 websocket。
这些是我在 Netty 管道中的处理程序:
new HttpServerCodec());
new HttpObjectAggregator(65536));
new WebSocketServerCompressionHandler());
new WebSocketServerProtocolHandler(WEBSOCKET_PATH, null, true));
// Next handler serves index.html page
// ... extends SimpleChannelInboundHandler<FullHttpRequest>
// @Override
// protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest req) throws Exception {
new WebSocketIndexPageHandlerA1(WEBSOCKET_PATH));
// Next handler handles websocket communication
// ... extends SimpleChannelInboundHandler<WebSocketFrame>
// @Override
// protected void channelRead0(ChannelHandlerContext ctx, WebSocketFrame frame) {
new WebSocketFrameHandlerA1());
我们如何让 Netty 接受这个 websocket 请求并将流向下传递到我的最后一个处理程序 - web socket 消息处理程序 - WebSocketFrameHandlerA1 以便它可以回复 websocket 客户端 - 无论它们来自哪里?
【问题讨论】: