在我的节点应用程序中,我需要使用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;
});
答案 0 :(得分:2)
试试这个,这对我有用。
Var client = require('socket.io').listen(8080).sockets;
client.on('connection',function(socket){
var clientIpAddress= socket.request.socket.remoteAddress;
});