Search code examples
jakarta-eejava-websocket

Getting client IP ADDRESS when using ServerEndpoint


Means, that it will get connections from many users in local web. How can i get IP address each single connection? I use JSR356.

@ServerEndpoint(value = "/ws/example")
public class ExampleServlet {

     private static final AtomicInteger connectionIds = new AtomicInteger(0);
     private static final Set<ExampleServlet> connections = new CopyOnWriteArraySet<>();
     private Session session;

     @OnOpen
     public void start(Session session) {
         this.session = session;
         connections.add(this);
     }

     @OnClose
     public void end() {
     }

     @OnMessage
     public void incoming(String message) {
     }

     @OnError
     public void onError(Throwable t) throws Throwable {
     }
}

Solution

  • Unfortunately, JSR356 Websocket specification does not expose client IP address. Solution, how to hack this, i found there: JSR-356 WebSockets with Tomcat - How to limit connections within single IP address?