如何将socket.io附加到多个HTTPServer实例?

时间:2013-10-03 14:51:37

标签: javascript node.js socket.io

My Express应用程序侦听HTTP和HTTPS:

var app = express()
  , srv = http.createServer(app)
  , ssl = https.createServer({ ... }, app);

我可以让socket.io监听其中一个服务器,但不能同时监听两者:

var io = require('socket.io').listen(srv); // now what about `ssl`?

如何创建单个socket.io实例同时监听HTTP和HTTPS服务器?我想进行一次io.emit(...)调用,将消息发送到通过HTTP和HTTPS连接的客户端。

1 个答案:

答案 0 :(得分:0)

根据阅读源(未经测试),您可以尝试以下一件事:

https://github.com/LearnBoost/socket.io/blob/64f6b244b6bd79880ba2e0ba00778a2309b39d0b/lib/index.js#L127

var engine = require('engine.io');
var httpsEIO =  engine.attach(server.https, {path: '/socket.io'});
server.ws.bind(eio);

或在0.9-land中手动复制这些绑定:

https://github.com/LearnBoost/socket.io/blob/64d8f572aa9291889dd766e7b5832fc6f23b32a3/lib/manager.js#L103