localhost&之间的socket.io切换服务器

时间:2017-10-18 00:28:36

标签: socket.io

我正在本地Mac机器上的node.js上运行socket.io。

我有设置端口转发

我使用以下代码:

socket = io.connect('XXX.XXX.137.143:1337');

socket.on('connect_failed', function(){
$.jGrowl('Connection Failed');

});
socket.on('disconnect', function(){
$.jGrowl('disconnected');

});
    socket.on('connect', function () { 
   $.jGrowl('We are connected ' );

});

IP地址是我的路由器IP地址。这一切都按预期工作。

但是,如果我在与路由器相同的网络上运行应用程序,则无法连接。它需要:

socket = io.connect('localhost'); // or the ip of the machine.

如何确定我与本机位于同一网络并切换IP设置?

我试过了:

if (location.hostname === "localhost" || location.hostname ===   "192.168.1.1")
{$.jGrowl("It's a local client!");
var IPaddress = 'http://192.168.1.6:1337';}
else {var IPaddress = '92.12.137.143:1337';
$.jGrowl("It's a remote client!")
}

这在本地工作但在另一个IP = 192.168.1.1的网络上失败。

干杯

Steve Warby

1 个答案:

答案 0 :(得分:1)

您可以首先尝试连接到公共IP地址,如果失败,则尝试在本地连接。这是一种方法:

let socket = io.connect('http://92.12.137.143:1337');
socket.on('connect_error', handleNoConnect);
socket.on('connect_timeout', handleNoConnect);
socket.on('connect', onConnect);

function handleNoConnect() {
    console.log("No connection to http://92.12.137.143:1337");
    socket = io.connect('http://192.168.1.6:1337');
    socket.on('connect_error', handleNoConnect2);
    socket.on('connect_timeout', handleNoConnect2);
    socket.on('connect', onConnect);
}

function handleNoConnect2() {
    console.log("No connection to http://192.168.1.6:1337");
    // decide what to do when you can't connect to either
}

function onConnect() {
    console.log("connected");

    // set other event handlers on a connected socket
    socket.on('disconnect', function() {
        console.log("disconnected");
    });
}

或者,这里是一个更通用的实现,它允许您传入一系列URL以尝试连接,并返回一个承诺,该承诺将通过socket解决,该{1}}建立连接或拒绝适当的错误。

function connect(list) {
    let index = 0;
    return new Promise((resolve, reject) => {
        function next() {
            if (index < list.length) {
                let url = list[index++];
                let doneThis = false;
                let socket = io.connect(url);
                socket.on('connect_error', handleNoConnect);
                socket.on('connect_timeout', handleNoConnect);
                socket.on('connect', () => {
                    // set property on socket object that tells you 
                    //   which URL it connected to
                    socket._url = url;
                    resolve(socket);
                });

                function handleNoConnect() {
                    if (!doneThis) {
                        doneThis = true;
                        console.log("No connection to " + url);
                        // try next item in the list
                        next();
                    }
                }

            } else {
                reject(new Error("No connections succeeded"));
            }
        }
        if (!list || !list.length) {
            reject(new Error("Must pass array of URLs to connect()"));
            return;
        }
        next();
    });
}

connect(['http://92.12.137.143:1337', 'http://192.168.1.6:1337']).then(socket => {
    // connected here
    console.log("connected to " + socket._url);

    // configure event handlers on the connected socket object here
    socket.on('xxx', ...);
}).catch(err => {
    // all connections failed here
    console.log(err);
})