我已经创建了两个网站,使用socketio+nodejs( +2个开发站点为他们)。现在,我需要将这两个站点移动到一个服务器中。
为此,我使用了express-vhost,它适用于everything...except for socket.io。
在我的应用程序中,我已经使用了数百次以下函数。socket.emit(), io.sockets.emit() , socket.broadcast.emit()等等。当我创建这些网站的时候,我第一次使用了socketio,并且正在学习它。因此,我从未使用过名称空间(仍然没有使用它们)。
现在,当我在vhost下运行两个站点时,当有人连接到site1时,它们也连接到site2,因为它们使用相同的socket.io实例。
因此,我尝试创建多个socketio实例,每个域一个,如下所示
http = server.listen(80);//my vhost main app
global.io_for_domain_1 = require('socket.io')(http, {'transports': ['websocket', 'polling']} );
global.io_for_domain_2 = require('socket.io')(http, {'transports': ['websocket', 'polling']} );
/*And then, in my domain app, i was hoping to simply swap out the reference for io like so
...in my global socket controller that passes io reference to all other controllers...*/
this.io = global.io_for_domain_1 ;
//hoping that this would help me to avoid not having to re-write the hundreds of references that use io.当我为socketio创建第二个实例(服务器)时,work...But几乎觉得是这样,这些实例是在“断开连接”并停止工作之前创建的。
如何创建多个socketio服务器实例并让它们独立工作。或者,我如何使用名称空间来解决这个problem...perhaps (我仍然不知道如何使用它们)。
发布于 2016-02-12 12:57:51
很难回答,这取决于您的服务器代码,但这可能会帮助您找到解决方案.
一个唯一的socket.io实例(使用名称空间):
你必须找到最好的方法来实现它..。
var domain_a = io.of('/domain_a');
var domain_b = io.of('/domain_b');
domain_a.on('domainA_clientAction', function(socket){
domain_a.emit('domainA_clientAction');
});
domain_b.on('domainB_clientAction', function(socket){
domain_b.emit('domainB_serverResponse');
});编辑:如果您希望来自domain_a & domain_b的客户端在他们之间进行通信,则使用此选项
2独立node.js服务:
使用端口80上带有node-http-proxy的node.js作为其他node.js服务的路由器。
var http = require('http')
, httpProxy = require('http-proxy');
httpProxy.createServer({
hostnameOnly: true,
router: {
'domain-a.com': '127.0.0.1:3001',
'domain-b.com': '127.0.0.1:3002'
}
}).listen(80);..。或者你可以试试..。
NGINX https://www.nginx.com/blog/nginx-nodejs-websockets-socketio/
祝你的执行顺利。
https://stackoverflow.com/questions/35190846
复制相似问题