1

The question is in the title.

I've been roaming the web for what seems like an eternity to find out how to get the client's IP after a connection.

No amount of ws.request, ws._socket, ws.adress() or ws.everyfreakingthingthereisoutthere gave me any result.The server's IP is 192.168.0.23 , the client's is 192.168.0.15 .

The connection works well (I can send, receive, connect and close connections just fine). in it.

The server's IP is 192.168.0.23 , the client's is 192.168.0.15 . edit: Also tried with in localhost and with another external machine on a different router (with external IP adress)

wss.on('connection', function(ws) {

    console.log(ws.what?);

    ws.on('message', function(message) {
        console.log('received : %s', message);
    });

    ws.on('close', function(){
    });

    ws.send('HELLO');
});

I'd be very thankful for any help I can get!

EDIT : I effectively continued looking for a solution without any result. No documentation is available online, not sure why, but getting the IP adress of the client should be an easy enough thing to do.

I'm really lost here, I've put many hours into this problem and can't seem to fix it. Everybody online seem to have different solutions working, and other not working, I'm not sure if it depends on the version, but on the latest version none of them seem to work.

Again, I'd be very grateful for any tip that would allow me to take a step forward with this issue, I'm trying my best.

EDIT : Here is a log corresponding on console.log(ws) on connection to help with the search.

Cafe
  • 85
  • 2
  • 8
  • 2
    possible duplicate of [socket.io: get client's IP address](http://stackoverflow.com/questions/6458083/socket-io-get-clients-ip-address) – thgaskell Oct 10 '13 at 23:29
  • Not a single solution suggested in this thread works for me. I've got the last stable versions of both node and socket.io . – Cafe Oct 10 '13 at 23:51

2 Answers2

1

You may be using the wrong object for your .on() handler:

var wss = require('http').createServer(handler).listen(80);
var io = require('socket.io').listen(wss);
wss.on('connection', function(ws) { ...  // OOPS, http.server.on
io.on('connection', function(ws) { ...  // CORRECT, socket.io.on

I'm guessing this is the case because your console.log(ws) object looks like an instance of node's builtin net.Socket class, not of socket.io's socket class.

Sean Gugler
  • 779
  • 8
  • 18
-1

Try this:

 wss.sockets.on("connection", function (ws) { var address = ws.handshake.address; console.log("Ip from " + address.address + ":" + address.port); }
Vicky Gonsalves
  • 11,593
  • 2
  • 37
  • 58
  • 1
    As mentioned, this solution and all other suggested in the other thread doesn't work. ws.handshake is undefined (ws.handshake.anything throws an error). – Cafe Oct 11 '13 at 12:10
  • 1
    How does it mean I'm not able to connect if I can successfuly do everything I should with sockets? (receive,send,connect, disconnect). – Cafe Oct 11 '13 at 14:06