简体   繁体   English

ZeroMQ,客户端< - >服务器,只有让客户端连接到主机才能实现双向通信?

[英]ZeroMQ, Client<-> Server , bi-directional communication possible with only having the client connect to host?

I am facing the following problem: 我面临以下问题:

I have a client (ultimately n-clients) and like to connect to a server. 我有一个客户端(最终是n-clients)并且喜欢连接到服务器。 Clients know the server/host address but the server does not know the address of the client(s). 客户端知道服务器/主机地址,但服务器不知道客户端的地址。 I like to be able to accomplish the following messaging patterns between client-server (both, the client and the server need to be able to accomplish the following message patterns): 我希望能够在客户端 - 服务器之间完成以下消息传递模式(客户端和服务器都需要能够完成以下消息模式):

  • Publish Messages (no reply expected) 发布消息(无需回复)
  • Receive Messages (no reply expected) 接收消息(无预期回复)
  • Request / Receive Messages (reply expected) 请求/接收消息(预期回复)
  • Stream messages (this may be redundant as it may be served through the publish message pattern above) 流消息(这可能是多余的,因为它可能通过上面的发布消息模式提供)

Again the important point, and where I struggle is how to connect to the host while still being able to send AND receive messages. 同样重要的一点,我奋斗的地方是如何连接到主机,同时仍然能够发送和接收消息。 The host has no ability to connect to clients, it can only accept client connection requests. 主机无法连接到客户端,它只能接受客户端连接请求。 Please note that I do not look for a solution with proxy/broker to which both client and server connect otherwise I could go directly with solutions such as rabbitmq. 请注意,我不寻找与客户端和服务器连接的代理/代理的解决方案,否则我可以直接使用rabbitmq等解决方案。

How can I best accomplish this, even better with reference to code samples. 我怎样才能最好地完成这一点,参考代码示例甚至更好。

Thanks a lot. 非常感谢。

For connecting to the server, you need a DEALER socket on the client side, and a ROUTER socket on the server. 要连接到服务器,客户端需要一个DEALER套接字,服务器上需要一个ROUTER套接字。 Because you need a publish subscribe pattern you will need a SUB socket on the client side, and a PUB socket on the server side. 因为您需要发布订阅模式,所以您需要在客户端使用SUB套接字,在服务器端使用PUB套接字。

  Client       Server
+-------+      +--------+
| Dealer| <--> | Router |
|  Sub  | <--  |  Pub   |
+-------+      +--------+

So you bind the Router and the Pub sockets, and connect the Dealer, and the Sub sockets. 因此,您绑定路由器和Pub套接字,并连接Dealer和Sub套接字。 Than when you want to: 比你想要的时间:

  • Publish Messages (no reply expected): Create an envelope ( pub, channel, message ) and send it through the Dealer, on the Router side the router will receive the following envelope ( dealer, pub, channel, message ), so you can publish the message on channel through PUB socket. 发布消息(无需回复):创建一个信封(酒吧,频道,消息)并通过经销商发送,在路由器端,路由器将收到以下信封(经销商,酒吧,频道,消息),以便您可以发布通过PUB套接字在通道上的消息。

  • Receive Messages (no reply expected): It's done with the SUB sockets on the client side, and since every publication goes through the ROUTER you can easily implement a subscription mechanism, or just add a SUB socket on the server side, and connect (inproc) to the PUB socket (maybe this is a cleaner solution). 接收消息(无需回复):使用客户端的SUB套接字完成,并且由于每个发布都通过ROUTER,您可以轻松实现订阅机制,或者只是在服务器端添加SUB套接字并连接(inproc) )到PUB插座(也许这是一个更清洁的解决方案)。

  • Request / Receive Messages (reply expected): It can be done with the Dealer - Router. 请求/接收消息(预期回复):可以通过经销商 - 路由器完成。 you just create a different envelope ( req, message ), so your Router ( receive: dealer, req, message ) will know that it should be processed, and can send a reply to the dealer. 您只需创建一个不同的信封(请求,消息),因此您的路由器(接收:经销商,请求,消息)将知道它应该被处理,并可以向经销商发送回复。 If your server needs to send requests to the clients, you just need to keep track of the connected clients, and send an envelope (dealer, req, msg), so your dealer can reply with example a ( rep, message ) envelope. 如果您的服务器需要向客户端发送请求,您只需要跟踪连接的客户端,并发送信封(经销商,请求,消息),以便您的经销商可以回复示例(代表,消息)信封。

  • Streaming: as you stated it can be done with the publish pattern 流式传输:如您所述,可以使用发布模式完成

This is how I would do it, if you need heart beating, it gets a bit complicated, but not much. 我就是这样做的,如果你需要心脏跳动,它会有点复杂,但并不多。

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

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