Reputation: 83
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.
Upvotes: 8
Views: 17499
Reputation: 1985
Usually the client will be some Javascript code in a web page delivered by your web application.
Have the client first fetch a servlet, that calls req.getRemoteAddr() and so on Servlet returns this data. Javascript then can send the data as @PathParam to the @ServerEndpoint.
Upvotes: 0
Reputation: 289
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
Upvotes: 4
Reputation: 1846
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?
Upvotes: 4