diff --git a/cim-client-sdk/src/test/java/com/crossoverjie/cim/client/sdk/ClientTest.java b/cim-client-sdk/src/test/java/com/crossoverjie/cim/client/sdk/ClientTest.java index 7685698f..bc51afaf 100644 --- a/cim-client-sdk/src/test/java/com/crossoverjie/cim/client/sdk/ClientTest.java +++ b/cim-client-sdk/src/test/java/com/crossoverjie/cim/client/sdk/ClientTest.java @@ -395,4 +395,26 @@ public void testClose() throws Exception { super.stopSingle(); } + @Test + public void testIncorrectUser() throws Exception { + super.starSingleServer(); + super.startRoute(); + String routeUrl = "http://localhost:8083"; + String cj = "xx"; + long id = 100L; + var auth1 = ClientConfigurationData.Auth.builder() + .userId(id) + .userName(cj) + .build(); + + Client client1 = Client.builder() + .auth(auth1) + .routeUrl(routeUrl) + .build(); + TimeUnit.SECONDS.sleep(3); + + Assertions.assertDoesNotThrow(client1::close); + + super.stopSingle(); + } } \ No newline at end of file diff --git a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/controller/RouteController.java b/cim-forward-route/src/main/java/com/crossoverjie/cim/route/controller/RouteController.java index e655473e..28c54bce 100644 --- a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/controller/RouteController.java +++ b/cim-forward-route/src/main/java/com/crossoverjie/cim/route/controller/RouteController.java @@ -23,6 +23,7 @@ import jakarta.annotation.Resource; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Set; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; @@ -75,8 +76,8 @@ public BaseResponse groupRoute(@RequestBody ChatReqVO groupReqVO) thro CIMServerResVO cimServerResVO = cimServerResVOEntry.getValue(); if (userId.equals(groupReqVO.getUserId())) { //过滤掉自己 - CIMUserInfo cimUserInfo = userInfoCacheService.loadUserInfoByUserId(groupReqVO.getUserId()); - log.warn("过滤掉了发送者 userId={}", cimUserInfo.toString()); + Optional cimUserInfo = userInfoCacheService.loadUserInfoByUserId(groupReqVO.getUserId()); + cimUserInfo.ifPresent(userInfo -> log.warn("过滤掉了发送者 userId={}", userInfo.toString())); continue; } @@ -131,10 +132,12 @@ public BaseResponse p2pRoute(@RequestBody P2PReqVO p2pRequest) throws public BaseResponse offLine(@RequestBody ChatReqVO groupReqVO) { BaseResponse res = new BaseResponse(); - CIMUserInfo cimUserInfo = userInfoCacheService.loadUserInfoByUserId(groupReqVO.getUserId()); + Optional cimUserInfo = userInfoCacheService.loadUserInfoByUserId(groupReqVO.getUserId()); - log.info("user [{}] offline!", cimUserInfo.toString()); - accountService.offLine(groupReqVO.getUserId()); + cimUserInfo.ifPresent(userInfo -> { + log.info("user [{}] offline!", userInfo.toString()); + accountService.offLine(groupReqVO.getUserId()); + }); res.setCode(StatusEnum.SUCCESS.getCode()); res.setMessage(StatusEnum.SUCCESS.getMessage()); diff --git a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/service/UserInfoCacheService.java b/cim-forward-route/src/main/java/com/crossoverjie/cim/route/service/UserInfoCacheService.java index 7e71d8e6..a0c57a42 100644 --- a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/service/UserInfoCacheService.java +++ b/cim-forward-route/src/main/java/com/crossoverjie/cim/route/service/UserInfoCacheService.java @@ -2,6 +2,7 @@ import com.crossoverjie.cim.common.pojo.CIMUserInfo; +import java.util.Optional; import java.util.Set; /** @@ -19,7 +20,7 @@ public interface UserInfoCacheService { * @return * @throws Exception */ - CIMUserInfo loadUserInfoByUserId(Long userId) ; + Optional loadUserInfoByUserId(Long userId) ; /** * 保存和检查用户登录情况 diff --git a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/service/impl/AccountServiceRedisImpl.java b/cim-forward-route/src/main/java/com/crossoverjie/cim/route/service/impl/AccountServiceRedisImpl.java index 1c7104c1..39b7a314 100644 --- a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/service/impl/AccountServiceRedisImpl.java +++ b/cim-forward-route/src/main/java/com/crossoverjie/cim/route/service/impl/AccountServiceRedisImpl.java @@ -32,6 +32,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.Map; +import java.util.Optional; import static com.crossoverjie.cim.common.enums.StatusEnum.OFF_LINE; import static com.crossoverjie.cim.route.constant.Constant.*; @@ -154,12 +155,18 @@ private void parseServerInfo(Map routes, String key) { @Override public void pushMsg(CIMServerResVO cimServerResVO, long sendUserId, ChatReqVO groupReqVO) throws Exception { - CIMUserInfo cimUserInfo = userInfoCacheService.loadUserInfoByUserId(sendUserId); - - String url = "http://" + cimServerResVO.getIp() + ":" + cimServerResVO.getHttpPort(); - - SendMsgReqVO vo = new SendMsgReqVO(cimUserInfo.getUserName() + ":" + groupReqVO.getMsg(), groupReqVO.getUserId()); - serverApi.sendMsg(vo, url); + Optional cimUserInfo = userInfoCacheService.loadUserInfoByUserId(sendUserId); + + cimUserInfo.ifPresent(userInfo -> { + String url = "http://" + cimServerResVO.getIp() + ":" + cimServerResVO.getHttpPort(); + SendMsgReqVO vo = new SendMsgReqVO(userInfo.getUserName() + ":" + groupReqVO.getMsg(), groupReqVO.getUserId()); + try { + serverApi.sendMsg(vo, url); + } catch (Exception e) { + log.error("Error sending message", e); + throw new RuntimeException("Error sending message", e); + } + }); } @Override diff --git a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/service/impl/UserInfoCacheServiceImpl.java b/cim-forward-route/src/main/java/com/crossoverjie/cim/route/service/impl/UserInfoCacheServiceImpl.java index 6f709a45..5f32e585 100644 --- a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/service/impl/UserInfoCacheServiceImpl.java +++ b/cim-forward-route/src/main/java/com/crossoverjie/cim/route/service/impl/UserInfoCacheServiceImpl.java @@ -8,6 +8,7 @@ import java.util.HashSet; import java.util.Map; +import java.util.Optional; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; @@ -34,12 +35,12 @@ public class UserInfoCacheServiceImpl implements UserInfoCacheService { private RedisTemplate redisTemplate ; @Override - public CIMUserInfo loadUserInfoByUserId(Long userId) { + public Optional loadUserInfoByUserId(Long userId) { //优先从本地缓存获取 CIMUserInfo cimUserInfo = USER_INFO_MAP.get(userId); if (cimUserInfo != null){ - return cimUserInfo ; + return Optional.ofNullable(cimUserInfo); } //load redis @@ -49,7 +50,7 @@ public CIMUserInfo loadUserInfoByUserId(Long userId) { USER_INFO_MAP.put(userId,cimUserInfo) ; } - return cimUserInfo; + return Optional.ofNullable(cimUserInfo); } @Override @@ -71,8 +72,9 @@ public Set onlineUser() { if (set == null){ set = new HashSet<>(64) ; } - CIMUserInfo cimUserInfo = loadUserInfoByUserId(Long.valueOf(member)) ; - set.add(cimUserInfo) ; + Optional cimUserInfo = loadUserInfoByUserId(Long.valueOf(member)) ; + + cimUserInfo.ifPresent(set::add); } return set;