在 socket.io 中获取客户端的 IP 地址 [英] Get the client's IP address in socket.io

查看:78
本文介绍了在 socket.io 中获取客户端的 IP 地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Node.js 服务器中使用 socket.IO 时,是否有一种简单的方法可以获取传入连接的 IP 地址?我知道您可以通过标准 HTTP 连接获取它,但 socket.io 有点不同.

When using socket.IO in a Node.js server, is there an easy way to get the IP address of an incoming connection? I know you can get it from a standard HTTP connection, but socket.io is a bit of a different beast.

推荐答案

好的,从 0.7.7 开始,这是可用的,但不是以 lubar 描述的方式.我最终需要解析 git hub 上的一些提交日志来解决这个问题,但以下代码现在确实对我有用:

Okay, as of 0.7.7 this is available, but not in the manner that lubar describes. I ended up needing to parse through some commit logs on git hub to figure this one out, but the following code does actually work for me now:

var io = require('socket.io').listen(server);

io.sockets.on('connection', function (socket) {
  var address = socket.handshake.address;
  console.log('New connection from ' + address.address + ':' + address.port);
});

这篇关于在 socket.io 中获取客户端的 IP 地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆