【问题标题】:Capture connection events of two socket servers捕获两个套接字服务器的连接事件
【发布时间】:2014-02-26 10:23:16
【问题描述】:

我知道我想多了……但答案就是没有点击。

我有两台服务器,一台是 TCP 套接字服务器,另一台是 SockJS 服务器。我需要将它们的两个连接事件合并为一个超级事件:

async.parallel({
  tcp: function (done) {
    self._tcp = net.createServer(function (sock) {
      done(null, sock);
    });
  },
  ws: function (done) {
    self._ws = sockjs.createServer(function (sock) {
      done(null, sock);
    });
  }
}, function (err, results) {
   // This never gets fired!!!
   // But I'd like to do stuff here with both 
   // socket instances – you know, like piping =)
});

最初我将 TCP 连接嵌套在 WS 连接中,但事实证明这是有问题的,因为它需要严格的连接顺序。我真正需要的是当两个连接都建立并可以访问它们各自的sock 实例时触发的事件。非常感谢帮助慢跑大脑!

【问题讨论】:

    标签: node.js sockets tcp sockjs


    【解决方案1】:

    这可能过于简单 - 但查看 sockjs 文档,createServer() 没有回调函数 - 所以它永远不会循环回并行函数的回调。

    尝试调用 done(null, sock);就在你做 socket.createServer();你应该准备好了。

    文档:https://github.com/sockjs/sockjs-node

    【讨论】:

    • 非常感谢 - 我知道那是愚蠢的事情!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-27
    • 1970-01-01
    • 1970-01-01
    • 2019-12-10
    • 2011-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多