How do I access instantiated WebSockets in Jetty 9?

前端 未结 1 1300
遥遥无期
遥遥无期 2020-11-29 06:15

This is probably obvious, but I am new to this paradigm. I create a Jetty Server and register my websocket class as follows:

        Server server = new Ser         


        
相关标签:
1条回答
  • 2020-11-29 06:32

    Two common techniques, presented here in a super simplified chatroom concept.

    Option #1: Have WebSocket report back its state to a central location

    @WebSocket
    public class ChatSocket {
        public Session session;
    
        @OnWebSocketConnect
        public void onConnect(Session session) {
            this.session = session;
            ChatRoom.getInstance().join(this);
        }
    
        @OnWebSocketMessage
        public void onText(String message) {
            ChatRoom.getInstance().writeAllMembers("Hello all");
        }
    
        @OnWebSocketClose
        public void onClose(int statusCode, String reason) {
            ChatRoom.getInstance().part(this);
        }
    }
    
    public class ChatRoom {
        private static final ChatRoom INSTANCE = new ChatRoom();
    
        public static ChatRoom getInstance()
        {
            return INSTANCE;
        }
    
        private List<ChatSocket> members = new ArrayList<>();
    
        public void join(ChatSocket socket) 
        {
            members.add(socket);
        }
    
        public void part(ChatSocket socket) 
        {
            members.remove(socket);
        }
    
        public void writeAllMembers(String message) 
        {
            for(ChatSocket member: members)
            {
                member.session.getRemote().sendStringByFuture(message);
            }
        }
    
        public void writeSpecificMember(String memberName, String message) 
        {
            ChatSocket member = findMemberByName(memberName);
            member.session.getRemote().sendStringByFuture(message);
        }
    
        public ChatSocket findMemberByName(String memberName) 
        {
            // left as exercise to reader
        }
    }
    

    Then simply use the central location to talk to the websockets of your choice.

    ChatRoom.getInstance().writeSpecificMember("alex", "Hello");
    
    // or
    
    ChatRoom.getInstance().writeAllMembers("Hello all");
    

    Option #2: Have WebSocket be created manually with WebSocketCreator

    @WebSocket
    public class ChatSocket {
        public ChatRoom chatroom;
    
        public ChatSocket(ChatRoom chatroom)
        {
            this.chatroom = chatroom;
        }
    
        @OnWebSocketConnect
        public void onConnect(Session session) {
            chatroom.join(this);
        }
    
        @OnWebSocketMessage
        public void onText(String message) {
            chatroom.writeAllMembers(message);
        }
    
        @OnWebSocketClose
        public void onClose(int statusCode, String reason) {
            chatroom.part(this);
        }
    }
    
    public class ChatCreator implements WebSocketCreator
    {
        private ChatRoom chatroom;
    
        public ChatCreator(ChatRoom chatroom)
        {
            this.chatroom = chatroom;
        }
    
        public Object createWebSocket(UpgradeRequest request, 
                                      UpgradeResponse response)
        {
            // We want to create the Chat Socket and associate
            // it with our chatroom implementation
            return new ChatSocket(chatroom);
        }
    }
    
    public class ChatHandler extends WebSocketHandler
    {
        private ChatRoom chatroom = new ChatRoom();
    
        @Override
        public void configure(WebSocketServletFactory factory)
        {
            factory.setCreator(new ChatCreator(chatroom));
        }
    }
    

    At this point you can use the same techniques as above to talk to the websockets of your choice.

    0 讨论(0)
提交回复
热议问题