Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the hostname of a socket?

When I receive a certain event from a connected socket, I have to send a request with as parameter my hostname and port. I was hoping to be able to retrieve this information from the socket object. Unfortunately, there is little documentation on this and I can't seem to be able to find out if and how this is possible.

So, is it possible to do something like this in Socket.io:

io.sockets.on('connection', function(socket){
    console.log(socket.manager.server.hostname)
})'

(Or, alternatively: which thinking error am I making here in thinking that this should be possible in the first case?)

like image 966
Vincent Avatar asked Jan 13 '23 22:01

Vincent


1 Answers

try this:

console.log(socket.handshake.headers.host);

split port if necessary:

console.log(socket.handshake.headers.host.split(":").shift());
like image 66
dknaus Avatar answered Jan 21 '23 01:01

dknaus