Javascript 在socket.io服务器之间切换
有人能告诉我如何让socket.io客户端连接到服务器a->断开连接,然后连接到服务器B吗 干杯 编辑:这是我遇到问题的代码Javascript 在socket.io服务器之间切换,javascript,node.js,socket.io,Javascript,Node.js,Socket.io,有人能告诉我如何让socket.io客户端连接到服务器a->断开连接,然后连接到服务器B吗 干杯 编辑:这是我遇到问题的代码 login.on('connect', function (data) { console.log('Connected to login server'); }); login.on('disconnect', function(){ console.log('Disconnected from login server'); }); login.
login.on('connect', function (data) {
console.log('Connected to login server');
});
login.on('disconnect', function(){
console.log('Disconnected from login server');
});
login.on('login', function (data) {
// set the connection details for the storage server
storage = io.connect('http://'+data.node);
// Disconnect from the login server
login.disconnect();
});
storage.on('connect', function (data) {
console.log('Connected to storage server');
});
连接上的存储从不触发。。为什么?请尝试以下代码:
login.on('connect', function (data) {
console.log('Connected to login server');
});
login.on('disconnect', function(){
console.log('Disconnected from login server');
});
login.on('login', function (data) {
// set the connection details for the storage server
storage = io.connect('http://'+data.node);
storage.on('connect', function (data) {
console.log('Connected to storage server');
});
// Disconnect from the login server
login.disconnect();
});
var socket=io'serverA'。。。套接字=io'serverB'@Mohammadhahchamani说服务器A返回一个地址,客户端需要在其中为服务器B连接。如何正确地执行此操作?将地址存储在变量中,然后使用该变量连接新的套接字。@Mohammadhahchamani我会再次尝试,并用一些代码与您联系。@Mohammadhahchamani我已经用代码更新了我的第一篇帖子。因此,对于每一个需要在登录中定义存储上的实例。登录时?必须有一个更干净的方法来做到这一点?尝试在函数外定义存储,只需在login.on login函数中连接它。我将存储定义为var存储;在登录功能之外定义,但似乎仍不起作用。是否检查了url?它真的连接到服务器吗?是的。服务器B控制台在.connected上记录传入连接