gpt4 book ai didi

node.js - 如果 socket.send() 仅使用一次,则 zmq 订阅者无法订阅已发布的消息

转载 作者:搜寻专家 更新时间:2023-11-01 00:39:12 24 4
gpt4 key购买 nike

如果 socket.send() 在发布者中使用仅一次zmq 订阅者将无法订阅消息。

在发布者中使用以下代码时,订阅者能够订阅消息:

var zmq = require('zmq')
, sock = zmq.socket('pub')

sock.bindSync('tcp://127.0.0.1:3000');
var message = {"test" : true};
setInterval(function(){
sock.send(['notify_anomaly', JSON.stringify(message)]);
},1000);

但是如果在publisher代码中去掉setInterval就不行了,如下:

var zmq = require('zmq')
, sock = zmq.socket('pub')

sock.bindSync('tcp://127.0.0.1:3000');
var message = {"test" : true};
sock.send(['notify_anomaly', JSON.stringify(message)]);

最佳答案

这是“慢连接器”问题的结果。

这是来自 guide 的引述:

There is one more important thing to know about PUB-SUB sockets: you do not know precisely when a subscriber starts to get messages. Even if you start a subscriber, wait a while, and then start the publisher, the subscriber will always miss the first messages that the publisher sends. This is because as the subscriber connects to the publisher (something that takes a small but non-zero time), the publisher may already be sending messages out.

基本上,当发布者运行时,它会与订阅者握手。由于这是异步的,发布者可能会在握手完成之前完成发送消息。在那种情况下,订阅者将错过消息。为了让订阅者收到第一条消息,发布者需要等待发送,直到确定订阅者已连接。

这里是进一步的引用:

In Chapter 2 - Sockets and Patterns we'll explain how to synchronize a publisher and subscribers so that you don't start to publish data until the subscribers really are connected and ready.

它显示了如何使用另一个套接字对使用 REQ-REP 在订阅者准备好在订阅套接字上接收信号时发出信号 here .

关于node.js - 如果 socket.send() 仅使用一次,则 zmq 订阅者无法订阅已发布的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40932923/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com