|
|
@@ -1,20 +1,13 @@
|
|
|
package cn.reghao.tnb.message.app.ws.service;
|
|
|
|
|
|
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.model.EventType;
|
|
|
import cn.reghao.tnb.message.api.model.resp.EventMessageResp;
|
|
|
import cn.reghao.tnb.message.api.model.resp.EvtLoginResp;
|
|
|
-import cn.reghao.tnb.message.app.rabbit.RabbitProducer;
|
|
|
import cn.reghao.tnb.message.app.ws.WsConnection;
|
|
|
import cn.reghao.tnb.message.app.ws.msg.ChatPayload;
|
|
|
-import cn.reghao.tnb.message.app.ws.msg.EventMessage;
|
|
|
import cn.reghao.tnb.user.api.iface.UserContactService;
|
|
|
-import com.google.gson.Gson;
|
|
|
-import com.google.gson.GsonBuilder;
|
|
|
-import com.google.gson.JsonObject;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.dubbo.config.annotation.DubboReference;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
@@ -22,9 +15,7 @@ import org.springframework.web.socket.TextMessage;
|
|
|
import org.springframework.web.socket.WebSocketSession;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
-import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
* @author reghao
|
|
|
@@ -38,29 +29,12 @@ public class ChatService {
|
|
|
@DubboReference(check = false, retries = 0)
|
|
|
private UserContactService userContactService;
|
|
|
|
|
|
- private final Map<String, Long> sessionMap = new HashMap<>();
|
|
|
- private final Map<Long, WebSocketSession> userMap = new HashMap<>();
|
|
|
- private final Gson gson = new GsonBuilder().create();
|
|
|
private final WsConnection wsConnection;
|
|
|
- private RabbitProducer rabbitProducer;
|
|
|
|
|
|
public ChatService(WsConnection wsConnection) {
|
|
|
this.wsConnection = wsConnection;
|
|
|
}
|
|
|
|
|
|
- public void online(WebSocketSession session) throws IOException {
|
|
|
- String query = session.getUri().getQuery();
|
|
|
- String jwtToken = query.replace("token=", "");
|
|
|
- AuthedAccount authedAccount = accountQuery.getAuthedAccount(TokenType.token.getValue(), jwtToken);
|
|
|
- if (authedAccount != null) {
|
|
|
- long userId = authedAccount.getUserId();
|
|
|
- onLoginEvent(userId, true);
|
|
|
- sessionMap.put(session.getId(), userId);
|
|
|
- userMap.put(userId, session);
|
|
|
- log.info("{} 在线", userId);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
public void onLoginEvent(long userId, boolean online) {
|
|
|
EvtLoginResp evtLoginResp = new EvtLoginResp(userId, online);
|
|
|
EventMessageResp<EvtLoginResp> resp = new EventMessageResp<>(EventType.event_login, evtLoginResp);
|
|
|
@@ -69,7 +43,7 @@ public class ChatService {
|
|
|
userContactService.setOnline(userId, online);
|
|
|
List<Long> friendIds = userContactService.getOnlineFriends(userId);
|
|
|
for (Long friendId : friendIds) {
|
|
|
- WebSocketSession session = userMap.get(friendId);
|
|
|
+ WebSocketSession session = wsConnection.getUserSession(friendId);
|
|
|
if (session != null) {
|
|
|
try {
|
|
|
session.sendMessage(textMessage);
|
|
|
@@ -88,6 +62,6 @@ public class ChatService {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- wsConnection.sendText(receiverId+"", payload);
|
|
|
+ wsConnection.sendChatMessage(receiverId+"", chatPayload);
|
|
|
}
|
|
|
}
|