|
|
@@ -2,8 +2,12 @@ package com.izouma.nineth.websocket;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.izouma.nineth.domain.PublicScreenChat;
|
|
|
-import com.izouma.nineth.dto.MetaWebsocketMessage;
|
|
|
+import com.izouma.nineth.domain.PurchaseLevel;
|
|
|
+import com.izouma.nineth.domain.User;
|
|
|
+import com.izouma.nineth.exception.BusinessException;
|
|
|
import com.izouma.nineth.repo.PublicScreenChatRepo;
|
|
|
+import com.izouma.nineth.repo.UserRepo;
|
|
|
+import com.izouma.nineth.service.PurchaseLevelService;
|
|
|
import com.izouma.nineth.utils.ApplicationContextUtil;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
@@ -32,10 +36,20 @@ public class PublicScreenChatWebsocket extends WebsocketCommon {
|
|
|
|
|
|
private final String PREFIX = "meta-chat:";
|
|
|
|
|
|
+ private UserRepo userRepo;
|
|
|
+
|
|
|
+ private PurchaseLevelService purchaseLevelService;
|
|
|
+
|
|
|
private void init() {
|
|
|
if (Objects.isNull(publicScreenChatRepo)) {
|
|
|
publicScreenChatRepo = (PublicScreenChatRepo) ApplicationContextUtil.getBean("publicScreenChatRepo");
|
|
|
}
|
|
|
+ if (Objects.isNull(userRepo)) {
|
|
|
+ userRepo = (UserRepo) ApplicationContextUtil.getBean("userRepo");
|
|
|
+ }
|
|
|
+ if (Objects.isNull(purchaseLevelService)) {
|
|
|
+ purchaseLevelService = (PurchaseLevelService) ApplicationContextUtil.getBean("purchaseLevelService");
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@OnOpen
|
|
|
@@ -43,29 +57,28 @@ public class PublicScreenChatWebsocket extends WebsocketCommon {
|
|
|
init();
|
|
|
// 判断当前玩家是否在其他地方登陆
|
|
|
if (clients.containsKey(PREFIX.concat(userId))) {
|
|
|
- String msg = String.format("玩家[%S]已在别处登陆,sessionId为[%S],正在为您关闭本连接", userId, session.getId());
|
|
|
- log.info(msg);
|
|
|
- // 关闭连接
|
|
|
- MetaWebsocketMessage message = new MetaWebsocketMessage();
|
|
|
- message.setType(3);
|
|
|
- message.setTip(msg);
|
|
|
- sendMessageTo(clients, JSON.toJSONString(message), PREFIX.concat(userId));
|
|
|
+ String msg = String.format("已在别处登陆,sessionId为[%S],正在为您关闭本连接", session.getId());
|
|
|
+ exceptionHandle(userId, msg);
|
|
|
try {
|
|
|
- log.info("关闭上次登陆的session连接");
|
|
|
+ log.info("关闭session连接");
|
|
|
clients.get(PREFIX.concat(userId)).close();
|
|
|
} catch (Exception e) {
|
|
|
- log.error("session close throw exception:", e);
|
|
|
+ exceptionHandle(userId, String.format("session close throw exception[%S]", e));
|
|
|
+ return;
|
|
|
}
|
|
|
}
|
|
|
log.info("现在来连接的sessionId:" + session.getId() + "玩家id:" + userId + "玩家昵称" + nickName);
|
|
|
clients.put(PREFIX.concat(userId), session);
|
|
|
- PublicScreenChat publicScreenChat = new PublicScreenChat();
|
|
|
- publicScreenChat.setFromNickName(nickName);
|
|
|
- publicScreenChat.setFromUserId(userId);
|
|
|
- publicScreenChat.setMessageInfo(String.format("玩家[%S][%S]进入大厅", userId, nickName));
|
|
|
- publicScreenChat.setTime(LocalDateTime.now());
|
|
|
- PublicScreenChat save = publicScreenChatRepo.save(publicScreenChat);
|
|
|
- sendMessageToOther(clients, JSON.toJSONString(save), PREFIX.concat(userId));
|
|
|
+ String format = String.format("玩家[%S][%S]进入大厅", userId, nickName);
|
|
|
+ PublicScreenChat publicScreenChat;
|
|
|
+ try {
|
|
|
+ publicScreenChat = savePublicScreenChat(userId, format);
|
|
|
+ } catch (Exception e) {
|
|
|
+ String errMsg = String.format("玩家进入大厅,保存信息发生异常[%S]", e);
|
|
|
+ exceptionHandle(userId, errMsg);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ sendMessageToOther(clients, JSON.toJSONString(publicScreenChat), PREFIX.concat(userId));
|
|
|
}
|
|
|
|
|
|
@OnError
|
|
|
@@ -78,42 +91,36 @@ public class PublicScreenChatWebsocket extends WebsocketCommon {
|
|
|
public void onClose(@PathParam("nickName") String nickName, @PathParam("userId") String userId, Session session) {
|
|
|
init();
|
|
|
log.info(String.format("sessionId:[%S] userId:[%S] is closed", session.getId(), userId));
|
|
|
- PublicScreenChat publicScreenChat = new PublicScreenChat();
|
|
|
- publicScreenChat.setFromNickName(nickName);
|
|
|
- publicScreenChat.setFromUserId(userId);
|
|
|
- publicScreenChat.setMessageInfo(String.format("玩家[%S][%S]离开大厅", userId, nickName));
|
|
|
- publicScreenChat.setTime(LocalDateTime.now());
|
|
|
- PublicScreenChat save = publicScreenChatRepo.save(publicScreenChat);
|
|
|
- sendMessageToOther(clients, JSON.toJSONString(save), PREFIX.concat(userId));
|
|
|
+ String format = String.format("玩家[%S][%S]离开大厅", userId, nickName);
|
|
|
+ PublicScreenChat publicScreenChat;
|
|
|
+ try {
|
|
|
+ publicScreenChat = savePublicScreenChat(userId, format);
|
|
|
+ } catch (Exception e) {
|
|
|
+ String errMsg = String.format("玩家离开大厅,保存信息发生异常[%S]", e);
|
|
|
+ exceptionHandle(userId, errMsg);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ sendMessageToOther(clients, JSON.toJSONString(publicScreenChat), PREFIX.concat(userId));
|
|
|
clients.remove(PREFIX.concat(userId));
|
|
|
}
|
|
|
|
|
|
@OnMessage
|
|
|
- public void onMessage(@PathParam("nickName") String nickName, @PathParam("userId") String userId, String message) {
|
|
|
+ public void onMessage(@PathParam("userId") String userId, String message) {
|
|
|
init();
|
|
|
if (StringUtils.isBlank(message)) {
|
|
|
- log.error("Illegal parameter : message can not be null");
|
|
|
+ String errMsg = "Illegal parameter : message can not be null";
|
|
|
+ exceptionHandle(userId, errMsg);
|
|
|
return;
|
|
|
}
|
|
|
- PublicScreenChat publicScreenChat = JSON.parseObject(message, PublicScreenChat.class);
|
|
|
- if (Objects.isNull(publicScreenChat)) {
|
|
|
- log.error("metaChatResultMessage can not be null");
|
|
|
+ PublicScreenChat publicScreenChat;
|
|
|
+ try {
|
|
|
+ publicScreenChat = savePublicScreenChat(userId, message);
|
|
|
+ } catch (Exception e) {
|
|
|
+ String errMsg = String.format("玩家发送消息,保存信息发生异常[%S]", e);
|
|
|
+ exceptionHandle(userId, errMsg);
|
|
|
return;
|
|
|
}
|
|
|
- if (StringUtils.isBlank(publicScreenChat.getMessageInfo())) {
|
|
|
- log.error("Illegal parameter : messageInfo can not be null");
|
|
|
- return;
|
|
|
- }
|
|
|
- if (StringUtils.isBlank(publicScreenChat.getFromNickName())) {
|
|
|
- publicScreenChat.setFromNickName(nickName);
|
|
|
- }
|
|
|
- if (StringUtils.isBlank(publicScreenChat.getFromUserId())) {
|
|
|
- publicScreenChat.setFromUserId(userId);
|
|
|
- }
|
|
|
- publicScreenChat.setTime(LocalDateTime.now());
|
|
|
- // 消息入库
|
|
|
- PublicScreenChat save = publicScreenChatRepo.save(publicScreenChat);
|
|
|
- sendMessageToAll(clients, JSON.toJSONString(save), PREFIX.concat(userId));
|
|
|
+ sendMessageToAll(clients, JSON.toJSONString(publicScreenChat), PREFIX.concat(userId));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -135,5 +142,29 @@ public class PublicScreenChatWebsocket extends WebsocketCommon {
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
+
|
|
|
+ private PublicScreenChat savePublicScreenChat(String userId, String messageInfo) {
|
|
|
+ User user = userRepo.findById(Long.parseLong(userId)).orElse(null);
|
|
|
+ if (Objects.isNull(user)) {
|
|
|
+ throw new BusinessException("用户信息不存在");
|
|
|
+ }
|
|
|
+ PublicScreenChat publicScreenChat = new PublicScreenChat();
|
|
|
+ publicScreenChat.setUserId(userId);
|
|
|
+ publicScreenChat.setAvatar(user.getAvatar());
|
|
|
+ publicScreenChat.setNickname(user.getNickname());
|
|
|
+ publicScreenChat.setLevel(user.getLevel());
|
|
|
+ PurchaseLevel purchaseLevel = purchaseLevelService.findPurchaseLevelByLevel(publicScreenChat.getLevel());
|
|
|
+ publicScreenChat.setRealm(purchaseLevel.getRealm());
|
|
|
+ publicScreenChat.setTitle(purchaseLevel.getTitle());
|
|
|
+ publicScreenChat.setMessageInfo(messageInfo);
|
|
|
+ publicScreenChat.setTime(LocalDateTime.now());
|
|
|
+ return publicScreenChatRepo.save(publicScreenChat);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void exceptionHandle(String userId, String msg) {
|
|
|
+ log.error(msg);
|
|
|
+ // 推送消息给该玩家
|
|
|
+ sendMessageTo(clients, JSON.toJSONString(msg), PREFIX.concat(userId));
|
|
|
+ }
|
|
|
}
|
|
|
|