2016-07-16 118 views
0

您好我有socket.io版本1.3.7中獲取客戶端IP地址之前更新其完美的工作,但更新後我得到每次未定義這是我的代碼是我更新如何在socket.io中獲取客戶端IP地址

之前使用,這是我的服務器端

sockets.on('connection',function(sock){ 

var address = sock.handshake.address; 

這個代碼從客戶端更新後,發送IP地址

var remote = 'http://my_ip_address:9008/'; 
    socket = io.connect(!!local ? local : remote); 
    socket.on("connect", function() { 
    console.log("connected"); 
      getData(); 
    }); 

它不是在socket.io版本1.3.7使工作p租賃幫助我解決這個問題,並建議完美的代碼爲1.3.7版本

+0

'sock.handshake.address '應該是客戶端的IP地址。它適用於socket.io 1.4.5。它可能會以IPv6格式顯示,具體取決於您的系統配置。 – jfriend00

+0

身份證有新的版本的socket.io客戶端連接的任何變化,我的客戶端連接套接字的代碼是完美的或不,如果你有任何想法,所以請告訴我,我不能得到IP地址 –

+0

從服務器,這裏是你如何檢索各種IP地址的東西:http://stackoverflow.com/questions/38423930/how-to-retrieve-client-and-server-ip-address-and-port-number-in-node-js/ 38426473?noredirect = 1#comment64286103_38426473 – jfriend00

回答

1

我沒有太多的插座,但我想這會幫助你。

您需要使用address.address和端口address.port

sockets.on('connection',function(sock){ 
var address = sock.handshake.address; 
     console.log('New connection from ' + address.address + ':' + address.port); 
}); 

而對於Socket.io版本1.0 服務器端:

sockets.on('connection',function(sock){ 
    var clientIpAddress = sock.request.headers['x-forwarded-for'] || sock.request.connection.remoteAddress; 
    console.log(' new request from : '+clientIpAddress); 
}); 
+0

非常感謝 - 花費太多時間搜索服務器端解決方案 – user1274820

相關問題