在我的节点应用程序中,我需要使用 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;
});