如何通过MINA正确发送数据?
我正在尝试开始使用 MINA,并且所有示例似乎都将数据写入会话,而不是使用可以反复写入相同类型数据的方法。
我正在尝试使用 org.apache.mina.filter.codec.demux.MessageEncoder
/ MessageDecoder
来 encode
/ 解码消息,这将允许我始终在中心位置执行任务,而不是像示例那样在代码中内联执行。
假设我有一个 ProtocolCodecFactory
(它扩展了 DemushingProtocolCodecFactory
),它有一个 LoginRequestEncoder
(它实现了 MessageEncoder
,并通过工厂的 addMessageEncoder
方法添加)。这是否意味着我不应该使用用户名/密码数据直接调用 session.write()
,而是应该执行类似的操作?
LoginRequest request = new LoginRequest(username, password);
new ProtocolCodecFactory()
.getEncoder(session)
.encode(session, request, someProtocolEncoderOutput);
我不会撒谎...MINA 似乎应该简化网络过程,而且我确信当我掌握它时它会的,但我现在完全困惑了。
I'm attempting to get started with MINA, and all of the examples seem to have data written to the session, rather than making use of a method that can write the same type of data over and over.
I'm trying to make use of org.apache.mina.filter.codec.demux.MessageEncoder
/ MessageDecoder
to encode
/ decode
messages, which will allow me to always perform the task in a central location instead of doing it inline in the code, like the examples do.
Let's say I have a ProtocolCodecFactory
(which extends DemuxingProtocolCodecFactory
) that has a LoginRequestEncoder
(which implements MessageEncoder<LoginRequest>
, and was added via the factory's addMessageEncoder
method). Does that mean that instead of directly calling session.write()
with the username/password data, I should instead do something like this?
LoginRequest request = new LoginRequest(username, password);
new ProtocolCodecFactory()
.getEncoder(session)
.encode(session, request, someProtocolEncoderOutput);
I'm not going to lie...MINA seems like it's supposed to simplify the networking process, and I'm sure it will when I get a handle on it, but I'm thoroughly confused right now.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明,您可以通过 IoSession.write()。这是一个基于我原来的问题的简单示例:
It turns out you can simple send a request via IoSession.write(). Here is a simple example based upon my original question: