您将如何扩展频道?
我需要一些有关 Netty 的帮助。我想知道如何扩展 Channel 接口并向其中添加我自己的方法并使 Netty 使用它(或强制转换为它)?
马特。
I need a little help with Netty. I was wondering how would you extend the Channel interface and add my own methods to it and make Netty use it (or cast to it)?
Matt.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
答案对于 netty 4.1.25.Final 来说是实际的。
首先扩展您的通道类型,
如果您想将其用于客户端引导,只需将该类传递给它即可。
对于服务器引导程序,您需要覆盖 NioServerSocketChannel 并将 NioServerSocketChannel.class 传递给 ServerBootstrap。
The answer is actual for netty 4.1.25.Final.
First extend your channel type
If you want to use it for client bootstap just pass that class to it.
For server bootstrap you need to override NioServerSocketChannel and pass NioServerSocketChannel.class to ServerBootstrap.
我想更好的解决方案是将 Channel“包装”到您的类中,将该类存储在 ChannelLocal 或 ChannelHandlerContext 中。然后只需检索您的实现并使用它。这将允许您在 NIO 和 OIO 之间切换,而无需担心实现。
我在 niosmtp 中做了类似的事情:
https://github.com/normanmaurer/niosmtp/blob/master/src/main/java/me/normanmaurer/niosmtp/transport/netty/NettySMTPClientSession.java
这也可以帮助您解耦你的代码。
要提供您自己的通道,您需要破解套接字实现。但我认为你应该尽可能避免它。
I guess a better solution would be to "wrap" the Channel into your class store the class in a ChannelLocal or in the ChannelHandlerContext. Then jsut retrieve your implementation and use it. This will allow you to switch between NIO and OIO without the need to worry about the implementation.
I'm doing something similar in niosmtp:
https://github.com/normanmaurer/niosmtp/blob/master/src/main/java/me/normanmaurer/niosmtp/transport/netty/NettySMTPClientSession.java
This also helps you to decouble your code.
To provide your own channel you would need to hack the socket implementation. But as I think you should avoid it whenever possible.