简体   繁体   中英

socket.io how to get server socket listen address and port

here is my simplified code example on server side, i need to get to which ip (preferably hostname) and port the server socket is listening, given the socket argument on my callback and assuming i don't have accessible io variable here

function myCallback(socket, data){
    //how do i get, to which ip or hostname and port the socket argument is listening
}

io.on('connection', function(socket){
     socket.on('event',function(data){
          myCallback(socket,data);
     });
});

You can use:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM