Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
286 views
in Technique[技术] by (71.8m points)

node.js 的socket.io如何获取客户端的ip来标记客户socket

想在server 端获得客户ip实现断线续连,发送私信的功能,问题是如何获取客户端的真实ip来标记socket?

var ChatterList=[];
io.sockets.on("connection", function(socket){
var newChatter={};
    newChatter.Soc=socket;//加入socket属性
   newChatter.IP=?; //如何获得客户端socket的ip?

   ChatterList.push(newChatter);//socket加入聊天列表


}
//给特定ip的socket发消息
ChatterList.forEach(function(){
    if(newChatter.IP===192.168.1.10){
        newChatter.Avail=false;
        newChatter.Soc.emit('test',"this is a test");
    };



)
      

主要问题是: newChatter.IP=?; //如何获得客户端socket的ip?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

笔误吧?

ChatterList[0].Id   //  <--- 好歹给数组一个索引。

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...