使用uWebSockets获取IP地址

时间:2017-06-12 14:22:20

标签: websocket

请问,如何使用uWebSockets获取发件人IP地址?

const wss = new uws.Server({
  server: server,
  perMessageDeflate: false
});

wss.on(`connection`, ws => {
  ws.remoteAddress = ws.upgradeReq.connection.remoteAddress;
  console.log(`user connected: `, ws.remoteAddress);
});

用户已连接undefined

2 个答案:

答案 0 :(得分:0)

请试试这个:

wss.on(`connection`, ws => {
ws.remoteAddress = ws._socket.remoteAddress;
   console.log(`user connected: `, ws.remoteAddress);
});

您还可以记录ws._socket以了解它是什么。

答案 1 :(得分:0)

ws._socket.remoteAddress应该可以工作,但是如果您的服务器在负载均衡器后面,则可以通过以下方式获取它:

ws.upgradeReq.headers["x-forwarded-for"]