feat: introduce middleware rabbitmq and redis
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
package xin.merlin.myplayerbackend.utils.websocket.command;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public interface BaseCommandHandler {
|
||||
void handle(JSONObject msg) throws IOException;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
package xin.merlin.myplayerbackend.utils.websocket.command;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Component;
|
||||
import xin.merlin.myplayerbackend.utils.websocket.command.impl.*;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class CommandDispatcher {
|
||||
private final Ping pingCommand;
|
||||
private final Message messageCommand;
|
||||
private final Typing typingCommand;
|
||||
private final Heartbeat heartbeatCommand;
|
||||
private final SystemNotify systemNotifyCommand;
|
||||
|
||||
public void dispatch(JSONObject msg) throws IOException {
|
||||
String cmd = msg.getString("cmd");
|
||||
|
||||
switch (cmd) {
|
||||
case "PING" -> pingCommand.handle(msg);
|
||||
case "MESSAGE" -> messageCommand.handle(msg);
|
||||
case "TYPING" -> typingCommand.handle(msg);
|
||||
case "HEARTBEAT" -> heartbeatCommand.handle(msg);
|
||||
case "SYSTEM_NOTIFY" -> systemNotifyCommand.handle(msg);
|
||||
|
||||
default -> {
|
||||
System.err.println("Unknown command: " + cmd);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package xin.merlin.myplayerbackend.utils.websocket.command.impl;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
import xin.merlin.myplayerbackend.utils.websocket.command.BaseCommandHandler;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class Heartbeat implements BaseCommandHandler {
|
||||
|
||||
private final RedisTemplate<String, String> redis;
|
||||
|
||||
@Override
|
||||
public void handle(JSONObject msg) {
|
||||
String userId = msg.getString("from");
|
||||
redis.opsForValue().set("online:" + userId, "1", Duration.ofMinutes(5));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
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 Message 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());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
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 Ping implements BaseCommandHandler {
|
||||
|
||||
private final WebSocketSessionManager sessionManager;
|
||||
|
||||
@Override
|
||||
public void handle(JSONObject msg) throws IOException {
|
||||
String from = msg.getString("from");
|
||||
msg.put("cmd", "PONG");
|
||||
sessionManager.sendToUser(Integer.valueOf(from), 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 SystemNotify implements BaseCommandHandler {
|
||||
|
||||
private final WebSocketSessionManager sessionManager;
|
||||
|
||||
@Override
|
||||
public void handle(JSONObject msg) throws IOException {
|
||||
sessionManager.broadcast(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 Typing 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