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

查看:162
本文介绍了在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天全站免登陆