|
@@ -2,15 +2,23 @@ package com.qmth.themis.exam.websocket;//package com.qmth.themis.backend.websock
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.qmth.themis.business.entity.TBSession;
|
|
|
+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 org.apache.commons.lang3.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
import javax.websocket.*;
|
|
|
import javax.websocket.server.PathParam;
|
|
|
import javax.websocket.server.ServerEndpoint;
|
|
|
import java.io.IOException;
|
|
|
+import java.util.Objects;
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
|
|
/**
|
|
@@ -20,7 +28,7 @@ import java.util.concurrent.ConcurrentHashMap;
|
|
|
* @Author: wangliang
|
|
|
* @Date: 2020/7/10
|
|
|
*/
|
|
|
-@ServerEndpoint("/oe/{platform}/{deviceId}/{Authorization}/{time}")
|
|
|
+@ServerEndpoint("/oe/{platform}/{deviceId}/{Authorization}/{time}/{method}")
|
|
|
@Component
|
|
|
public class WebSocketServer
|
|
|
// implements MessageListenerConcurrently
|
|
@@ -38,33 +46,61 @@ public class WebSocketServer
|
|
|
* 与某个客户端的连接会话,需要通过它来给客户端发送数据
|
|
|
*/
|
|
|
private Session session;
|
|
|
- /**
|
|
|
- * 接收userId
|
|
|
- */
|
|
|
- private String userId = null;
|
|
|
+
|
|
|
+ private String platform = null;
|
|
|
+ private String deviceId = null;
|
|
|
+ private String Authorization = null;
|
|
|
+ private Long time = null;
|
|
|
+ private String method = null;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ RedisUtil redisUtil;
|
|
|
|
|
|
/**
|
|
|
* 连接建立成功调用的方法
|
|
|
*/
|
|
|
@OnOpen
|
|
|
- public void onOpen(Session session, @PathParam("platform") String platform, @PathParam("deviceId") String deviceI, @PathParam("Authorization") String Authorization, @PathParam("time") Long time) {
|
|
|
+ public void onOpen(Session session, @PathParam("platform") String platform, @PathParam("deviceId") String deviceId, @PathParam("Authorization") String Authorization, @PathParam("time") Long time, @PathParam("method") String method) {
|
|
|
this.session = session;
|
|
|
- this.userId = userId;
|
|
|
- if (webSocketMap.containsKey(userId)) {
|
|
|
- webSocketMap.remove(userId);
|
|
|
- webSocketMap.put(userId, this);
|
|
|
- //加入set中
|
|
|
- } else {
|
|
|
- webSocketMap.put(userId, this);
|
|
|
- //加入set中
|
|
|
- addOnlineCount();
|
|
|
- //在线数加1
|
|
|
+ if (Objects.isNull(platform) || Objects.equals(platform, "")) {
|
|
|
+ throw new BusinessException(ExceptionResultEnum.PLATFORM_INVALID);
|
|
|
}
|
|
|
- log.info("用户连接:" + userId + ",当前在线人数为:" + getOnlineCount());
|
|
|
- try {
|
|
|
- sendMessage("连接成功");
|
|
|
- } catch (IOException e) {
|
|
|
- log.error("用户:" + userId + ",网络异常!!!!!!");
|
|
|
+ if (Objects.isNull(deviceId) || Objects.equals(deviceId, "")) {
|
|
|
+ throw new BusinessException(ExceptionResultEnum.DEVICE_ID_INVALID);
|
|
|
+ }
|
|
|
+ this.platform = platform;
|
|
|
+ this.deviceId = deviceId;
|
|
|
+ this.Authorization = Authorization;
|
|
|
+ this.time = time;
|
|
|
+ this.method = method;
|
|
|
+ final SignatureInfo info = SignatureInfo
|
|
|
+ .parse(Authorization);
|
|
|
+ if (Objects.nonNull(info) && info.getType() == SignatureType.TOKEN) {
|
|
|
+ String sessionId = info.getInvoker();
|
|
|
+ TBSession tbSession = (TBSession) redisUtil.getUserSession(sessionId);
|
|
|
+ if (info.validate(tbSession.getAccessToken()) && info.getTimestamp() < tbSession.getExpireTime().getTime()
|
|
|
+ && platform.equalsIgnoreCase(tbSession.getPlatform()) && Objects.equals(deviceId, tbSession.getDeviceId())) {
|
|
|
+// if (webSocketMap.containsKey(userId)) {
|
|
|
+// webSocketMap.remove(userId);
|
|
|
+// webSocketMap.put(userId, this);
|
|
|
+// //加入set中
|
|
|
+// } else {
|
|
|
+// webSocketMap.put(userId, this);
|
|
|
+// //加入set中
|
|
|
+// addOnlineCount();
|
|
|
+// //在线数加1
|
|
|
+// }
|
|
|
+// log.info("用户连接:" + userId + ",当前在线人数为:" + getOnlineCount());
|
|
|
+// try {
|
|
|
+// sendMessage("连接成功");
|
|
|
+// } catch (IOException e) {
|
|
|
+// log.error("用户:" + userId + ",网络异常!!!!!!");
|
|
|
+// }
|
|
|
+ } else {
|
|
|
+ throw new BusinessException(ExceptionResultEnum.AUTHORIZATION_ERROR);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ throw new BusinessException(ExceptionResultEnum.AUTHORIZATION_ERROR);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -73,12 +109,12 @@ public class WebSocketServer
|
|
|
*/
|
|
|
@OnClose
|
|
|
public void onClose() {
|
|
|
- if (webSocketMap.containsKey(userId)) {
|
|
|
- webSocketMap.remove(userId);
|
|
|
- //从set中删除
|
|
|
- subOnlineCount();
|
|
|
- }
|
|
|
- log.info("用户退出:" + userId + ",当前在线人数为:" + getOnlineCount());
|
|
|
+// if (webSocketMap.containsKey(userId)) {
|
|
|
+// webSocketMap.remove(userId);
|
|
|
+// //从set中删除
|
|
|
+// subOnlineCount();
|
|
|
+// }
|
|
|
+// log.info("用户退出:" + userId + ",当前在线人数为:" + getOnlineCount());
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -88,27 +124,27 @@ public class WebSocketServer
|
|
|
*/
|
|
|
@OnMessage
|
|
|
public void onMessage(String message, Session session) {
|
|
|
- log.info("用户消息:" + userId + ",报文:" + message);
|
|
|
- //可以群发消息
|
|
|
- //消息保存到数据库、redis
|
|
|
- if (StringUtils.isNotBlank(message)) {
|
|
|
- try {
|
|
|
- //解析发送的报文
|
|
|
- JSONObject jsonObject = JSON.parseObject(message);
|
|
|
- //追加发送人(防止串改)
|
|
|
- jsonObject.put("fromUserId", this.userId);
|
|
|
- String toUserId = jsonObject.getString("toUserId");
|
|
|
- //传送给对应toUserId用户的websocket
|
|
|
- if (StringUtils.isNotBlank(toUserId) && webSocketMap.containsKey(toUserId)) {
|
|
|
- webSocketMap.get(toUserId).sendMessage(jsonObject.toJSONString());
|
|
|
- } else {
|
|
|
- log.error("请求的userId:" + toUserId + "不在该服务器上");
|
|
|
- //否则不在这个服务器上,发送到mysql或者redis
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
+// log.info("用户消息:" + userId + ",报文:" + message);
|
|
|
+// //可以群发消息
|
|
|
+// //消息保存到数据库、redis
|
|
|
+// if (StringUtils.isNotBlank(message)) {
|
|
|
+// try {
|
|
|
+// //解析发送的报文
|
|
|
+// JSONObject jsonObject = JSON.parseObject(message);
|
|
|
+// //追加发送人(防止串改)
|
|
|
+// jsonObject.put("fromUserId", this.userId);
|
|
|
+// String toUserId = jsonObject.getString("toUserId");
|
|
|
+// //传送给对应toUserId用户的websocket
|
|
|
+// if (StringUtils.isNotBlank(toUserId) && webSocketMap.containsKey(toUserId)) {
|
|
|
+// webSocketMap.get(toUserId).sendMessage(jsonObject.toJSONString());
|
|
|
+// } else {
|
|
|
+// log.error("请求的userId:" + toUserId + "不在该服务器上");
|
|
|
+// //否则不在这个服务器上,发送到mysql或者redis
|
|
|
+// }
|
|
|
+// } catch (Exception e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+// }
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -117,8 +153,9 @@ public class WebSocketServer
|
|
|
*/
|
|
|
@OnError
|
|
|
public void onError(Session session, Throwable error) {
|
|
|
- log.error("用户错误:" + this.userId + ",原因:" + error.getMessage());
|
|
|
+// log.error("用户错误:" + this.userId + ",原因:" + error.getMessage());
|
|
|
error.printStackTrace();
|
|
|
+ throw new BusinessException(error.getMessage());
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -128,7 +165,6 @@ public class WebSocketServer
|
|
|
this.session.getBasicRemote().sendText(message);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
* 发送自定义消息
|
|
|
*/
|