|
@@ -1,35 +1,36 @@
|
|
|
package com.qmth.themis.exam.websocket;//package com.qmth.themis.backend.websocket;
|
|
|
|
|
|
-import java.io.IOException;
|
|
|
-import java.lang.reflect.Method;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.Objects;
|
|
|
-import java.util.concurrent.ConcurrentHashMap;
|
|
|
-
|
|
|
-import javax.websocket.OnClose;
|
|
|
-import javax.websocket.OnError;
|
|
|
-import javax.websocket.OnMessage;
|
|
|
-import javax.websocket.OnOpen;
|
|
|
-import javax.websocket.Session;
|
|
|
-import javax.websocket.server.PathParam;
|
|
|
-import javax.websocket.server.ServerEndpoint;
|
|
|
-
|
|
|
-import org.slf4j.Logger;
|
|
|
-import org.slf4j.LoggerFactory;
|
|
|
-import org.springframework.stereotype.Component;
|
|
|
-
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.qmth.themis.business.constant.SpringContextHolder;
|
|
|
import com.qmth.themis.business.constant.SystemConstant;
|
|
|
import com.qmth.themis.business.entity.TBSession;
|
|
|
+import com.qmth.themis.business.enums.MqEnum;
|
|
|
+import com.qmth.themis.business.enums.SystemOperationEnum;
|
|
|
import com.qmth.themis.business.util.RedisUtil;
|
|
|
import com.qmth.themis.common.enums.ExceptionResultEnum;
|
|
|
import com.qmth.themis.common.exception.BusinessException;
|
|
|
import com.qmth.themis.common.signature.SignatureInfo;
|
|
|
import com.qmth.themis.common.signature.SignatureType;
|
|
|
+import com.qmth.themis.common.util.Result;
|
|
|
+import com.qmth.themis.exam.config.DictionaryConfig;
|
|
|
import com.qmth.themis.exam.enums.WebsocketTypeEnum;
|
|
|
import com.qmth.themis.exam.websocketTemplete.WebSocketOeMessageTemplete;
|
|
|
+import com.qmth.themis.mq.dto.MqDto;
|
|
|
+import com.qmth.themis.mq.service.MqDtoService;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import javax.websocket.*;
|
|
|
+import javax.websocket.server.PathParam;
|
|
|
+import javax.websocket.server.ServerEndpoint;
|
|
|
+import java.io.IOException;
|
|
|
+import java.lang.reflect.Method;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
|
|
/**
|
|
|
* @Description: websocker服务端
|
|
@@ -50,11 +51,13 @@ public class WebSocketServer
|
|
|
*/
|
|
|
private Session session;
|
|
|
private String sessionId;
|
|
|
+ private Long recordId;
|
|
|
private String platform = null;
|
|
|
private String deviceId = null;
|
|
|
private String Authorization = null;
|
|
|
private Long time = null;
|
|
|
private RedisUtil redisUtil;
|
|
|
+ private Long updateTime = null;
|
|
|
|
|
|
/**
|
|
|
* 连接建立成功调用的方法
|
|
@@ -75,6 +78,7 @@ public class WebSocketServer
|
|
|
this.deviceId = String.valueOf(mapParameter.get("deviceId").get(0));
|
|
|
this.Authorization = String.valueOf(mapParameter.get("Authorization").get(0));
|
|
|
this.time = Long.parseLong(String.valueOf(mapParameter.get("time").get(0)));
|
|
|
+ this.recordId = Long.parseLong(String.valueOf(mapParameter.get("recordId").get(0)));
|
|
|
String method = SystemConstant.GET;
|
|
|
final SignatureInfo info = SignatureInfo
|
|
|
.parse(Authorization);
|
|
@@ -102,7 +106,7 @@ public class WebSocketServer
|
|
|
}
|
|
|
log.info("用户连接:" + this.sessionId + ",当前在线人数为:" + getOnlineCount());
|
|
|
try {
|
|
|
- sendMessage("连接成功");
|
|
|
+ this.sendMessage("连接成功");
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
log.error("用户:" + this.sessionId + ",网络异常!!!!!!");
|
|
@@ -125,8 +129,21 @@ public class WebSocketServer
|
|
|
webSocketMap.remove(this.sessionId);
|
|
|
//从set中删除
|
|
|
subOnlineCount();
|
|
|
+ //判断是否是正常退出
|
|
|
+ Date now = new Date();
|
|
|
+ //大于等于超时时间,说明规定时间内都没有通信,非正常退出,因为期间会有心跳更新updateTime
|
|
|
+ if ((now.getTime() - this.updateTime) / 1000 >= SystemConstant.WEBSOCKET_MAX_TIME_OUT / 1000) {
|
|
|
+ log.info("超时退出");
|
|
|
+ redisUtil.set(SystemConstant.WEBSOCKET_UN_NORMAL_LIST, this.sessionId, this.sessionId);
|
|
|
+ //发送延时mq消息start
|
|
|
+ MqDtoService mqDtoService = SpringContextHolder.getBean(MqDtoService.class);
|
|
|
+ DictionaryConfig dictionaryConfig = SpringContextHolder.getBean(DictionaryConfig.class);
|
|
|
+ MqDto mqDto = new MqDto(dictionaryConfig.mqConfigDomain().getWebsocketUnNormalTopic(), dictionaryConfig.mqConfigDomain().getWebsocketUnNormalTopicOeTag(), SystemOperationEnum.OE_NET_UN_NORMAL, MqEnum.WEBSOCKET_UN_NORMAL_LOG, String.valueOf(this.recordId), this.sessionId);
|
|
|
+ mqDtoService.assembleSendAsyncDelayMsg(mqDto, SystemConstant.mqDelayLevel.get("10s"));
|
|
|
+ //发送延时mq消息end
|
|
|
+ }
|
|
|
}
|
|
|
- log.info("用户退出:" + this.sessionId + ",当前在线人数为:" + getOnlineCount());
|
|
|
+ log.info("用户退出:{},当前在线人数为:{},updateTime:{}", this.sessionId, getOnlineCount(), this.updateTime);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -148,21 +165,11 @@ public class WebSocketServer
|
|
|
WebSocketOeMessageTemplete webSocketOeMessageTemplete = SpringContextHolder.getBean(WebSocketOeMessageTemplete.class);
|
|
|
String type = String.valueOf(jsonObject.get("type"));
|
|
|
Long time = Long.parseLong(String.valueOf(jsonObject.get("time")));
|
|
|
+ //todo 加入当前时间和time比较的校验
|
|
|
Method method = webSocketOeMessageTemplete.getClass().getDeclaredMethod(WebsocketTypeEnum.valueOf(type).getDesc(), String.class);
|
|
|
- method.invoke(webSocketOeMessageTemplete, String.valueOf(jsonObject.get("body")));
|
|
|
- session.getAsyncRemote().sendText("123");
|
|
|
+ Result result = (Result) method.invoke(webSocketOeMessageTemplete, String.valueOf(jsonObject.get("body")));
|
|
|
+ this.sendMessage(JSONObject.toJSONString(result));
|
|
|
}
|
|
|
-// //追加发送人(防止串改)
|
|
|
-// jsonObject.put("fromSessionId", this.sessionId);
|
|
|
-// String toSessionId = jsonObject.getString("toSessionId");
|
|
|
-// //传送给对应toSessionId用户的websocket
|
|
|
-// if (Objects.nonNull(toSessionId) && webSocketMap.containsKey(toSessionId)) {
|
|
|
-// webSocketMap.get(toSessionId).sendMessage(jsonObject.toJSONString());
|
|
|
-// log.info("发送消息jsonObject:{}", jsonObject.toJSONString());
|
|
|
-// } else {
|
|
|
-// log.error("请求的sessionId:" + toSessionId + "不在该服务器上");
|
|
|
-// //否则不在这个服务器上,发送到mysql或者redis
|
|
|
-// }
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
@@ -177,7 +184,7 @@ public class WebSocketServer
|
|
|
*/
|
|
|
@OnError
|
|
|
public void onError(Session session, Throwable error) {
|
|
|
- log.error("用户错误:" + this.sessionId + ",原因:" + error.getMessage());
|
|
|
+ log.error("用户错误:{},原因:{}", this.sessionId, error.getMessage());
|
|
|
error.printStackTrace();
|
|
|
throw new BusinessException(error.getMessage());
|
|
|
}
|
|
@@ -190,17 +197,19 @@ public class WebSocketServer
|
|
|
*/
|
|
|
public void sendMessage(String message) throws IOException {
|
|
|
log.info("message:{}", message);
|
|
|
+ this.session.getAsyncRemote().sendText(message);
|
|
|
+ this.updateTime = System.currentTimeMillis();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 发送自定义消息
|
|
|
*/
|
|
|
public static void sendInfo(String message, @PathParam("sessionId") String sessionId) throws IOException {
|
|
|
- log.info("发送消息到:" + sessionId + ",报文:" + message);
|
|
|
+ log.info("发送消息到:{},报文:{}", sessionId, message);
|
|
|
if (Objects.nonNull(sessionId) && webSocketMap.containsKey(sessionId)) {
|
|
|
webSocketMap.get(sessionId).sendMessage(message);
|
|
|
} else {
|
|
|
- log.error("用户" + sessionId + ",不在线!");
|
|
|
+ log.error("用户[:{}]不在线!", sessionId);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -218,7 +227,7 @@ public class WebSocketServer
|
|
|
* 在线人数加一
|
|
|
*/
|
|
|
public synchronized void addOnlineCount() {
|
|
|
- if (redisUtil.lock(SystemConstant.REDIS_LOCK_WEBSOCKET_PREFIX+this.sessionId, SystemConstant.REDIS_LOCK_WEBSOCKET_TIME_OUT)) {
|
|
|
+ if (redisUtil.lock(SystemConstant.REDIS_LOCK_WEBSOCKET_PREFIX + this.sessionId, SystemConstant.REDIS_LOCK_WEBSOCKET_TIME_OUT)) {
|
|
|
Object o = redisUtil.get(SystemConstant.WEBSOCKET_OE_ONLINE_COUNT);
|
|
|
int count = 0;
|
|
|
if (Objects.nonNull(o)) {
|
|
@@ -233,7 +242,7 @@ public class WebSocketServer
|
|
|
* 在线人数减一
|
|
|
*/
|
|
|
public synchronized void subOnlineCount() {
|
|
|
- if (redisUtil.lock(SystemConstant.REDIS_LOCK_WEBSOCKET_PREFIX+this.sessionId, SystemConstant.REDIS_LOCK_WEBSOCKET_TIME_OUT)) {
|
|
|
+ if (redisUtil.lock(SystemConstant.REDIS_LOCK_WEBSOCKET_PREFIX + this.sessionId, SystemConstant.REDIS_LOCK_WEBSOCKET_TIME_OUT)) {
|
|
|
Object o = redisUtil.get(SystemConstant.WEBSOCKET_OE_ONLINE_COUNT);
|
|
|
int count = 0;
|
|
|
if (Objects.nonNull(o)) {
|