Node.js

//传入请求HttpRequest
function getClientIp(req) {
        return req.headers['x-forwarded-for'] ||
        req.connection.remoteAddress ||
        req.socket.remoteAddress ||
        req.connection.socket.remoteAddress;
}

Express

//express框架则简单许多
req.ip

Colyseus

  • 使用 Express
//arena.config.ts 的 initializeExpress 函数中
app.use('/*',  (req, res) => {
            console.log("getting ip address...")
            var ip = req.ip;
            console.log(ip);
        });
  • 使用 onAuth 函数
//Room 类的 onAuth 函数
    onAuth(client: Client, options: any, request?: http.IncomingMessage): any {
        console.log("getting ip address...")
        var ip = request.headers['x-forwarded-for'] || request.connection.remoteAddress;
        console.log(ip);
        return true;
    }