捕获两个套接字服务器的连接事件

时间:2014-02-03 02:15:52

标签: node.js sockets tcp sockjs

我知道我正在推翻这个...但答案是不点击。

我有两台服务器,一台是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实例时触发的事件。帮助慢跑大脑将非常感激!

1 个答案:

答案 0 :(得分:1)

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

尝试调用done(null,sock);就在你执行socket.createServer()之后;你应该全力以赴。

Doc:https://github.com/sockjs/sockjs-node