|
@@ -1,110 +1,110 @@
|
|
|
-package thyyxxk.wxservice_server.websocket;
|
|
|
-
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.springframework.stereotype.Component;
|
|
|
-import thyyxxk.wxservice_server.constant.Constants;
|
|
|
-
|
|
|
-import javax.websocket.*;
|
|
|
-import javax.websocket.server.PathParam;
|
|
|
-import javax.websocket.server.ServerEndpoint;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.concurrent.ConcurrentHashMap;
|
|
|
-import java.util.concurrent.atomic.AtomicInteger;
|
|
|
-
|
|
|
-/**
|
|
|
- * @description: websocket config
|
|
|
- * @author: DingJie
|
|
|
- * @create: 2021-05-18 11:20:43
|
|
|
- **/
|
|
|
-@Slf4j
|
|
|
-@ServerEndpoint("/websocket/{openId}")
|
|
|
-@Component
|
|
|
-public class WebSocketServer {
|
|
|
- private static final AtomicInteger ONLINE_COUNT = new AtomicInteger();
|
|
|
- private static ConcurrentHashMap<String, Session> SESSION_MAP = new ConcurrentHashMap<>(Constants.Capacity.DEFAULT);
|
|
|
-
|
|
|
- public static void sendMessage(String openId, String message) {
|
|
|
- if (null == SESSION_MAP) {
|
|
|
- return;
|
|
|
- }
|
|
|
- Session session = SESSION_MAP.get(openId);
|
|
|
- if (null == session) {
|
|
|
- return;
|
|
|
- }
|
|
|
- try {
|
|
|
- session.getBasicRemote().sendText(message);
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("发送socket消息出错。【openId: {},message: {}, exception: {}】", openId, message, e.getMessage());
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public static int getOnlineCount() {
|
|
|
- return ONLINE_COUNT.get();
|
|
|
- }
|
|
|
-
|
|
|
- public static int systemBreak() {
|
|
|
- int count = 0;
|
|
|
- if (null == SESSION_MAP) {
|
|
|
- return count;
|
|
|
- }
|
|
|
- for (Map.Entry<String, Session> entry : SESSION_MAP.entrySet()) {
|
|
|
- if (entry.getValue().isOpen()) {
|
|
|
- try {
|
|
|
- CloseReason closeReason = new CloseReason(CloseReason.CloseCodes.NORMAL_CLOSURE, "服务更新。");
|
|
|
- entry.getValue().close(closeReason);
|
|
|
- count++;
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("关闭WebSocket发生错误>>> session: {},错误:{}", entry.getValue().getId(), e.getMessage());
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- SESSION_MAP = null;
|
|
|
- ONLINE_COUNT.set(0);
|
|
|
- return count;
|
|
|
- }
|
|
|
-
|
|
|
- public static void systemRecover() {
|
|
|
- if (null == SESSION_MAP) {
|
|
|
- SESSION_MAP = new ConcurrentHashMap<>(Constants.Capacity.DEFAULT);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @OnOpen
|
|
|
- public void onOpen(Session session, @PathParam("openId") String openId) {
|
|
|
- if (null == SESSION_MAP) {
|
|
|
- if (session.isOpen()) {
|
|
|
- try {
|
|
|
- CloseReason closeReason = new CloseReason(CloseReason.CloseCodes.NORMAL_CLOSURE, "服务更新。");
|
|
|
- session.close(closeReason);
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("关闭WebSocket发生错误>>> session: {},错误:{}", session.getId(), e.getMessage());
|
|
|
- }
|
|
|
- }
|
|
|
- } else {
|
|
|
- SESSION_MAP.put(openId, session);
|
|
|
- ONLINE_COUNT.incrementAndGet();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @OnClose
|
|
|
- public void onClose(@PathParam("openId") String openId) {
|
|
|
- if (null != SESSION_MAP) {
|
|
|
- SESSION_MAP.remove(openId);
|
|
|
- }
|
|
|
- ONLINE_COUNT.decrementAndGet();
|
|
|
- }
|
|
|
-
|
|
|
- @OnError
|
|
|
- public void onError(Session session, Throwable error) {
|
|
|
- log.error("WebSocket发生错误>>> session: {},错误:{} ", session.getId(), error.getMessage());
|
|
|
- if (session.isOpen()) {
|
|
|
- try {
|
|
|
- CloseReason closeReason = new CloseReason(CloseReason.CloseCodes.NORMAL_CLOSURE, "服务更新。");
|
|
|
- session.close(closeReason);
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("关闭WebSocket发生错误>>> session: {},错误:{}", session.getId(), e.getMessage());
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
+//package thyyxxk.wxservice_server.websocket;
|
|
|
+//
|
|
|
+//import lombok.extern.slf4j.Slf4j;
|
|
|
+//import org.springframework.stereotype.Component;
|
|
|
+//import thyyxxk.wxservice_server.constant.Constants;
|
|
|
+//
|
|
|
+//import javax.websocket.*;
|
|
|
+//import javax.websocket.server.PathParam;
|
|
|
+//import javax.websocket.server.ServerEndpoint;
|
|
|
+//import java.util.Map;
|
|
|
+//import java.util.concurrent.ConcurrentHashMap;
|
|
|
+//import java.util.concurrent.atomic.AtomicInteger;
|
|
|
+//
|
|
|
+///**
|
|
|
+// * @description: websocket config
|
|
|
+// * @author: DingJie
|
|
|
+// * @create: 2021-05-18 11:20:43
|
|
|
+// **/
|
|
|
+//@Slf4j
|
|
|
+//@ServerEndpoint("/websocket/{openId}")
|
|
|
+//@Component
|
|
|
+//public class WebSocketServer {
|
|
|
+// private static final AtomicInteger ONLINE_COUNT = new AtomicInteger();
|
|
|
+// private static ConcurrentHashMap<String, Session> SESSION_MAP = new ConcurrentHashMap<>(Constants.Capacity.DEFAULT);
|
|
|
+//
|
|
|
+// public static void sendMessage(String openId, String message) {
|
|
|
+// if (null == SESSION_MAP) {
|
|
|
+// return;
|
|
|
+// }
|
|
|
+// Session session = SESSION_MAP.get(openId);
|
|
|
+// if (null == session) {
|
|
|
+// return;
|
|
|
+// }
|
|
|
+// try {
|
|
|
+// session.getBasicRemote().sendText(message);
|
|
|
+// } catch (Exception e) {
|
|
|
+// log.error("发送socket消息出错。【openId: {},message: {}, exception: {}】", openId, message, e.getMessage());
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// public static int getOnlineCount() {
|
|
|
+// return ONLINE_COUNT.get();
|
|
|
+// }
|
|
|
+//
|
|
|
+// public static int systemBreak() {
|
|
|
+// int count = 0;
|
|
|
+// if (null == SESSION_MAP) {
|
|
|
+// return count;
|
|
|
+// }
|
|
|
+// for (Map.Entry<String, Session> entry : SESSION_MAP.entrySet()) {
|
|
|
+// if (entry.getValue().isOpen()) {
|
|
|
+// try {
|
|
|
+// CloseReason closeReason = new CloseReason(CloseReason.CloseCodes.NORMAL_CLOSURE, "服务更新。");
|
|
|
+// entry.getValue().close(closeReason);
|
|
|
+// count++;
|
|
|
+// } catch (Exception e) {
|
|
|
+// log.error("关闭WebSocket发生错误>>> session: {},错误:{}", entry.getValue().getId(), e.getMessage());
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+// SESSION_MAP = null;
|
|
|
+// ONLINE_COUNT.set(0);
|
|
|
+// return count;
|
|
|
+// }
|
|
|
+//
|
|
|
+// public static void systemRecover() {
|
|
|
+// if (null == SESSION_MAP) {
|
|
|
+// SESSION_MAP = new ConcurrentHashMap<>(Constants.Capacity.DEFAULT);
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// @OnOpen
|
|
|
+// public void onOpen(Session session, @PathParam("openId") String openId) {
|
|
|
+// if (null == SESSION_MAP) {
|
|
|
+// if (session.isOpen()) {
|
|
|
+// try {
|
|
|
+// CloseReason closeReason = new CloseReason(CloseReason.CloseCodes.NORMAL_CLOSURE, "服务更新。");
|
|
|
+// session.close(closeReason);
|
|
|
+// } catch (Exception e) {
|
|
|
+// log.error("关闭WebSocket发生错误>>> session: {},错误:{}", session.getId(), e.getMessage());
|
|
|
+// }
|
|
|
+// }
|
|
|
+// } else {
|
|
|
+// SESSION_MAP.put(openId, session);
|
|
|
+// ONLINE_COUNT.incrementAndGet();
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// @OnClose
|
|
|
+// public void onClose(@PathParam("openId") String openId) {
|
|
|
+// if (null != SESSION_MAP) {
|
|
|
+// SESSION_MAP.remove(openId);
|
|
|
+// }
|
|
|
+// ONLINE_COUNT.decrementAndGet();
|
|
|
+// }
|
|
|
+//
|
|
|
+// @OnError
|
|
|
+// public void onError(Session session, Throwable error) {
|
|
|
+// log.error("WebSocket发生错误>>> session: {},错误:{} ", session.getId(), error.getMessage());
|
|
|
+// if (session.isOpen()) {
|
|
|
+// try {
|
|
|
+// CloseReason closeReason = new CloseReason(CloseReason.CloseCodes.NORMAL_CLOSURE, "服务更新。");
|
|
|
+// session.close(closeReason);
|
|
|
+// } catch (Exception e) {
|
|
|
+// log.error("关闭WebSocket发生错误>>> session: {},错误:{}", session.getId(), e.getMessage());
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+//}
|