webSocket模板

@Component
@ServerEndpoint(value = "/webSocket/time/out/{doctorCode}")
public class TimeOutWebSocketServer {

    /**静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。*/
    private static int onlineCount = 0;

    /**concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。*/
    private static ConcurrentHashMap<String, TimeOutWebSocketServer> webSocketMap = new ConcurrentHashMap<>();

    /**与某个客户端的连接会话,需要通过它来给客户端发送数据 phone-Session*/
    private static ConcurrentHashMap<String, Session> sessionDoctorMap = new ConcurrentHashMap<>();

    /**
     *功能描述 连接时执行
     * @author zhaozm
     * @date 2022/11/9
     * @param session:
     * @return void
     */
    @OnOpen
    public void onOpen(@PathParam(value = "doctorCode") String doctorCode, Session session){
        log.info("打开websocket连接");
        if(sessionDoctorMap.containsKey(doctorCode)){
            sessionDoctorMap.remove(doctorCode);
            sessionDoctorMap.put(doctorCode, session);
        } else {
            sessionDoctorMap.put(doctorCode, session);
        }

        if (webSocketMap.containsKey(doctorCode)){
            webSocketMap.remove(doctorCode);
            webSocketMap.put(doctorCode, this);
        } else {
            webSocketMap.put(doctorCode, this);
            //在线数加1
            addOnlineCount();
        }

        log.info("用户连接:" + session.getId() + ",当前在线人数为:" + getOnlineCount());
    }

    /**
     *功能描述 收到消息时执行
     * @author zhaozm
     * @date 2022/11/9
     * @param message:
     * @return void
     */
    @OnMessage
    public void onMessage(@PathParam(value = "doctorCode") String doctorCode, String message) {
        try {
            log.info("收到{}的消息:{}", doctorCode, message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     *功能描述 关闭时执行
     * @author zhaozm
     * @date 2022/11/9
     * @param
     * @return void
     */
    @OnClose
    public void onClose(@PathParam(value = "doctorCode") String doctorCode){
        //医生端
        if (sessionDoctorMap.containsKey(doctorCode)){
            sessionDoctorMap.remove(doctorCode);
        }
        if(webSocketMap.containsKey(doctorCode)){
            webSocketMap.remove(doctorCode);
            //从set中删除
            subOnlineCount();
        }
        log.info("用户退出:" + doctorCode + ",当前在线人数为:" + getOnlineCount());
    }

    /**
     *功能描述 连接错误时执行
     * @author zhaozm
     * @date 2022/11/9
     * @param doctorCode:
     * @param error:
     * @return void
     */
    @OnError
    public void onError(@PathParam(value = "doctorCode") String doctorCode, Throwable error){
        log.error("用户错误:{},原因-Exception:{}", doctorCode, error.getMessage());
        //医生端
        if (sessionDoctorMap.containsKey(doctorCode)){
            sessionDoctorMap.remove(doctorCode);
        }
        if(webSocketMap.containsKey(doctorCode)){
            webSocketMap.remove(doctorCode);
            //从set中删除
            subOnlineCount();
        }
    }

    /**
     * 实现服务器主动推送
     */
    public void sendMessage(Session session,String message) throws IOException {
        if (session != null){
            synchronized (this){
                session.getBasicRemote().sendText(message);
            }
        } else {
            log.error("发送session不能为空");
        }
    }

    /**
     * 功能描述 发送消息
     * @author zhaozm
     * @date 2022/11/9
     * @param doctorCode:
     * @param message:
     * @return void
     */
    public static void sendSelfMessage(String doctorCode, String message){
        if (StringUtils.isEmpty(message) || StringUtils.isEmpty(doctorCode)){
            return;
        }
        System.out.println("doctorCode:"+doctorCode);
        System.out.println("message:"+message);
        try {
            if (webSocketMap.containsKey(doctorCode) && sessionDoctorMap.containsKey(doctorCode)){
                webSocketMap.get(doctorCode).sendMessage(sessionDoctorMap.get(doctorCode), message);
            }
        } catch (IOException e) {
            log.error("给指定用户发时,用户{}出现错误,错误信息是:{}!", doctorCode, e.getMessage());
        }
    }

    /**
     * 功能描述 发送消息
     * @author zhaozm
     * @date 2022/11/9
     * @return void
     */
    public static void sendSelfMessage(){
        String doctorCode = null;
        try {
            Enumeration enu = webSocketMap.keys();

            while (enu.hasMoreElements()) {
                doctorCode = enu.nextElement().toString();
                if (webSocketMap.containsKey(doctorCode) && sessionDoctorMap.containsKey(doctorCode)){
                    //获取该token的提示信息
                    String message = "";
                    webSocketMap.get(doctorCode).sendMessage(sessionDoctorMap.get(doctorCode), message);
                }
            }
        } catch (IOException e) {
            log.error("给指定用户发时,用户{}出现错误,错误信息是:{}!", doctorCode, e.getMessage());
        }
    }

    public static synchronized int getOnlineCount() {
        return onlineCount;
    }

    public static synchronized void addOnlineCount() {
        TimeOutWebSocketServer.onlineCount++;
    }

    public static synchronized void subOnlineCount() {
        TimeOutWebSocketServer.onlineCount--;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值