node.js - zeromq.js-等待连接

标签 node.js sockets tcp zeromq publish-subscribe

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 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 )无法重现实时 session ,因此最好调整MCVE代码以使其可运行,我们可以继续进行此操作。

关于node.js - zeromq.js-等待连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61693959/

相关文章:

linux - 无法从 unix 域套接字获取可用字节

android - 访问 NATed HTTP 服务器

c++ - 打开带有零填充 IP 字符串的 boost 套接字

node.js - Stripe Node JS 在多个帐户中创建客户

javascript - 多变量 socket.io 发出的问题

java - 点对点与服务器客户端的实时游戏

Android客户端与Java服务器TCP通信

node.js - 在 Strongloop 环回中重写关系远程方法

随着流量增加,Node.js EMFILE 错误

javascript - 如何使用 mysql 制作 Electron 登录系统还是应该做其他事情?