feat: introduce middleware rabbitmq and redis
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
package xin.merlin.myplayerbackend.utils.websocket;
|
||||
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
import xin.merlin.myplayerbackend.config.RabbitMQConfig;
|
||||
import xin.merlin.myplayerbackend.utils.websocket.command.CommandDispatcher;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class WebSocketMessageConsumer {
|
||||
|
||||
private final WebSocketSessionManager sessionManager;
|
||||
|
||||
private final CommandDispatcher commandDispatcher;
|
||||
|
||||
@RabbitListener(queues = RabbitMQConfig.WS_MESSAGE_QUEUE)
|
||||
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);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
log.info(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user