connect
方法的documentation表示,
Connects to the socket at the given remote address and returns immediately. The connection will be made asynchronously in the background.
但是,
await
似乎不适用,如其订户代码示例中所示。subscriber.js
const zmq = require("zeromq")
async function run() {
const sock = new zmq.Subscriber
sock.connect("tcp://127.0.0.1:3000") //Happens async; can we await this?
sock.subscribe("kitty cats")
console.log("Subscriber connected to port 3000")
for await (const [topic, msg] of sock) {
console.log("received a message related to:", topic, "containing message:", msg)
}
}
run()
另外,
connect()
方法可能会引起什么错误?我提供了一个“淫秽”端口号,例如8124000
进行连接。我希望提出一些错误消息。
最佳答案
Q : "what error(s) maybe raised by the
connect()
method?"
如果您从未使用过ZeroMQ,可以在深入了解更多细节之前先浏览"ZeroMQ Principles in less than Five Seconds"
错误部分
ZeroMQ native API为此区分(自v2.1以来未更改)这些错误:
EINVAL
The endpoint supplied is invalid.
EPROTONOSUPPORT
The requested transport protocol is not supported.
ENOCOMPATPROTO
The requested transport protocol is not compatible with the socket type.
ETERM
The ØMQ context associated with the specified socket was terminated.
ENOTSOCK
The provided socket was invalid.
EMTHREAD
No I/O thread is available to accomplish the task.
但是您的实际观察者取决于
zeromq.js
重新包装这些主要状态,因此,最好的下一步是重新阅读包装器源代码,以便了解如何在zeromq.js
中实际处理这些 native API错误状态-包装器。备注:
The following socket events can be generated. This list may be different depending on the ZeroMQ version that is used.
Note that the error event is avoided by design, since this has a special behaviour inNode.js
causing an exception to be thrown if it is unhandled.
Other error names are adjusted to be as close to possible as other networking related event names in Node.js and/or to the correspondingZeroMQ.js
method call. Events (including any errors) that correspond to a specific operation are namespaced with a colon :, e.g.bind:error
orconnect:retry
.
但是还是很警告,不是吗?
await
部分MCVE-code ( as-is )无法重现实时 session ,因此最好调整MCVE代码以使其可运行,我们可以继续进行此操作。
关于node.js - zeromq.js-等待连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61693959/