nodejs开发中,socket.io如何获取客户端真实IP地址_

0分享至

用扫码二维码

分享至好友和朋友圈

一般情况下,socket.handshake.address就是客户端ip,但如果使用了CDN加速等情况下,会导致socket.handshake.address拿到值都是服务器的,比如很可能是:127.0.0.1。这时,如何获取客户端的真实IP呢?

代码如下:

if(socket.handshake.headers[x-forwarded-for] != null){

ip = socket.handshake.headers[x-forwarded-for];

}else{

ip = socket.handshake.address;

}

也可以写成个函数,如:

特别声明:以上内容(如有图片或视频亦包括在内)为自媒体平台“网易号”用户上传并发布,本平台仅提供信息存储服务。

Notice: The content above (including the pictures and videos if any) is uploaded and posted by a user of NetEase Hao, which is a social media platform and only provides information storage services.

/阅读下一篇/

返回网易首页 下载网易新闻客户端