zeromq.js - 等待连接

zeromq.js - Await connection

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() 方法可能会引发哪些错误?我提供了一个 'obscene' 端口号,例如 8124000 来连接。我希望提出一些错误消息。

Q : "what error(s) maybe raised by the connect() method?"


错误部分

ZeroMQ 本机 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 重新包装这些主要状态,因此最好的下一步是重新阅读包装器源代码,以便了解这些原生 API 错误状态是如何在 zeromq.js-wrapper.

中实际处理的

备注:

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 in Node.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 corresponding ZeroMQ.js method call. Events (including any errors) that correspond to a specific operation are namespaced with a colon :, e.g. bind:error or connect:retry.

不过还是很警示,不是吗?


await部分

MCVE-code ( as-is ) 无法重现实时会话,因此最好调整 MCVE 代码以获得 运行-able,我们可以继续进行此操作。