调用下面方法即可获取
// koa2 中 req 为 ctx.req const getUserIp = (req) => { return req.headers['x-forwarded-for'] || req.connection.remoteAddress || req.socket.remoteAddress || req.connection.socket.remoteAddress; }
websocket 连接时获取
ws.on('connection', function(wsocket) { const ip = wsocket._socket.remoteAddress; })
转载于:https://www.cnblogs.com/YMaster/p/9908503.html