|
@@ -3,16 +3,21 @@ package com.zanxiang.game.module.manage.websocket;
|
|
|
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
import com.zanxiang.erp.base.pojo.TokenInfo;
|
|
|
import com.zanxiang.erp.security.util.SecurityUtil;
|
|
|
+import com.zanxiang.game.module.manage.constant.RedisKeyConstant;
|
|
|
import com.zanxiang.game.module.manage.enums.KfRoomMsgOwnerEnum;
|
|
|
+import com.zanxiang.game.module.manage.enums.KfRoomMsgTypeEnum;
|
|
|
import com.zanxiang.game.module.manage.enums.KfWebSocketMsgEnum;
|
|
|
+import com.zanxiang.game.module.manage.pojo.dto.GameAppletDTO;
|
|
|
import com.zanxiang.game.module.manage.pojo.dto.KfWebSocketMsgDTO;
|
|
|
import com.zanxiang.game.module.manage.pojo.params.KfWebSocketMsgParam;
|
|
|
+import com.zanxiang.game.module.manage.service.IGameAppletService;
|
|
|
import com.zanxiang.game.module.manage.service.IKfRoomMsgService;
|
|
|
import com.zanxiang.game.module.manage.service.IKfRoomService;
|
|
|
import com.zanxiang.game.module.manage.service.IKfSessionUserService;
|
|
|
import com.zanxiang.game.module.mybatis.entity.KfRoom;
|
|
|
import com.zanxiang.game.module.mybatis.entity.KfRoomMsg;
|
|
|
import com.zanxiang.game.module.mybatis.entity.KfSessionUser;
|
|
|
+import com.zanxiang.module.redis.service.IDistributedLockComponent;
|
|
|
import com.zanxiang.module.util.JsonUtil;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.logging.log4j.util.Strings;
|
|
@@ -24,8 +29,11 @@ import reactor.util.function.Tuple2;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.time.LocalDateTime;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
import java.util.Objects;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
/**
|
|
|
* @author : lingfeng
|
|
@@ -40,6 +48,9 @@ public class KfMsgWebsocketHandler implements WebSocketHandler {
|
|
|
@Autowired
|
|
|
private KfMsgWebSocketSessionRegistry kfMsgWebSocketSessionRegistry;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private IGameAppletService gameAppletService;
|
|
|
+
|
|
|
@Autowired
|
|
|
private IKfRoomService kfRoomService;
|
|
|
|
|
@@ -49,6 +60,9 @@ public class KfMsgWebsocketHandler implements WebSocketHandler {
|
|
|
@Autowired
|
|
|
private IKfRoomMsgService kfRoomMsgService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private IDistributedLockComponent distributedLockComponent;
|
|
|
+
|
|
|
/**
|
|
|
* websocket连接建立成功
|
|
|
*/
|
|
@@ -145,6 +159,37 @@ public class KfMsgWebsocketHandler implements WebSocketHandler {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
+ private void fkSendMsg(WebSocketSession session, KfWebSocketMsgParam param) {
|
|
|
+ //查询小程序信息
|
|
|
+ GameAppletDTO gameAppletDTO = gameAppletService.getByGameId(param.getGameId());
|
|
|
+ if (gameAppletDTO == null) {
|
|
|
+ this.sendMessage(session, KfWebSocketMsgDTO.fail(param.getWebSocketMsgType(),
|
|
|
+ "参数错误, 小程序信息不存在, param : " + JsonUtil.toString(param)));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //发送消息
|
|
|
+ KfWebSocketMsgParam.MsgContentBean msgContent = param.getMsgContent();
|
|
|
+ Map<String, Object> msgParamMap = new HashMap<>(3);
|
|
|
+ msgParamMap.put("touser", param.getOpenId());
|
|
|
+ //文本
|
|
|
+ if (Objects.equals(msgContent.getMsgType(), KfRoomMsgTypeEnum.KF_MSG_TYPE_TEXT)) {
|
|
|
+ Map<String, Object> textMap = new HashMap<>(1);
|
|
|
+ textMap.put("content", msgContent.getText());
|
|
|
+ msgParamMap.put("msgtype", KfRoomMsgTypeEnum.KF_MSG_TYPE_TEXT.getValue());
|
|
|
+ msgParamMap.put(KfRoomMsgTypeEnum.KF_MSG_TYPE_TEXT.getValue(), textMap);
|
|
|
+ }
|
|
|
+ //图片
|
|
|
+ if (Objects.equals(msgContent.getMsgType(), KfRoomMsgTypeEnum.KF_MSG_TYPE_IMAGE)) {
|
|
|
+
|
|
|
+ Map<String, Object> imageMap = new HashMap<>(1);
|
|
|
+ imageMap.put("media_id", msgContent.getText());
|
|
|
+ msgParamMap.put("msgtype", KfRoomMsgTypeEnum.KF_MSG_TYPE_IMAGE.getValue());
|
|
|
+ msgParamMap.put(KfRoomMsgTypeEnum.KF_MSG_TYPE_IMAGE.getValue(), imageMap);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
private void kfFinishSession(WebSocketSession session, KfWebSocketMsgEnum webSocketMsgType, Long gameId, Long roomId) {
|
|
|
//房间在线状态更新
|
|
|
kfRoomService.update(new LambdaUpdateWrapper<KfRoom>()
|
|
@@ -201,14 +246,21 @@ public class KfMsgWebsocketHandler implements WebSocketHandler {
|
|
|
private void userConnectJoin(WebSocketSession session, KfWebSocketMsgParam param) {
|
|
|
if (Strings.isBlank(param.getOpenId())) {
|
|
|
this.sendMessage(session, KfWebSocketMsgDTO.fail(param.getWebSocketMsgType(),
|
|
|
- "接入玩家参数错误, openId和gameId都不可为空, param : " + JsonUtil.toString(param)));
|
|
|
+ "接入玩家参数错误, openId不可为空, param : " + JsonUtil.toString(param)));
|
|
|
+ }
|
|
|
+ //触发玩家接入线程锁
|
|
|
+ if (!distributedLockComponent.doLock(RedisKeyConstant.KF_MSG_USER_CONNECT_JOIN + param.getOpenId(),
|
|
|
+ 0L, 1L, TimeUnit.MINUTES)) {
|
|
|
+ this.sendMessage(session, KfWebSocketMsgDTO.fail(param.getWebSocketMsgType(),
|
|
|
+ "玩家已被其他客服接入, param : " + JsonUtil.toString(param)));
|
|
|
+ return;
|
|
|
}
|
|
|
- //todo : 判断玩家是否已被接入
|
|
|
-
|
|
|
//玩家更新
|
|
|
kfSessionUserService.update(new LambdaUpdateWrapper<KfSessionUser>()
|
|
|
.set(KfSessionUser::getIsWait, Boolean.FALSE)
|
|
|
- .set(KfSessionUser::getUpdateTime, LocalDateTime.now()));
|
|
|
+ .set(KfSessionUser::getUpdateTime, LocalDateTime.now())
|
|
|
+ .eq(KfSessionUser::getOpenId, param.getOpenId())
|
|
|
+ );
|
|
|
//房间更新
|
|
|
Long roomId = kfRoomService.userJoinRoom(param.getOpenId(), param.getGameId());
|
|
|
//玩家未读消息更新到房间
|
|
@@ -267,7 +319,7 @@ public class KfMsgWebsocketHandler implements WebSocketHandler {
|
|
|
if (gameId == null) {
|
|
|
//心跳和握手消息, 不需要携带gameId
|
|
|
if (!Objects.equals(KfWebSocketMsgEnum.WEBSOCKET_MSG_CONNECT_HEART_BEAT, webSocketMsgType)
|
|
|
- || !Objects.equals(KfWebSocketMsgEnum.WEBSOCKET_MSG_KF_HAND_SHAKE, webSocketMsgType)) {
|
|
|
+ && !Objects.equals(KfWebSocketMsgEnum.WEBSOCKET_MSG_KF_HAND_SHAKE, webSocketMsgType)) {
|
|
|
this.sendMessage(session, KfWebSocketMsgDTO.fail(webSocketMsgType, "参数错误, gameId为空"));
|
|
|
return Boolean.FALSE;
|
|
|
}
|