|
|
@@ -2,11 +2,19 @@ package cn.reghao.tnb.message.app.ws;
|
|
|
|
|
|
import cn.reghao.jutil.jdk.serializer.JdkSerializer;
|
|
|
import cn.reghao.jutil.jdk.serializer.JsonConverter;
|
|
|
+import cn.reghao.tnb.account.api.constant.TokenType;
|
|
|
+import cn.reghao.tnb.account.api.dto.AuthedAccount;
|
|
|
+import cn.reghao.tnb.account.api.iface.AccountQuery;
|
|
|
import cn.reghao.tnb.message.api.dto.PushMsg;
|
|
|
import cn.reghao.tnb.message.api.dto.event.EventMsg;
|
|
|
import cn.reghao.tnb.message.api.model.EventType;
|
|
|
import cn.reghao.tnb.message.api.model.resp.EventMessageResp;
|
|
|
+import cn.reghao.tnb.message.app.rabbit.RabbitProducer;
|
|
|
+import cn.reghao.tnb.message.app.ws.msg.ChatPayload;
|
|
|
+import cn.reghao.tnb.message.app.ws.msg.EventMessage;
|
|
|
+import cn.reghao.tnb.message.app.ws.msg.PingPayload;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.dubbo.config.annotation.DubboReference;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.web.socket.BinaryMessage;
|
|
|
import org.springframework.web.socket.TextMessage;
|
|
|
@@ -24,9 +32,31 @@ import java.util.Map;
|
|
|
@Slf4j
|
|
|
@Component
|
|
|
public class WsConnection {
|
|
|
+ @DubboReference(check = false)
|
|
|
+ private AccountQuery accountQuery;
|
|
|
+ private final RabbitProducer rabbitProducer;
|
|
|
+ // userId -> session
|
|
|
private final Map<String, WebSocketSession> userSessions = new HashMap<>();
|
|
|
+ // sessionId -> userId
|
|
|
private final Map<String, String> sessionUsers = new HashMap<>();
|
|
|
|
|
|
+ public WsConnection(RabbitProducer rabbitProducer) {
|
|
|
+ this.rabbitProducer = rabbitProducer;
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean addSession(WebSocketSession webSocketSession) {
|
|
|
+ String query = webSocketSession.getUri().getQuery();
|
|
|
+ String jwtToken = query.replace("token=", "");
|
|
|
+ AuthedAccount authedAccount = accountQuery.getAuthedAccount(TokenType.token.getValue(), jwtToken);
|
|
|
+ if (authedAccount != null) {
|
|
|
+ long userId = authedAccount.getUserId();
|
|
|
+ addUserSession(userId+"", webSocketSession);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
public void addUserSession(String userId, WebSocketSession webSocketSession) {
|
|
|
String sessionId = webSocketSession.getId();
|
|
|
userSessions.put(userId, webSocketSession);
|
|
|
@@ -44,6 +74,26 @@ public class WsConnection {
|
|
|
return sessionUsers.get(sessionId);
|
|
|
}
|
|
|
|
|
|
+ public WebSocketSession getUserSession(long userId) {
|
|
|
+ return userSessions.get(userId+"");
|
|
|
+ }
|
|
|
+
|
|
|
+ public void sendPingPayload(WebSocketSession session) throws IOException {
|
|
|
+ PingPayload pingPayload = new PingPayload(60, 300);
|
|
|
+ EventMessage eventMessage = new EventMessage();
|
|
|
+ eventMessage.setEvent("connect");
|
|
|
+ eventMessage.setPayload(pingPayload);
|
|
|
+
|
|
|
+ TextMessage textMessage = new TextMessage(JsonConverter.objectToJson(pingPayload));
|
|
|
+ session.sendMessage(textMessage);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void sendHeartbeatPong(WebSocketSession session) throws IOException {
|
|
|
+ EventMessageResp<String> resp = new EventMessageResp<>(EventType.heartbeat, "pong");
|
|
|
+ TextMessage textMessage = new TextMessage(JsonConverter.objectToJson(resp));
|
|
|
+ session.sendMessage(textMessage);
|
|
|
+ }
|
|
|
+
|
|
|
public void sendTextMsg(PushMsg pushMsg) throws IOException {
|
|
|
String userId = pushMsg.getReceiverId();
|
|
|
EventMsg eventMsg = pushMsg.getEventMsg();
|
|
|
@@ -58,10 +108,10 @@ public class WsConnection {
|
|
|
webSocketSession.sendMessage(textMessage);
|
|
|
}
|
|
|
|
|
|
- public void send(String userId, String content) throws IOException {
|
|
|
- WebSocketSession webSocketSession = userSessions.get(userId);
|
|
|
+ public void send(String receiverId, String content) throws IOException {
|
|
|
+ WebSocketSession webSocketSession = userSessions.get(receiverId);
|
|
|
if (webSocketSession == null) {
|
|
|
- log.error("{} 不在线", userId);
|
|
|
+ log.error("{} 不在线", receiverId);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
@@ -70,10 +120,10 @@ public class WsConnection {
|
|
|
webSocketSession.sendMessage(textMessage);
|
|
|
}
|
|
|
|
|
|
- public void send1(String userId, Object object) throws IOException {
|
|
|
- WebSocketSession webSocketSession = userSessions.get(userId);
|
|
|
+ public void send1(String receiverId, Object object) throws IOException {
|
|
|
+ WebSocketSession webSocketSession = userSessions.get(receiverId);
|
|
|
if (webSocketSession == null) {
|
|
|
- log.error("{} 不在线", userId);
|
|
|
+ log.error("{} 不在线", receiverId);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
@@ -81,8 +131,8 @@ public class WsConnection {
|
|
|
webSocketSession.sendMessage(textMessage);
|
|
|
}
|
|
|
|
|
|
- public void send(String dest, Object message) throws IOException {
|
|
|
- WebSocketSession session = userSessions.get(dest);
|
|
|
+ public void send(String receiverId, Object message) throws IOException {
|
|
|
+ WebSocketSession session = userSessions.get(receiverId);
|
|
|
if (session != null) {
|
|
|
byte[] bytes = JdkSerializer.serialize(message);
|
|
|
BinaryMessage binaryMessage = new BinaryMessage(bytes);
|
|
|
@@ -90,8 +140,8 @@ public class WsConnection {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public void sendText(String dest, Object message) throws IOException {
|
|
|
- WebSocketSession session = userSessions.get(dest);
|
|
|
+ public void sendText(String receiverId, String message) throws IOException {
|
|
|
+ WebSocketSession session = userSessions.get(receiverId);
|
|
|
if (session != null) {
|
|
|
String json = JsonConverter.objectToJson(message);
|
|
|
TextMessage textMessage = new TextMessage(json);
|
|
|
@@ -99,6 +149,20 @@ public class WsConnection {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public void sendChatMessage(String receiverId, ChatPayload chatPayload) throws IOException {
|
|
|
+ WebSocketSession session = userSessions.get(receiverId);
|
|
|
+ if (session == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ EventMessage eventMessage = new EventMessage();
|
|
|
+ eventMessage.setEvent("event_talk");
|
|
|
+ eventMessage.setPayload(chatPayload);
|
|
|
+
|
|
|
+ TextMessage textMessage = new TextMessage(JsonConverter.objectToJson(eventMessage));
|
|
|
+ session.sendMessage(textMessage);
|
|
|
+ }
|
|
|
+
|
|
|
public void unicast() {
|
|
|
}
|
|
|
|
|
|
@@ -122,4 +186,8 @@ public class WsConnection {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ public void clusterBroadcast(String payload) {
|
|
|
+ rabbitProducer.sendChatMessage(payload);
|
|
|
+ }
|
|
|
}
|