2017-10-18 105 views
0

我在本地Mac機上的node.js上運行socket.io。本地主機和服務器之間的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 =另一個網絡上。

乾杯

史蒂夫Warby

回答

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解決的承諾,通過適當的錯誤進行連接或拒絕。

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); 
}) 
+0

修復了一個錯字,並增加了保護,防止在同一個套接字上發出多個錯誤(可能不需要,但是這樣做是安全的)。 – jfriend00

相關問題