trinopoty / socket.io-server-java

Socket.IO Server Library for Java
Other
183 stars 51 forks source link

Getting client's ip address (Socket io v2) #21

Closed manikmagar closed 2 years ago

manikmagar commented 3 years ago

I am using 1.0.3 version of this library (with engine.io server 1.3.5) that supports Socket.io v2. I am looking to get the ip address of the connected client. V2 Server API documentation https://socket.io/docs/v2/server-api/#sockethandshake, suggests that socket.handshake.address should provide the ip of the client. But I can't see that in this library. Any pointers for this? Thanks!

{
  headers: /* the headers sent as part of the handshake */,
  time: /* the date of creation (as string) */,
  address: /* the ip of the client */,
  xdomain: /* whether the connection is cross-domain */,
  secure: /* whether the connection is secure */,
  issued: /* the date of creation (as unix timestamp) */,
  url: /* the request URL string */,
  query: /* the query object */
}
trinopoty commented 3 years ago

Getting the IP address of the client is a bit difficult right now as saving the connection object is not considered a good idea with servlets. You can get the headers and query string from the initial connection but not the client's ip currently.

You can place a reverse proxy and pass the client's ip in a header like X-Forwarded-IP. In this case, you can get the IP from the initial headers by calling the method getInitialHeaders

I'll take this as a feature request and try to add this information somewhere.

manikmagar commented 3 years ago

Thanks! getInitialHeaders seem to be available on v3 of this library but not on v2 (Maven version 1.x). By feature request, do you mean for backporting initial headers support to 1.x library?

trinopoty commented 3 years ago

v4 will be released which will be backward compatible with v1. You can update the library to that version if you don't want to update the frontend library version. Since v4 will be backward compatible with all previous versions, a backport will not be necessary.

manikmagar commented 3 years ago

Thats a good news! I can't upgrade frontend library version. Any thoughts on when that is planned to be released?

trinopoty commented 2 years ago

I've published version 4.0.0 to maven and it should be available in a few hours. This version is compatible with all versions of the socket.io-client package at the moment. There are also some restructure to the packages, particularly around the Emitter class that you'll need to patch in your source code. Please try out the new release and let me know if you run into any issues.