在 socket.io 服务器之间切换

Switch between socket.io servers

本文关键字:之间 服务器 socket io      更新时间:2023-09-26

有人可以向我解释如何让 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.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();
});