简体   繁体   English

获取WebSocket连接的连接IP

[英]Get connection IP of WebSocket connection

Once I've opened a web socket like so: 一旦我打开了这样的网络套接字:

m_ws = new WebSocket(127.0.0.1);

Is it possible to get the IP that was just used to open? 是否有可能获得刚刚用于打开的IP? Something like m_ws._websocket.address ? m_ws._websocket.address这样的m_ws._websocket.address

I've seen numerous examples but none of them worked as I think they apply to WebSocketServer instances. 我已经看过很多例子,但是没有一个例子,因为我认为它们适用于WebSocketServer实例。

Do you use the ws library ? 你使用ws吗?

If you use ws to get IP Connection : 如果您使用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;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM