Netty WebSockets 和 Netty WebSockets TCP套接字
我正在尝试在 Netty 中混合 TCP 和 WebSocket。 我有两台服务器,一台带有 Websockets,另一台带有 TCP。在 DefaultUserGroup 中收集的所有用户,因此我不知道 userGroup 中的哪个用户来自 WebSockets 或 TCP,我无法正确写入它们(DefaultUserGroup.write)。
Websocket 服务器:
ChannelPipeline pipeline = pipeline();
pipeline.addLast("decoder", new HttpRequestDecoder());
pipeline.addLast("aggregator", new HttpChunkAggregator(65536));
pipeline.addLast("encoder", new HttpResponseEncoder());
pipeline.addLast("handler", new WebSocketServerHandler(readQueueHandler));
TCP 服务器:
pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
pipeline.addLast("decoder", new StringDecoder());
pipeline.addLast("encoder", new StringEncoder());
pipeline.addLast("handler", new MyBusinessLogicHandler(readQueueHandler));
I'm trying mixing TCP and WebSockets in Netty.
I'm have two servers, one with Websockets and one with TCP. All users collected in DefaultUserGroup and cause of this i don't know which user in userGroup come from WebSockets or TCP i can't correctly write to them (DefaultUserGroup.write).
Websocket Server:
ChannelPipeline pipeline = pipeline();
pipeline.addLast("decoder", new HttpRequestDecoder());
pipeline.addLast("aggregator", new HttpChunkAggregator(65536));
pipeline.addLast("encoder", new HttpResponseEncoder());
pipeline.addLast("handler", new WebSocketServerHandler(readQueueHandler));
TCP Server:
pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
pipeline.addLast("decoder", new StringDecoder());
pipeline.addLast("encoder", new StringEncoder());
pipeline.addLast("handler", new MyBusinessLogicHandler(readQueueHandler));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果
DefaultUserGroup
是一个ChannelGroup
,为什么不直接创建两个不同的 ChannelGroup,一个用于 TCP,一个用于 WS,并使用各自的帧写入这两个通道组。或者,您可以创建一个带有一些标识通道类型的标志的 POJO,然后在建立新连接时,实例化一个新的 POJO,设置通道类型标志,并将其粘贴到通道的附件 (
channel. setAttachment(...)
)。然后,您可以在写入之前检查通道类型,以确保正确设置帧格式 (channel.getAttachement()
)。If
DefaultUserGroup
is aChannelGroup
, why not just make two different ChannelGroups, one for TCP and one for WS, and write to both using their respective frames.Alternately, you could create a POJO with some flags on it that identify the channel type, then when a new connection is established, instantiate a new POJO, set the channel type flag, and stick it on the channel's attachement (
channel.setAttachement(...)
). Then you can check the channel type before writing to it to make sure you format the frame properly (channel.getAttachement()
).