fix: fixed bugs for runtime application
This commit is contained in:
@@ -1,8 +1,5 @@
|
||||
package xin.merlin.myplayerbackend.utils.websocket;
|
||||
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import jakarta.annotation.PreDestroy;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
@@ -30,7 +27,14 @@ public class CustomWebSocketHandler extends TextWebSocketHandler {
|
||||
|
||||
@Override
|
||||
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
|
||||
Integer userId = (Integer) session.getAttributes().get("userId");
|
||||
/*
|
||||
TODO:这里需要处理在数据库中未发送的离线消息
|
||||
初步想法:
|
||||
开多线程,分页获取所有相关信息,然后送入rabbitmq,送入之后删除对应的message
|
||||
|
||||
但是需要处理的是,用户一上线即下线的问题,如何终止这个多线程任务以及送入rabbitmq的message可能又会回到数据库中并出现重复数据
|
||||
*/
|
||||
Integer userId = (Integer) session.getAttributes().get("id");
|
||||
|
||||
onlineStatusService.online(userId);
|
||||
|
||||
@@ -64,7 +68,7 @@ public class CustomWebSocketHandler extends TextWebSocketHandler {
|
||||
|
||||
@Override
|
||||
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
|
||||
Integer userId = (Integer) session.getAttributes().get("userId");
|
||||
Integer userId = (Integer) session.getAttributes().get("id");
|
||||
|
||||
onlineStatusService.offline(userId);
|
||||
|
||||
|
||||
@@ -22,17 +22,7 @@ public class WebSocketMessageConsumer {
|
||||
public void onMessage(String json) {
|
||||
try {
|
||||
JSONObject msg = JSON.parseObject(json);
|
||||
|
||||
String to = msg.getString("to");
|
||||
|
||||
if (to == null || to.isEmpty()) {
|
||||
// 这是广播 / 系统消息
|
||||
sessionManager.broadcast(json);
|
||||
} else {
|
||||
// 单发消息
|
||||
// sessionManager.sendToUser(Integer.valueOf(to), json);
|
||||
commandDispatcher.dispatch(msg);
|
||||
}
|
||||
commandDispatcher.dispatch(msg);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.info(e.getMessage());
|
||||
|
||||
@@ -15,6 +15,7 @@ public class CommandDispatcher {
|
||||
private final Typing typingCommand;
|
||||
private final Heartbeat heartbeatCommand;
|
||||
private final SystemNotify systemNotifyCommand;
|
||||
private final PersonalNotify personalNotifyCommand;
|
||||
|
||||
public void dispatch(JSONObject msg) throws IOException {
|
||||
String cmd = msg.getString("cmd");
|
||||
@@ -25,6 +26,7 @@ public class CommandDispatcher {
|
||||
case "TYPING" -> typingCommand.handle(msg);
|
||||
case "HEARTBEAT" -> heartbeatCommand.handle(msg);
|
||||
case "SYSTEM_NOTIFY" -> systemNotifyCommand.handle(msg);
|
||||
case "PERSONAL_NOTIFY" -> personalNotifyCommand.handle(msg);
|
||||
|
||||
default -> {
|
||||
System.err.println("Unknown command: " + cmd);
|
||||
|
||||
@@ -16,6 +16,9 @@ public class Message implements BaseCommandHandler {
|
||||
|
||||
@Override
|
||||
public void handle(JSONObject msg) throws IOException {
|
||||
/*
|
||||
TODO: 这里需要处理如果目标用户不在线,如何去完成消息的存储
|
||||
*/
|
||||
String to = msg.getString("to");
|
||||
sessionManager.sendToUser(Integer.valueOf(to), msg.toJSONString());
|
||||
}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package xin.merlin.myplayerbackend.utils.websocket.command.impl;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Component;
|
||||
import xin.merlin.myplayerbackend.utils.websocket.WebSocketSessionManager;
|
||||
import xin.merlin.myplayerbackend.utils.websocket.command.BaseCommandHandler;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class PersonalNotify implements BaseCommandHandler {
|
||||
|
||||
private final WebSocketSessionManager sessionManager;
|
||||
|
||||
@Override
|
||||
public void handle(JSONObject msg) throws IOException {
|
||||
String to = msg.getString("to");
|
||||
sessionManager.sendToUser(Integer.valueOf(to), msg.toJSONString());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user