Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Websocket getting remote IP on connect

Tags:

java

websocket

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.

like image 719
ZdeenaP Avatar asked Sep 18 '14 13:09

ZdeenaP


2 Answers

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?

like image 72
Benjamin Avatar answered Oct 14 '22 02:10

Benjamin


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

like image 27
Aere Xu Avatar answered Oct 14 '22 03:10

Aere Xu