Reputation: 14375
In my node application i need to get the ip address of the user using socket.io.I have tried below code but till i'm getting 127.0.0.1.How can i get my ip address.
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;
});
Upvotes: 0
Views: 5112
Reputation: 546
Try this, this works for me.
Var client = require('socket.io').listen(8080).sockets;
client.on('connection',function(socket){
var clientIpAddress= socket.request.socket.remoteAddress;
});
Upvotes: 2