1. 程式人生 > >nodejs socket.io獲取使用者ip細節

nodejs socket.io獲取使用者ip細節

是不斷除錯過程中發現的

io.sockets.on('connection', function(socket) {
    socket.on('postMsg', function(customerName,msg) {
  //  var clientIp=socket.handshake.headers['x-forwarded-for'] || socket.handshake.address.address;
    console.log(socket.handshake.address);
  var contentId =MathRand();
 socket.broadcast.emit('newMsg', customerName ,msg,contentId);
 console.log(contentId);
    });

通過socket.handshake獲得了使用者請求資訊

.address就可以了