node.js socket.io 客户端可以连接到两个不同的服务器/端口吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6843584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can socket.io client connect to two different servers/ports?
提问by Kriem
Can socket.io client connect to two different ports on the same server?
socket.io 客户端可以连接到同一服务器上的两个不同端口吗?
Can socket.io client connect to two different server?
socket.io 客户端可以连接到两个不同的服务器吗?
采纳答案by Alfred
Can socket.io client connect to two different ports on the same server?
socket.io 客户端可以连接到同一服务器上的两个不同端口吗?
I assume both machines share same domain. I believe it can use long-polling(websockets, flashsockets, etc also work), even passing along cookie. But I still need to test this on Internet Explorer because that browser never does what I want...
我假设两台机器共享同一个域。我相信它可以使用长轮询(websockets、flashsockets 等也可以使用),甚至可以传递 cookie。但是我仍然需要在 Internet Explorer 上进行测试,因为该浏览器永远不会做我想要的......
Can socket.io client connect to two different server?
socket.io 客户端可以连接到两个不同的服务器吗?
The big question is if those both machines are on different domains. If on same domain it will work just fine even passing along cookie(s). If they are on different domains then on some browser we fall-back to json-p transport(worst transport imaginable), but it will work. Unfortunately then the cookie is not passed along, because of same origin policy. Right now I am toying to get around this cookie restriction(hard problem)...
最大的问题是这两台机器是否在不同的域中。如果在同一个域上,即使传递 cookie 也能正常工作。如果它们在不同的域上,那么在某些浏览器上,我们回退到 json-p 传输(可以想象的最糟糕的传输),但它会起作用。不幸的是,由于同源策略,cookie 没有传递。现在我正在玩弄这个 cookie 限制(难题)...
回答by Kriem
Sure:
当然:
var io1 = require('socket.io').listen(8001);
io1.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
});
var io2 = require('socket.io').listen(8002);
io2.sockets.on('connection', function (socket) {
socket.emit('flash', { hello: 'world' });
});
Perhaps this is an interesting read: (from github)
也许这是一个有趣的阅读:(来自github)
// connect at the same host / port as your website
var socket = io.connect();
// different port or host
var socket = io.connect('http://url.com');
// secure
var socket = io.connect('https://localhost');

