0

在我的节点应用程序中,我需要使用 socket.io 获取用户的 IP 地址。我尝试了下面的代码,但直到我得到 127.0.0.1。我怎样才能获得我的 IP 地址。

1.

  var io = require("socket.io").listen(server);

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

2.

   var io = require('socket.io').listen(80);
     io.sockets.on('connection', function (socket) {
     var endpoint = socket.manager.handshaken[socket.id].address;
     console.log('Client connected from: ' + endpoint.address + ":" + endpoint.port);
  });

3.

   var socket = io.listen(server);
       socket.on('connection', function(client){
          var ip_address = client.connection.remoteAddress;
       });
4

1 回答 1

2

试试这个,这对我有用。

Var client = require('socket.io').listen(8080).sockets;

client.on('connection',function(socket){ 
var clientIpAddress= socket.request.socket.remoteAddress;
});
于 2014-06-03T02:25:18.890 回答