获取WebSocket连接的连接IP

Get connection IP of WebSocket connection

像这样打开网络套接字后:

m_ws = new WebSocket(127.0.0.1);

是否可以获取刚刚打开的IP?类似于 m_ws._websocket.address?

我看过很多示例,但 none 我认为它们适用于 WebSocketServer 实例。

你用ws library吗?

如果您使用 ws 获取 IP 连接:

const WebSocket = require('ws');

const wss = new WebSocket.Server({ port: 8080 });

wss.on('connection', function connection(ws, req) {
  const ip = req.connection.remoteAddress;
});