When using socket.IO in a Node.js server, is there an easy way to get the IP address of an incoming connection? I know you can get it from a standard HTTP connection, but socket.io is a bit of a different beast.
相关问题
- npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fs
- IPAddress.[Try]Parse parses 192.168 to 192.0.0.168
- google-drive can't get push notifications
- How to reimport module with ES6 import
- Why is `node.js` dying when called from inside pyt
相关文章
- node连接远程oracle报错
- How can make folder with Firebase Cloud Functions
- @angular-cli install fails with deprecated request
- node.js modify file data stream?
- How to resolve hostname to an ip address in node j
- Transactionally writing files in Node.js
- Log to node console or debug during webpack build
- Get file created date in node
If you use an other server as reverse proxy all of the mentioned fields will contain localhost. The easiest workaround is to add headers for ip/port in the proxy server.
Example for nginx: add this after your
proxy_pass
:This will make the headers available in the socket.io node server:
Note that the header is internally converted to lower case.
If you are connecting the node server directly to the client,
works with socket.io version 1.4.6 for me.
for 1.0.4:
Using the latest
1.0.6
version ofSocket.IO
and have my app deployed onHeroku
, I get the clientIP
andport
using theheaders
into thesocket handshake
:Very easy. First put
You will see all fields of socket. then use CTRL+F and search the word address. Finally, when you find the field remoteAddress use dots to filter data. in my case it is
As of socket 3.1.2
I have found that within the socket.handshake.headers there is a forwarded for address which doesn't appear on my local machine. And I was able to get the remote address using:
This is in the server side and not client side.