Ultimately, the goal here is to handle DNS round robin with socket.io, to coordinate different connections (socket.io and jQuery ajax) so they go to the same server when one name maps to multiple servers.
With this code, I am able to find the client-side IP address of an incoming websockets connection:
var app = express();
...
var server = http.createServer(app);
...
var io = socketio.listen(server);
io.on('connection', function(socket) {
var addr = socket.handshake.address;
logger.info("Just got incoming connection from client " + addr.address
+ ":" + addr.port);
But how can I find the server-side IP address of this socket? I would like to send the server-side IP address to the client (with socket.emit('serverSideIp': <server-IP>)
) so for diagnostic purposes we can track which specific IP address is in use. Also, when a socket.io connection fails and goes to a different IP, I want to redirect client-side AJAX requests to that IP so the client is not talking to different servers at the same itme.
EDIT: Get local IP address in node.js does not answer my question. Answered there is, "What is my list of IP addresses." My question is, "Given a specific socket, what is the local address (IP and port) of that specific socket" which cannot be answered on a multi-homed host by getting a list of addresses from the OS. In Java or C# it is trivial to get the local and remote port and IP address of a socket. How do you do this in node.js for a socket.io "socket" connection?