【问题标题】:Getting client IP ADDRESS when using ServerEndpoint使用 ServerEndpoint 时获取客户端 IP 地址
【发布时间】:2016-06-01 20:41:20
【问题描述】:

意味着它将获得本地网络中许多用户的连接。如何获取每个连接的 IP 地址?我使用 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 {
     }
}

【问题讨论】:

    标签: jakarta-ee java-websocket


    【解决方案1】:

    不幸的是,JSR356 Websocket 规范没有公开客户端 IP 地址。解决方案,如何破解这个,我在那里找到:JSR-356 WebSockets with Tomcat - How to limit connections within single IP address?

    【讨论】:

      【解决方案2】:

      这是用于带有弹簧的 websocket @覆盖 public void afterConnectionEstablished(WebSocketSession session) 抛出异常 { System.out.println("连接建立!"+session.getLocalAddress());
      }

      【讨论】:

      • 它不是关于 jsf ,spring。你可以从 session 中得到它。
      • OP 使用的是标准 Java EE,而不是第 3 方 Spring 库。你根本没有回答具体的问题。如果您想回答 Spring 问题,请查找 Spring questions。
      猜你喜欢
      • 2019-01-14
      • 1970-01-01
      • 2013-07-07
      • 2011-05-26
      • 2019-11-13
      • 1970-01-01
      • 2011-01-08
      • 2014-12-29
      • 2015-05-05
      相关资源
      最近更新 更多