Websocket - 每个连接的新实例?

时间:2014-05-09 21:30:37

标签: java websocket jetty

我按照这里的示例设置了websocket服务器。 https://www.eclipse.org/jetty/documentation/current/jetty-websocket-server-api.html

public class MySocketCreator implements WebSocketCreator {
    private MyEchoSocket mySocket;

    @Inject
    public MyAdvancedEchoCreator(MyEchoSocket mySocket) {
        this.mySocket = mySocket;
    }

    @Override
    public Object createWebSocket(ServletUpgradeRequest req, ServletUpgradeResponse resp) {
        return mySocket;
    }
}

我遇到的问题是:

  1. 我想知道MyEchoSocket是否是@Singleton?
  2. 由于MyEchoSocket看起来像这样:

     @WebSocket(maxTextMessageSize = 64 * 1024)
     public class MyEchoSocket {
    
        private final CountDownLatch closeLatch;
    
        private Set<Session> session;
    
        public MyEchoSocket() {
            sessionList = new ArrayList<>();
        }
    
       @OnWebSocketClose
        public void onClose(int statusCode, String reason) {
           ....
        }
    
        @OnWebSocketConnect
        public void onConnect(Session session) {
            sessionList.add(session);
        }
    
        @OnWebSocketMessage
        public void onMessage(String msg) {
            ....
        }
    }
    
  3. 我遇到的问题是,当连接多个不同的客户端,并且一个脱机时,我不知道哪个客户端脱机。 或者我做错了,每个新客户端我得到一个新的实例:MyEchoSocket 我用它来跟踪会话?

0 个答案:

没有答案