zoukankan      html  css  js  c++  java
  • websocket

     长连接推送:
     
     
     
    MAP:key-userid value-session
     
    import javax.websocket.OnClose;
    import javax.websocket.OnMessage;
    import javax.websocket.OnOpen;
    import javax.websocket.Session;
    import javax.websocket.server.ServerEndpoint;
    import java.io.IOException;
    
    @ServerEndpoint("/websocketTest")
    public class WebSocketTest {
        @OnMessage
        public void onMessage(String message, Session session) throws IOException, InterruptedException {
    
            // Print the client message for testing purposes
            System.out.println("Received: " + message);
    
            // Send the first message to the client
            session.getBasicRemote().sendText("This is the first server message");
    
            // Send 3 messages to the client every 5 seconds
            int sentMessages = 0;
            while (sentMessages < 3) {
                Thread.sleep(5000);
                session.getBasicRemote().sendText("This is an intermediate server message. Count: " + sentMessages);
                sentMessages++;
            }
    
            // Send a final message to the client
            session.getBasicRemote().sendText("This is the last server message");
        }
    
        @OnOpen
        public void onOpen() {
            System.out.println("Client connected");
        }
    
        @OnClose
        public void onClose() {
            System.out.println("Connection closed");
        }
    }
    

      

  • 相关阅读:
    sql语句相关操作
    点菜系统数据库课程设计
    JDBC连接mysql编程
    JFrame画图基础和事件监听
    JFrame编程
    Java基础知识
    bzoj1047-理想的正方形(二维单调队列)
    Project Eular 233/ BZOJ 1041
    Open Train 10352
    Codeforces Round 492 (Div.1)
  • 原文地址:https://www.cnblogs.com/lnas01/p/6544262.html
Copyright © 2011-2022 走看看