|
@@ -1,213 +1,213 @@
|
|
|
-package org.springblade.consumer.socket;
|
|
|
-
|
|
|
-import cn.hutool.core.util.ObjectUtil;
|
|
|
-import cn.hutool.core.util.StrUtil;
|
|
|
-import com.alibaba.fastjson.JSON;
|
|
|
-import org.apache.commons.lang.StringUtils;
|
|
|
-import org.apache.logging.log4j.LogManager;
|
|
|
-import org.apache.logging.log4j.Logger;
|
|
|
-import org.springblade.business.feign.BusinessWebSocketClient;
|
|
|
-import org.springblade.common.constant.ClientIdConstant;
|
|
|
-import org.springblade.feign.ArchiveWebSocketClient;
|
|
|
-import org.springblade.manager.feign.ManagerWebSocketClient;
|
|
|
-import org.springblade.meter.feign.MeterWebSocketClient;
|
|
|
-import org.springframework.beans.BeansException;
|
|
|
-import org.springframework.context.ApplicationContext;
|
|
|
-import org.springframework.context.ApplicationContextAware;
|
|
|
-import org.springframework.scheduling.annotation.Scheduled;
|
|
|
-import org.springframework.stereotype.Component;
|
|
|
-import org.springframework.web.bind.annotation.CrossOrigin;
|
|
|
-
|
|
|
-import javax.websocket.*;
|
|
|
-import javax.websocket.server.PathParam;
|
|
|
-import javax.websocket.server.ServerEndpoint;
|
|
|
-import java.io.IOException;
|
|
|
-import java.util.*;
|
|
|
-import java.util.concurrent.ConcurrentHashMap;
|
|
|
-import java.util.concurrent.Executors;
|
|
|
-import java.util.concurrent.ScheduledExecutorService;
|
|
|
-import java.util.concurrent.TimeUnit;
|
|
|
-
|
|
|
-@Component
|
|
|
-@CrossOrigin
|
|
|
-@ServerEndpoint(value = "/websocket/{userId}")
|
|
|
-public class WebSocketEndpoint implements ApplicationContextAware {
|
|
|
-
|
|
|
- private static final ConcurrentHashMap<String, WebSocketEndpoint> webSocketMap = new ConcurrentHashMap<>();
|
|
|
- private static final ConcurrentHashMap<String, String> webSocketMsgMap = new ConcurrentHashMap<>();
|
|
|
- private static final Logger logger = LogManager.getLogger(WebSocketEndpoint.class);
|
|
|
- private static int onlineCount = 0;
|
|
|
- private Session session;
|
|
|
- private String userId;
|
|
|
-
|
|
|
- private static ArchiveWebSocketClient archiveWebSocketClient;
|
|
|
- private static BusinessWebSocketClient businessWebSocketClient;
|
|
|
- private static ManagerWebSocketClient managerWebSocketClient;
|
|
|
- private static MeterWebSocketClient meterWebSocketClient;
|
|
|
-
|
|
|
- @Override
|
|
|
- public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
|
|
- WebSocketEndpoint.archiveWebSocketClient = applicationContext.getBean(ArchiveWebSocketClient.class);
|
|
|
- WebSocketEndpoint.businessWebSocketClient = applicationContext.getBean(BusinessWebSocketClient.class);
|
|
|
- WebSocketEndpoint.managerWebSocketClient = applicationContext.getBean(ManagerWebSocketClient.class);
|
|
|
- WebSocketEndpoint.meterWebSocketClient = applicationContext.getBean(MeterWebSocketClient.class);
|
|
|
- }
|
|
|
-
|
|
|
- @OnOpen
|
|
|
- public void onOpen(Session session, @PathParam("userId") String userId) {
|
|
|
- this.session = session;
|
|
|
- this.userId = userId;
|
|
|
-
|
|
|
- ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
|
|
|
- executorService.schedule(() -> {
|
|
|
-
|
|
|
- webSocketMap.put(userId, this);
|
|
|
-
|
|
|
- addOnlineCount();
|
|
|
- logger.info("用户:{}连接成功,当前在线人数为{}人", userId, getOnlineCount());
|
|
|
- if (ObjectUtil.isNotEmpty(this.session.getQueryString())) {
|
|
|
- try {
|
|
|
- sendMessageByUserId(userId, this.session.getQueryString());
|
|
|
- } catch (IOException e) {
|
|
|
- logger.error("IO异常");
|
|
|
- } finally {
|
|
|
- executorService.shutdown();
|
|
|
- }
|
|
|
- }
|
|
|
- }, 1, TimeUnit.SECONDS);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- @OnClose
|
|
|
- public void onClose() {
|
|
|
- webSocketMap.remove(userId);
|
|
|
- webSocketMsgMap.remove(userId);
|
|
|
- subOnlineCount();
|
|
|
- logger.info("用户:{}关闭了连接!当前在线人数为{}人", userId, getOnlineCount());
|
|
|
- }
|
|
|
-
|
|
|
- @OnMessage
|
|
|
- public void onMessage(String message, Session session) {
|
|
|
- if (StringUtils.isNotEmpty(userId)) {
|
|
|
- String projectId, contractId, clientId;
|
|
|
- if (!message.contains(",")) {
|
|
|
- clientId = ClientIdConstant.MANAGER_CLIENT_ID;
|
|
|
- projectId = "1";
|
|
|
- contractId = "1";
|
|
|
- } else {
|
|
|
- projectId = message.split(",")[0];
|
|
|
- contractId = message.split(",")[1];
|
|
|
- clientId = message.split(",")[2];
|
|
|
- }
|
|
|
-
|
|
|
- if (StringUtils.isNotEmpty(clientId) && StringUtils.isNotEmpty(projectId) && StringUtils.isNotEmpty(contractId)) {
|
|
|
-
|
|
|
- logger.info("来自用户:{} 消息:{}", userId, message);
|
|
|
- webSocketMsgMap.put(userId, message);
|
|
|
-
|
|
|
- Map<String, String> stringMap = new HashMap<>();
|
|
|
- switch (clientId) {
|
|
|
- case ClientIdConstant.ARCHIVE_CLIENT_ID:
|
|
|
- stringMap = archiveWebSocketClient.getWebsocketMsg(projectId, contractId, userId);
|
|
|
- break;
|
|
|
- case ClientIdConstant.MANAGER_CLIENT_ID:
|
|
|
- stringMap = managerWebSocketClient.getWebsocketMsg(projectId, contractId, userId);
|
|
|
- break;
|
|
|
- case ClientIdConstant.BUSINESS_CLIENT_ID:
|
|
|
- stringMap = businessWebSocketClient.getWebsocketMsg(projectId, contractId, userId);
|
|
|
- break;
|
|
|
- case ClientIdConstant.METER_CLIENT_ID:
|
|
|
- stringMap = meterWebSocketClient.getWebsocketMsg(projectId, contractId, userId);
|
|
|
- break;
|
|
|
- }
|
|
|
- try {
|
|
|
- sendMessageByUserId(userId, JSON.toJSONString(stringMap));
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
- } else {
|
|
|
- logger.info("未获取到用户信息,接收消息失败");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @OnError
|
|
|
- public void onError(Session session, Throwable error) {
|
|
|
- logger.error("用户错误:" + this.userId + ",原因:" + error.getMessage());
|
|
|
- error.printStackTrace();
|
|
|
- }
|
|
|
-
|
|
|
- public void sendMessageByUserId(String userId, String message) throws IOException {
|
|
|
- logger.info("服务端发送消息到用户:{},消息:{}", userId, message);
|
|
|
- if (StrUtil.isNotBlank(userId) && webSocketMap.containsKey(userId)) {
|
|
|
- webSocketMap.get(userId).sendMessage(message);
|
|
|
- } else {
|
|
|
- logger.error("用户{}不在线", userId);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public void sendMessage(String message) {
|
|
|
- this.session.getAsyncRemote().sendText(message);
|
|
|
- }
|
|
|
-
|
|
|
- public static synchronized int getOnlineCount() {
|
|
|
- return onlineCount;
|
|
|
- }
|
|
|
-
|
|
|
- public static synchronized void addOnlineCount() {
|
|
|
- WebSocketEndpoint.onlineCount++;
|
|
|
- }
|
|
|
-
|
|
|
- public static synchronized void subOnlineCount() {
|
|
|
- WebSocketEndpoint.onlineCount--;
|
|
|
- }
|
|
|
-
|
|
|
- @Scheduled(cron = "0 0/5 * * * ?")
|
|
|
- public void reSendMessage() {
|
|
|
- if (webSocketMap.isEmpty() || webSocketMsgMap.isEmpty()) {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- logger.info("************************ 定时重发消息,reSendMessage()方法执行开始 ************************");
|
|
|
-
|
|
|
- Set<Map.Entry<String, String>> messageMaps = webSocketMsgMap.entrySet();
|
|
|
- for (Map.Entry<String, String> message : messageMaps) {
|
|
|
- String userId = message.getKey();
|
|
|
- String values = message.getValue();
|
|
|
- if (values.contains(",") && StringUtils.isNotEmpty(userId)) {
|
|
|
- String[] splitValues = values.split(",");
|
|
|
- if (splitValues.length == 3) {
|
|
|
- String projectId = splitValues[0];
|
|
|
- String contractId = splitValues[1];
|
|
|
- String clientId = splitValues[2];
|
|
|
- if (StringUtils.isNotEmpty(clientId) && StringUtils.isNotEmpty(contractId) && StringUtils.isNotEmpty(projectId)) {
|
|
|
- Map<String, String> stringMap = new HashMap<>();
|
|
|
- switch (clientId) {
|
|
|
- case ClientIdConstant.ARCHIVE_CLIENT_ID:
|
|
|
- stringMap = archiveWebSocketClient.getWebsocketMsg(projectId, contractId, userId);
|
|
|
- break;
|
|
|
- case ClientIdConstant.MANAGER_CLIENT_ID:
|
|
|
- stringMap = managerWebSocketClient.getWebsocketMsg(projectId, contractId, userId);
|
|
|
- break;
|
|
|
- case ClientIdConstant.BUSINESS_CLIENT_ID:
|
|
|
- stringMap = businessWebSocketClient.getWebsocketMsg(projectId, contractId, userId);
|
|
|
- break;
|
|
|
- case ClientIdConstant.METER_CLIENT_ID:
|
|
|
- stringMap = meterWebSocketClient.getWebsocketMsg(projectId, contractId, userId);
|
|
|
- break;
|
|
|
- }
|
|
|
- if (stringMap.size() > 0) {
|
|
|
- try {
|
|
|
- sendMessageByUserId(userId, JSON.toJSONString(stringMap));
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- logger.info("************************ 定时重发消息,reSendMessage()方法执行结束 ************************");
|
|
|
- }
|
|
|
-
|
|
|
-}
|
|
|
+//package org.springblade.consumer.socket;
|
|
|
+//
|
|
|
+//import cn.hutool.core.util.ObjectUtil;
|
|
|
+//import cn.hutool.core.util.StrUtil;
|
|
|
+//import com.alibaba.fastjson.JSON;
|
|
|
+//import org.apache.commons.lang.StringUtils;
|
|
|
+//import org.apache.logging.log4j.LogManager;
|
|
|
+//import org.apache.logging.log4j.Logger;
|
|
|
+//import org.springblade.business.feign.BusinessWebSocketClient;
|
|
|
+//import org.springblade.common.constant.ClientIdConstant;
|
|
|
+//import org.springblade.feign.ArchiveWebSocketClient;
|
|
|
+//import org.springblade.manager.feign.ManagerWebSocketClient;
|
|
|
+//import org.springblade.meter.feign.MeterWebSocketClient;
|
|
|
+//import org.springframework.beans.BeansException;
|
|
|
+//import org.springframework.context.ApplicationContext;
|
|
|
+//import org.springframework.context.ApplicationContextAware;
|
|
|
+//import org.springframework.scheduling.annotation.Scheduled;
|
|
|
+//import org.springframework.stereotype.Component;
|
|
|
+//import org.springframework.web.bind.annotation.CrossOrigin;
|
|
|
+//
|
|
|
+//import javax.websocket.*;
|
|
|
+//import javax.websocket.server.PathParam;
|
|
|
+//import javax.websocket.server.ServerEndpoint;
|
|
|
+//import java.io.IOException;
|
|
|
+//import java.util.*;
|
|
|
+//import java.util.concurrent.ConcurrentHashMap;
|
|
|
+//import java.util.concurrent.Executors;
|
|
|
+//import java.util.concurrent.ScheduledExecutorService;
|
|
|
+//import java.util.concurrent.TimeUnit;
|
|
|
+//
|
|
|
+//@Component
|
|
|
+//@CrossOrigin
|
|
|
+//@ServerEndpoint(value = "/websocket/{userId}")
|
|
|
+//public class WebSocketEndpoint implements ApplicationContextAware {
|
|
|
+//
|
|
|
+// private static final ConcurrentHashMap<String, WebSocketEndpoint> webSocketMap = new ConcurrentHashMap<>();
|
|
|
+// private static final ConcurrentHashMap<String, String> webSocketMsgMap = new ConcurrentHashMap<>();
|
|
|
+// private static final Logger logger = LogManager.getLogger(WebSocketEndpoint.class);
|
|
|
+// private static int onlineCount = 0;
|
|
|
+// private Session session;
|
|
|
+// private String userId;
|
|
|
+//
|
|
|
+// private static ArchiveWebSocketClient archiveWebSocketClient;
|
|
|
+// private static BusinessWebSocketClient businessWebSocketClient;
|
|
|
+// private static ManagerWebSocketClient managerWebSocketClient;
|
|
|
+// private static MeterWebSocketClient meterWebSocketClient;
|
|
|
+//
|
|
|
+// @Override
|
|
|
+// public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
|
|
+// WebSocketEndpoint.archiveWebSocketClient = applicationContext.getBean(ArchiveWebSocketClient.class);
|
|
|
+// WebSocketEndpoint.businessWebSocketClient = applicationContext.getBean(BusinessWebSocketClient.class);
|
|
|
+// WebSocketEndpoint.managerWebSocketClient = applicationContext.getBean(ManagerWebSocketClient.class);
|
|
|
+// WebSocketEndpoint.meterWebSocketClient = applicationContext.getBean(MeterWebSocketClient.class);
|
|
|
+// }
|
|
|
+//
|
|
|
+// @OnOpen
|
|
|
+// public void onOpen(Session session, @PathParam("userId") String userId) {
|
|
|
+// this.session = session;
|
|
|
+// this.userId = userId;
|
|
|
+//
|
|
|
+// ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
|
|
|
+// executorService.schedule(() -> {
|
|
|
+//
|
|
|
+// webSocketMap.put(userId, this);
|
|
|
+//
|
|
|
+// addOnlineCount();
|
|
|
+// logger.info("用户:{}连接成功,当前在线人数为{}人", userId, getOnlineCount());
|
|
|
+// if (ObjectUtil.isNotEmpty(this.session.getQueryString())) {
|
|
|
+// try {
|
|
|
+// sendMessageByUserId(userId, this.session.getQueryString());
|
|
|
+// } catch (IOException e) {
|
|
|
+// logger.error("IO异常");
|
|
|
+// } finally {
|
|
|
+// executorService.shutdown();
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }, 1, TimeUnit.SECONDS);
|
|
|
+// }
|
|
|
+//
|
|
|
+//
|
|
|
+// @OnClose
|
|
|
+// public void onClose() {
|
|
|
+// webSocketMap.remove(userId);
|
|
|
+// webSocketMsgMap.remove(userId);
|
|
|
+// subOnlineCount();
|
|
|
+// logger.info("用户:{}关闭了连接!当前在线人数为{}人", userId, getOnlineCount());
|
|
|
+// }
|
|
|
+//
|
|
|
+// @OnMessage
|
|
|
+// public void onMessage(String message, Session session) {
|
|
|
+// if (StringUtils.isNotEmpty(userId)) {
|
|
|
+// String projectId, contractId, clientId;
|
|
|
+// if (!message.contains(",")) {
|
|
|
+// clientId = ClientIdConstant.MANAGER_CLIENT_ID;
|
|
|
+// projectId = "1";
|
|
|
+// contractId = "1";
|
|
|
+// } else {
|
|
|
+// projectId = message.split(",")[0];
|
|
|
+// contractId = message.split(",")[1];
|
|
|
+// clientId = message.split(",")[2];
|
|
|
+// }
|
|
|
+//
|
|
|
+// if (StringUtils.isNotEmpty(clientId) && StringUtils.isNotEmpty(projectId) && StringUtils.isNotEmpty(contractId)) {
|
|
|
+//
|
|
|
+// logger.info("来自用户:{} 消息:{}", userId, message);
|
|
|
+// webSocketMsgMap.put(userId, message);
|
|
|
+//
|
|
|
+// Map<String, String> stringMap = new HashMap<>();
|
|
|
+// switch (clientId) {
|
|
|
+// case ClientIdConstant.ARCHIVE_CLIENT_ID:
|
|
|
+// stringMap = archiveWebSocketClient.getWebsocketMsg(projectId, contractId, userId);
|
|
|
+// break;
|
|
|
+// case ClientIdConstant.MANAGER_CLIENT_ID:
|
|
|
+// stringMap = managerWebSocketClient.getWebsocketMsg(projectId, contractId, userId);
|
|
|
+// break;
|
|
|
+// case ClientIdConstant.BUSINESS_CLIENT_ID:
|
|
|
+// stringMap = businessWebSocketClient.getWebsocketMsg(projectId, contractId, userId);
|
|
|
+// break;
|
|
|
+// case ClientIdConstant.METER_CLIENT_ID:
|
|
|
+// stringMap = meterWebSocketClient.getWebsocketMsg(projectId, contractId, userId);
|
|
|
+// break;
|
|
|
+// }
|
|
|
+// try {
|
|
|
+// sendMessageByUserId(userId, JSON.toJSONString(stringMap));
|
|
|
+// } catch (IOException e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+// }
|
|
|
+// } else {
|
|
|
+// logger.info("未获取到用户信息,接收消息失败");
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// @OnError
|
|
|
+// public void onError(Session session, Throwable error) {
|
|
|
+// logger.error("用户错误:" + this.userId + ",原因:" + error.getMessage());
|
|
|
+// error.printStackTrace();
|
|
|
+// }
|
|
|
+//
|
|
|
+// public void sendMessageByUserId(String userId, String message) throws IOException {
|
|
|
+// logger.info("服务端发送消息到用户:{},消息:{}", userId, message);
|
|
|
+// if (StrUtil.isNotBlank(userId) && webSocketMap.containsKey(userId)) {
|
|
|
+// webSocketMap.get(userId).sendMessage(message);
|
|
|
+// } else {
|
|
|
+// logger.error("用户{}不在线", userId);
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// public void sendMessage(String message) {
|
|
|
+// this.session.getAsyncRemote().sendText(message);
|
|
|
+// }
|
|
|
+//
|
|
|
+// public static synchronized int getOnlineCount() {
|
|
|
+// return onlineCount;
|
|
|
+// }
|
|
|
+//
|
|
|
+// public static synchronized void addOnlineCount() {
|
|
|
+// WebSocketEndpoint.onlineCount++;
|
|
|
+// }
|
|
|
+//
|
|
|
+// public static synchronized void subOnlineCount() {
|
|
|
+// WebSocketEndpoint.onlineCount--;
|
|
|
+// }
|
|
|
+//
|
|
|
+// @Scheduled(cron = "0 0/5 * * * ?")
|
|
|
+// public void reSendMessage() {
|
|
|
+// if (webSocketMap.isEmpty() || webSocketMsgMap.isEmpty()) {
|
|
|
+// return;
|
|
|
+// }
|
|
|
+//
|
|
|
+// logger.info("************************ 定时重发消息,reSendMessage()方法执行开始 ************************");
|
|
|
+//
|
|
|
+// Set<Map.Entry<String, String>> messageMaps = webSocketMsgMap.entrySet();
|
|
|
+// for (Map.Entry<String, String> message : messageMaps) {
|
|
|
+// String userId = message.getKey();
|
|
|
+// String values = message.getValue();
|
|
|
+// if (values.contains(",") && StringUtils.isNotEmpty(userId)) {
|
|
|
+// String[] splitValues = values.split(",");
|
|
|
+// if (splitValues.length == 3) {
|
|
|
+// String projectId = splitValues[0];
|
|
|
+// String contractId = splitValues[1];
|
|
|
+// String clientId = splitValues[2];
|
|
|
+// if (StringUtils.isNotEmpty(clientId) && StringUtils.isNotEmpty(contractId) && StringUtils.isNotEmpty(projectId)) {
|
|
|
+// Map<String, String> stringMap = new HashMap<>();
|
|
|
+// switch (clientId) {
|
|
|
+// case ClientIdConstant.ARCHIVE_CLIENT_ID:
|
|
|
+// stringMap = archiveWebSocketClient.getWebsocketMsg(projectId, contractId, userId);
|
|
|
+// break;
|
|
|
+// case ClientIdConstant.MANAGER_CLIENT_ID:
|
|
|
+// stringMap = managerWebSocketClient.getWebsocketMsg(projectId, contractId, userId);
|
|
|
+// break;
|
|
|
+// case ClientIdConstant.BUSINESS_CLIENT_ID:
|
|
|
+// stringMap = businessWebSocketClient.getWebsocketMsg(projectId, contractId, userId);
|
|
|
+// break;
|
|
|
+// case ClientIdConstant.METER_CLIENT_ID:
|
|
|
+// stringMap = meterWebSocketClient.getWebsocketMsg(projectId, contractId, userId);
|
|
|
+// break;
|
|
|
+// }
|
|
|
+// if (stringMap.size() > 0) {
|
|
|
+// try {
|
|
|
+// sendMessageByUserId(userId, JSON.toJSONString(stringMap));
|
|
|
+// } catch (IOException e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+// logger.info("************************ 定时重发消息,reSendMessage()方法执行结束 ************************");
|
|
|
+// }
|
|
|
+//
|
|
|
+//}
|