Can I get remote IP address inside @OnOpen
method of @ServletEndpoint
class?
I tried follow this Accessing HttpSession from HttpServletRequest in a Web Socket @SocketEndpoint but as in Websocket - httpSession returns null it doesn't work.
Anyway I only need clients IP address.
The JSR-356 Websocket specification does not expose client IP address.
You may try one of the 2 hacks descibed in this response: JSR-356 WebSockets with Tomcat - How to limit connections within single IP address?
Added: Works for Jetty implementation and it's not a common solution. Thanks to Benjamin !
Don't know if you still need it. I myself directly get the IP from the socket Session.
@OnOpen
public void onWebSocketConnect(Session session) {
System.Out.println(session.getUserProperties().get("javax.websocket.endpoint.remoteAddress"))
}
Then I get /127.0.0.1:57045
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With