sitegui / nodejs-websocket

A node.js module for websocket server and client
MIT License
736 stars 155 forks source link

how to get client ip address with nodejs-websocket? #57

Open youaresherlock opened 5 years ago

youaresherlock commented 5 years ago

how can a server get the ip address from the client?

jdgregson commented 5 years ago

The remote address should be available via the conn.socket.remoteAddress property. For example:

const ws = require('nodejs-websocket');
const LISTEN_ADDRESS = '0.0.0.0';
const LISTEN_PORT = 8080;

let server = ws.createServer(function(conn) {
  let rAddr = conn.socket.remoteAddress;
  let rPort = conn.socket.remotePort;
  console.log(`New connection from ${rAddr}:${rPort}`);
}).listen(LISTEN_PORT, LISTEN_ADDRESS);