WebSocketServer.java 5.53 KB
Newer Older
Rensq's avatar
Rensq committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
//package com.testor.websocket;
//
//import cn.hutool.core.util.StrUtil;
//import com.alibaba.fastjson.JSON;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.stereotype.Component;
//
//import javax.websocket.*;
//import javax.websocket.server.PathParam;
//import javax.websocket.server.ServerEndpoint;
//import java.io.IOException;
//import java.util.Date;
//import java.util.Map;
//import java.util.concurrent.ConcurrentHashMap;
//
//@Slf4j
//@Component
//@ServerEndpoint("/webSocket/{userId}")
//public class WebSocketServer {
//    private static ConcurrentHashMap<String, Session> userInfoMap = new ConcurrentHashMap<>();
//    private static int onlineCount = 0;
//
//    /**
//     * 建立连接调用的方法
//     *
//     * @param session
//     * @param userId
//     */
//    @OnOpen
//    public void onOpen(Session session, @PathParam("userId") String userId) {
//        if (StrUtil.isBlank(userId)) {
//            return;
//        }
//        userInfoMap.put(userId, session);
//        onlineCount = userInfoMap.size();
////        log.info("建立连接成功,当前人数为" + onlineCount);
////        String message = "建立连接成功,当前人数为" + onlineCount;
//
////        Message messageObj = new Message();
////        messageObj.setFrom("System");
////        messageObj.setTo("1242684364872761344");
////        messageObj.setMsg("pong");
////        messageObj.setType("0");
////        messageObj.setSendTime(new Date());
////        sendAllMessageByAsync(messageObj);
//    }
//
//    /**
//     * 收到客户端消息后调用的方法
//     * 后台收到客户端发送过来的消息
//     *
//     * @param userId
//     * @param message 客户端发送过来的消息
//     */
//    @OnMessage
//    public void onMessage(@PathParam("userId") String userId, String message) {
//        try {
//            if (StrUtil.isBlank(message)) {
//                return;
//            }
//            Message messageObj = JSON.parseObject(message, Message.class);
//            if (messageObj.getType().equals("0") && messageObj.getMsg().equals("ping")) {
//               String from = messageObj.getFrom();
//                messageObj.setFrom("System");
//                messageObj.setTo(from);
//                messageObj.setMsg("pong");
//                messageObj.setType("0");
//                messageObj.setSendTime(new Date());
//                sendTextMessage(messageObj);
//                return;
//            }
//            sendTextMessage(messageObj);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//
//    @OnError
//    public void onError(Throwable e) {
//        log.info("错误{}", e);
//    }
//
//    /**
//     * 关闭链接调用接口
//     *
//     * @param userId
//     */
//    @OnClose
//    public void onClose(@PathParam("userId") String userId) {
//        if (StrUtil.isBlank(userId)) {
//            return;
//        }
//        userInfoMap.remove(userId);
//        onlineCount = userInfoMap.size();
//        log.info("断开连接成功,当前人数为" + onlineCount);
//    }
//
//    /**
//     * 群发自定义消息
//     * 有两种会话发送信息的方法:
//     * session.getBasicRemote().sendText(message):同步发送信息
//     * session.getAsyncRemote().sendText(message):异步发送信息
//     */
//    public void sendAllMessageBySync(Message message) {
//
//        for (Map.Entry<String, Session> entry : userInfoMap.entrySet()) {
//            try {
//                Session session = entry.getValue();
//                message.setTo(entry.getKey());
//                // 判断会话是否连接
//                if (session.isOpen()) {
//                    // 发送同步信息到当前会话
//                    session.getBasicRemote().sendText(JSON.toJSONString(message));
//                }
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//    }
//
//    /**
//     * 此为广播消息
//     * 异步
//     **/
//    public void sendAllMessageByAsync(Message message) {
//        for (Map.Entry<String, Session> entry : userInfoMap.entrySet()) {
//            System.out.println("【websocket消息】广播消息:" + JSON.toJSONString(message));
//            try {
//                Session session = entry.getValue();
//                message.setTo(entry.getKey());
//                if (session.isOpen()) {
//                    // 发送同步信息到当前会话
//                    session.getAsyncRemote().sendText(JSON.toJSONString(message));
//                }
//
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
//    }
//
//    /**
//     * 此为单点消息:指定发送人 (发送文本)
//     * 同步
//     **/
//
//    public void sendTextMessage(Message message) {
//        if (message == null || StrUtil.isBlank(message.getTo())) {
//            return;
//        }
//        String toUsers = message.getTo();
//        if ("-1".equals(toUsers)) {
//            this.sendAllMessageByAsync(message);
//        }
//
//        String[] toUserArray = toUsers.split(",");
//        Session session;
//        try {
//            for (String userId : toUserArray) {
//                session = userInfoMap.get(userId);
//                if (session != null) {
//                    message.setTo(userId);
//                    session.getBasicRemote().sendText(JSON.toJSONString(message));
//                }
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//    }
//}