有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java getOpenSessions()提供空[]值

我试图在Set allSession中保存会话,但是getOpenSessions()方法创建了一个空数组。我的代码看起来像-

@OnOpen
public void onOpen(Session session){
    System.out.println(session.getId() + " has opened a connection");
    System.out.println(" Session: "+session.getOpenSessions());
    try {
        session.getBasicRemote().sendText("Connection Established");
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

所以,这给了我一个结果-

1已打开连接

会话[]

有人能提出确切的问题吗


共 (2) 个答案

  1. # 1 楼答案

    成功了

        @OnMessage
        public String echo(String msg) throws IOException {
       
            Set<Session> set1 =  wsSession.getOpenSessions();
            
            System.out.println("set:  "+set1);
            
             Iterator<Session> iter = set1.iterator();
             
             while(iter.hasNext()) {
                 
                 
                   System.out.println("entered inside while");
                   
             }
        }
    
  2. # 2 楼答案

    我也有同样的问题。。。但是当我在@onMessage注释方法中使用该方法时。。它很好用。。。布景中展示了