Get the client's IP address in socket.io

2019-01-04 07:10发布

When using socket.IO in a Node.js server, is there an easy way to get the IP address of an incoming connection? I know you can get it from a standard HTTP connection, but socket.io is a bit of a different beast.

17条回答
何必那么认真
2楼-- · 2019-01-04 08:12

Latest version works with:

console.log(socket.handshake.address);
查看更多
劳资没心,怎么记你
3楼-- · 2019-01-04 08:12

on socket.io 1.3.4 you have the following possibilities.

socket.handshake.address,

socket.conn.remoteAddress or

socket.request.client._peername.address

查看更多
做自己的国王
4楼-- · 2019-01-04 08:12

use socket.request.connection.remoteAddress

查看更多
成全新的幸福
5楼-- · 2019-01-04 08:13

In socket.io 2.0: you can use:

socket.conn.transport.socket._socket.remoteAddress

works with transports: ['websocket']

查看更多
虎瘦雄心在
6楼-- · 2019-01-04 08:14

Version 0.7.7 of Socket.IO now claims to return the client's IP address. I've had success with:

var socket = io.listen(server);
socket.on('connection', function (client) {
  var ip_address = client.connection.remoteAddress;
}
查看更多
登录 后发表回答