我目前在node.js服务器上使用socket.io v2.0.3,但找不到获取客户端IP地址的方法。
虽然stackoverflow上有很多提示和技巧,但它们已经过时并且不再起作用。
我目前在node.js服务器上使用socket.io v2.0.3,但找不到获取客户端IP地址的方法。
虽然stackoverflow上有很多提示和技巧,但它们已经过时并且不再起作用。
它位于 socket.handshake 中。
{
headers: /* the headers sent as part of the handshake */,
time: /* the date of creation (as string) */,
address: /* the ip of the client */,
xdomain: /* whether the connection is cross-domain */,
secure: /* whether the connection is secure */,
issued: /* the date of creation (as unix timestamp) */,
url: /* the request URL string */,
query: /* the query object */
}
目前存在一个未解决的问题:远程地址(IP)始终为未定义#2982
我相信很快就会解决,但在此之前,如果您确实需要获取客户端IP,则有一种可能的解决方案。
概念验证:
const socketServer = require('socket.io')(3000)
socketServer.on('connection',function(client){
console.log( client.request.connection._peername.address );
})
const socketCli = require('socket.io-client')('http://localhost:3000')
输出:
::ffff:127.0.0.1
socket.handshake.address
和socket.request.connection._peername.address
似乎都能正常工作,但是当您在客户端使用transports: ['websocket']
时,socket.hanshake.address
和socket.request.connection._peername
都会变成undefined
。希望这个问题能够尽快得到解决。 - mk12okclient.request.connection._peername
不存在。到目前为止,只有将服务器降级到 1.7.4 才有效。 - Darkersocket.conn.transport.socket._socket.remoteAddress
,该方法适用于transports: ['websocket']
。使用套接字连接在以下三个密钥上成功获取IP值,版本2.3.0
,使用transports: ['websocket']
。
socket.request.connection.remoteAddress ::ffff:127.0.0.1
socket.conn.remoteAddress ::ffff:127.0.0.1
socket.conn.transport.socket._socket.remoteAddress ::ffff:127.0.0.1
undefined
。 - Darker