Native Coder
Native Coder

Reputation: 1870

Using 'ws' for Node.js, how do I can I get a clients information?

I've set up a simple WebSocket Server using ws for Node.js

var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer({ 
    port: 8080 
});

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

    conn.on('message', function incoming(message) {
        console.log('<--', message);
    });

    conn.send('something');
});


// Console will print the message
console.log('-=-=- Server running :8080 -=-=-');

I'm trying to figure out how ws keeps track of it's clients. For instance, how would I get the clients IP address? I've tried conn.address, conn.headers.host, and conn.IncomingMessage.headers.host (I used util.inspect on the conn object to see the structure. But there's ALOT of metadata)

Basically, I'm trying to console.log(conn.IpAdress) in the wss.on('connection'... event

Upvotes: 0

Views: 256

Answers (1)

Adiii
Adiii

Reputation: 59936

try like this

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

console.log('New websocket connection from'+con._socket.remoteAddress+"and"+ con._socket.remotePort);
}

Upvotes: 1

Related Questions