简体   繁体   English

如何使用ZeroMq实现异步即时消息聊天

[英]How to implement async instant messaging chat with ZeroMq

Approach 途径

I'm building an asynchronous IM/chat application with ZeroMq, just as a learning exercise, using the following approach. 作为学习练习,我正在使用ZeroMq构建异步IM /聊天应用程序,它使用以下方法。

The server uses two ROUTER sockets, similar to lbbroker.java , reading from incoming and writing to outgoing , while manipulating the envelope for delivery to the intended receiver. 服务器使用两个类似于lbbroker.java ROUTER套接字,从incoming读取并写入outgoing ,同时操纵信封以将其传递给预期的接收者。

Clients set a unique identity with socket.setIdentity() , and use PULL and PUSH sockets, instead of REQ/REP, for sending and receiving chat messages asynchronously. 客户端使用socket.setIdentity()设置唯一身份,并使用PULL和PUSH套接字而不是REQ / REP来异步发送和接收聊天消息。 I've built and tested the code, everything appears to work fine. 我已经构建并测试了代码,一切似乎都可以正常工作。

异步聊天

Questions 问题

  • Is the use of PUSH/PULL with ROUTER a valid approach? 将PUSH / PULL与ROUTER结合使用是否有效?

  • Why do chat clients become unreachable when dropping off then reconnecting? 为什么在断开然后重新连接时聊天客户端变得无法访问?

Sally becomes unreachable if she disconnects then reconnects, she can no longer receives chat messages. 如果Sally断开连接然后重新连接,Sally将无法连接,她将无法再接收聊天消息。 After debugging the server, I confirmed it's definitely sending messages with the correct envelope to sally. 调试服务器后,我确认它肯定是发送带有正确信封的邮件发送给Sally。 I also used outgoing.setRouterMandatory(true) to ensure unaddressable messages weren't being dropped by the server, but no errors got raised. 我还使用outgoing.setRouterMandatory(true)来确保服务器不会丢弃无法寻址的消息,但是不会引发任何错误。

After further research, while PUSH and PULL sockets work with ROUTER, they are not valid socket combinations as per the ZeroMq spec. 经过进一步研究,虽然PUSH和PULL套接字与ROUTER一起使用,但根据ZeroMq规范,它们不是有效的套接字组合。

A DEALER socket on the client side is a better approach. 客户端上的DEALER套接字是一种更好的方法。 DEALER supports bidirectional message transfer, basically doing the job of PUSH and PULL but with a single socket. DEALER支持双向消息传输,基本上完成了PUSH和PULL的工作,但只有一个套接字。

I started a thread on Github discussing the issue, it includes some sample code for agent-based pattern using DEALER. 我在Github上发起了一个讨论该问题的线程,其中包括一些使用DEALER的基于代理的模式的示例代码。

https://github.com/zeromq/jeromq/issues/81 https://github.com/zeromq/jeromq/issues/81

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM