|
@@ -10,6 +10,7 @@ import com.zanxiang.game.module.manage.pojo.params.KfWebSocketMsgParam;
|
|
import com.zanxiang.game.module.manage.service.IKfRoomService;
|
|
import com.zanxiang.game.module.manage.service.IKfRoomService;
|
|
import com.zanxiang.game.module.manage.service.IKfSessionUserService;
|
|
import com.zanxiang.game.module.manage.service.IKfSessionUserService;
|
|
import com.zanxiang.game.module.mybatis.entity.KfRoom;
|
|
import com.zanxiang.game.module.mybatis.entity.KfRoom;
|
|
|
|
+import com.zanxiang.game.module.mybatis.entity.KfSessionUser;
|
|
import com.zanxiang.module.util.JsonUtil;
|
|
import com.zanxiang.module.util.JsonUtil;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.apache.logging.log4j.util.Strings;
|
|
import org.apache.logging.log4j.util.Strings;
|
|
@@ -64,6 +65,7 @@ public class KfMsgWebsocketHandler implements WebSocketHandler {
|
|
log.error("收到前端消息 msgStr : {}", msgStr);
|
|
log.error("收到前端消息 msgStr : {}", msgStr);
|
|
//消息解析
|
|
//消息解析
|
|
KfWebSocketMsgParam kfWebSocketMsgParam = JsonUtil.toObj(msgStr, KfWebSocketMsgParam.class);
|
|
KfWebSocketMsgParam kfWebSocketMsgParam = JsonUtil.toObj(msgStr, KfWebSocketMsgParam.class);
|
|
|
|
+ log.error("收到前端消息解析结果 kfWebSocketMsgParam : {}", JsonUtil.toString(kfWebSocketMsgParam));
|
|
//消息类型
|
|
//消息类型
|
|
KfWebSocketMsgEnum webSocketMsgType = kfWebSocketMsgParam.getWebSocketMsgType();
|
|
KfWebSocketMsgEnum webSocketMsgType = kfWebSocketMsgParam.getWebSocketMsgType();
|
|
//请求令牌
|
|
//请求令牌
|
|
@@ -72,29 +74,27 @@ public class KfMsgWebsocketHandler implements WebSocketHandler {
|
|
boolean tokenCheckResult = this.tokenCheck(webSocketMsgType, token);
|
|
boolean tokenCheckResult = this.tokenCheck(webSocketMsgType, token);
|
|
//身份验证失败, 没有后续动作
|
|
//身份验证失败, 没有后续动作
|
|
if (!tokenCheckResult) {
|
|
if (!tokenCheckResult) {
|
|
|
|
+ log.error("token验证失败, token : {}", token);
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
- //将token设置到当前线程
|
|
|
|
- SecurityUtil.fillToken(token);
|
|
|
|
- //添加会话
|
|
|
|
- Long kfUserId = SecurityUtil.getUserId();
|
|
|
|
- session.getAttributes().put("kfUserId", kfUserId);
|
|
|
|
- kfMsgWebSocketSessionRegistry.addSession(kfUserId, session);
|
|
|
|
- //客服创建连接-消息处理
|
|
|
|
|
|
+ //握手-消息处理
|
|
|
|
+ if (Objects.equals(kfWebSocketMsgParam.getWebSocketMsgType(), KfWebSocketMsgEnum.WEBSOCKET_MSG_KF_HAND_SHAKE)) {
|
|
|
|
+ log.error("首次握手, kfUserId : {}", SecurityUtil.getUserId());
|
|
|
|
+ this.kfHandShake(session, webSocketMsgType);
|
|
|
|
+ }
|
|
|
|
+ //创建连接-消息处理
|
|
if (Objects.equals(kfWebSocketMsgParam.getWebSocketMsgType(), KfWebSocketMsgEnum.WEBSOCKET_MSG_KF_CREATE_CONNECT)) {
|
|
if (Objects.equals(kfWebSocketMsgParam.getWebSocketMsgType(), KfWebSocketMsgEnum.WEBSOCKET_MSG_KF_CREATE_CONNECT)) {
|
|
- this.kfConnect(kfWebSocketMsgParam);
|
|
|
|
|
|
+ log.error("创建连接, kfUserId : {}", SecurityUtil.getUserId());
|
|
|
|
+ this.kfCreateConnect(webSocketMsgType);
|
|
}
|
|
}
|
|
//玩家接入-消息处理
|
|
//玩家接入-消息处理
|
|
if (Objects.equals(kfWebSocketMsgParam.getWebSocketMsgType(), KfWebSocketMsgEnum.WEBSOCKET_MSG_USER_CONNECT_JOIN)) {
|
|
if (Objects.equals(kfWebSocketMsgParam.getWebSocketMsgType(), KfWebSocketMsgEnum.WEBSOCKET_MSG_USER_CONNECT_JOIN)) {
|
|
-
|
|
|
|
|
|
+ log.error("玩家接入, kfUserId : {}", SecurityUtil.getUserId());
|
|
|
|
+ this.userConnectJoin(kfWebSocketMsgParam);
|
|
}
|
|
}
|
|
//获取房间历史消息-消息处理
|
|
//获取房间历史消息-消息处理
|
|
if (Objects.equals(kfWebSocketMsgParam.getWebSocketMsgType(), KfWebSocketMsgEnum.WEBSOCKET_MSG_ROOM_HISTORY)) {
|
|
if (Objects.equals(kfWebSocketMsgParam.getWebSocketMsgType(), KfWebSocketMsgEnum.WEBSOCKET_MSG_ROOM_HISTORY)) {
|
|
-
|
|
|
|
- }
|
|
|
|
- //客服发送消息-消息处理
|
|
|
|
- if (Objects.equals(kfWebSocketMsgParam.getWebSocketMsgType(), KfWebSocketMsgEnum.WEBSOCKET_MSG_KF_SEND)) {
|
|
|
|
-
|
|
|
|
|
|
+ log.error("获取房间历史消息, kfUserId : {}", SecurityUtil.getUserId());
|
|
}
|
|
}
|
|
//客服发送消息-消息处理
|
|
//客服发送消息-消息处理
|
|
if (Objects.equals(kfWebSocketMsgParam.getWebSocketMsgType(), KfWebSocketMsgEnum.WEBSOCKET_MSG_KF_SEND)) {
|
|
if (Objects.equals(kfWebSocketMsgParam.getWebSocketMsgType(), KfWebSocketMsgEnum.WEBSOCKET_MSG_KF_SEND)) {
|
|
@@ -144,39 +144,86 @@ public class KfMsgWebsocketHandler implements WebSocketHandler {
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
- private boolean tokenCheck(KfWebSocketMsgEnum webSocketMsgType, String token) {
|
|
|
|
- //令牌为空
|
|
|
|
- if (Strings.isBlank(token)) {
|
|
|
|
- this.pushMessage(KfWebSocketMsgDTO.fail(webSocketMsgType, "非法参数, token令牌不可为空"));
|
|
|
|
- return Boolean.FALSE;
|
|
|
|
|
|
+ private void msgRoomHistory(KfWebSocketMsgParam param) {
|
|
|
|
+ if (param.getRoomId() == null || param.getPage() == null) {
|
|
|
|
+ this.pushMessage(KfWebSocketMsgDTO.fail(param.getWebSocketMsgType(),
|
|
|
|
+ "获取房间历史消息参数错误, roomId和page都不可为空, param : " + JsonUtil.toString(param)));
|
|
}
|
|
}
|
|
- //令牌验证
|
|
|
|
- TokenInfo tokenInfo = SecurityUtil.parseToken(token);
|
|
|
|
- if (tokenInfo == null) {
|
|
|
|
- this.pushMessage(KfWebSocketMsgDTO.fail(webSocketMsgType, "参数错误, 令牌验证不通过"));
|
|
|
|
- return Boolean.FALSE;
|
|
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void userConnectJoin(KfWebSocketMsgParam param) {
|
|
|
|
+ if (Strings.isBlank(param.getOpenId()) || param.getGameId() == null) {
|
|
|
|
+ this.pushMessage(KfWebSocketMsgDTO.fail(param.getWebSocketMsgType(),
|
|
|
|
+ "接入玩家参数错误, openId和gameId都不可为空, param : " + JsonUtil.toString(param)));
|
|
}
|
|
}
|
|
- //返回验证通过
|
|
|
|
- return Boolean.TRUE;
|
|
|
|
|
|
+ //玩家更新
|
|
|
|
+ kfSessionUserService.update(new LambdaUpdateWrapper<KfSessionUser>()
|
|
|
|
+ .set(KfSessionUser::getIsWait, Boolean.FALSE)
|
|
|
|
+ .set(KfSessionUser::getUpdateTime, LocalDateTime.now()));
|
|
|
|
+ //房间更新
|
|
|
|
+ kfRoomService.userJoinRoom(param.getOpenId(), param.getGameId());
|
|
|
|
+ //发送消息, 给该客服返回完整的已接入房间列表
|
|
|
|
+ List<KfWebSocketMsgDTO.RoomBean> roomList = kfRoomService.getOnlineRoomList();
|
|
|
|
+ this.pushMessage(KfWebSocketMsgDTO.builder()
|
|
|
|
+ .webSocketMsgType(param.getWebSocketMsgType())
|
|
|
|
+ .kfUserId(SecurityUtil.getUserId())
|
|
|
|
+ .roomList(roomList)
|
|
|
|
+ .build());
|
|
|
|
+ //发送消息, 给所有在线客服推送完整待接入列表
|
|
|
|
+ List<KfWebSocketMsgDTO.WaitUserBean> waitUserList = kfSessionUserService.getWaitUserList();
|
|
|
|
+ this.pushMessage(KfWebSocketMsgDTO.builder()
|
|
|
|
+ .webSocketMsgType(param.getWebSocketMsgType())
|
|
|
|
+ .waitUserList(waitUserList)
|
|
|
|
+ .build());
|
|
}
|
|
}
|
|
|
|
|
|
- private void kfConnect(KfWebSocketMsgParam kfWebSocketMsgParam) {
|
|
|
|
- //获取游戏列表
|
|
|
|
- List<KfWebSocketMsgDTO.GameBean> gameList = kfRoomService.getKfGameList();
|
|
|
|
|
|
+ private void kfCreateConnect(KfWebSocketMsgEnum msgTypeEnum) {
|
|
//获取待接入列表
|
|
//获取待接入列表
|
|
List<KfWebSocketMsgDTO.WaitUserBean> waitUserList = kfSessionUserService.getWaitUserList();
|
|
List<KfWebSocketMsgDTO.WaitUserBean> waitUserList = kfSessionUserService.getWaitUserList();
|
|
//获取已接入房间列表
|
|
//获取已接入房间列表
|
|
List<KfWebSocketMsgDTO.RoomBean> roomList = kfRoomService.getOnlineRoomList();
|
|
List<KfWebSocketMsgDTO.RoomBean> roomList = kfRoomService.getOnlineRoomList();
|
|
//发送消息
|
|
//发送消息
|
|
this.pushMessage(KfWebSocketMsgDTO.builder()
|
|
this.pushMessage(KfWebSocketMsgDTO.builder()
|
|
- .webSocketMsgType(kfWebSocketMsgParam.getWebSocketMsgType())
|
|
|
|
|
|
+ .webSocketMsgType(msgTypeEnum)
|
|
.kfUserId(SecurityUtil.getUserId())
|
|
.kfUserId(SecurityUtil.getUserId())
|
|
- .gameList(gameList)
|
|
|
|
.waitUserList(waitUserList)
|
|
.waitUserList(waitUserList)
|
|
.roomList(roomList)
|
|
.roomList(roomList)
|
|
.build());
|
|
.build());
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ private void kfHandShake(WebSocketSession session, KfWebSocketMsgEnum msgTypeEnum) {
|
|
|
|
+ //添加会话
|
|
|
|
+ Long kfUserId = SecurityUtil.getUserId();
|
|
|
|
+ session.getAttributes().put("kfUserId", kfUserId);
|
|
|
|
+ kfMsgWebSocketSessionRegistry.addSession(kfUserId, session);
|
|
|
|
+ //获取游戏列表
|
|
|
|
+ List<KfWebSocketMsgDTO.GameBean> gameList = kfRoomService.getKfGameList();
|
|
|
|
+ //发送消息
|
|
|
|
+ this.pushMessage(KfWebSocketMsgDTO.builder()
|
|
|
|
+ .webSocketMsgType(msgTypeEnum)
|
|
|
|
+ .kfUserId(SecurityUtil.getUserId())
|
|
|
|
+ .gameList(gameList)
|
|
|
|
+ .build());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private boolean tokenCheck(KfWebSocketMsgEnum webSocketMsgType, String token) {
|
|
|
|
+ //令牌为空
|
|
|
|
+ if (Strings.isBlank(token)) {
|
|
|
|
+ this.pushMessage(KfWebSocketMsgDTO.fail(webSocketMsgType, "非法参数, token令牌不可为空"));
|
|
|
|
+ return Boolean.FALSE;
|
|
|
|
+ }
|
|
|
|
+ //令牌验证
|
|
|
|
+ TokenInfo tokenInfo = SecurityUtil.parseToken(token);
|
|
|
|
+ if (tokenInfo == null) {
|
|
|
|
+ this.pushMessage(KfWebSocketMsgDTO.fail(webSocketMsgType, "参数错误, 令牌验证不通过"));
|
|
|
|
+ return Boolean.FALSE;
|
|
|
|
+ }
|
|
|
|
+ //将token设置到当前线程
|
|
|
|
+ SecurityUtil.fillToken(token);
|
|
|
|
+ //返回验证通过
|
|
|
|
+ return Boolean.TRUE;
|
|
|
|
+ }
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 消息发送到redis广播
|
|
* 消息发送到redis广播
|
|
*/
|
|
*/
|