Skip to content

Commit

Permalink
Merge pull request #153 from cmgyqjj/fix_User_Logout_NPE
Browse files Browse the repository at this point in the history
fix:User_Logout_NPE
  • Loading branch information
crossoverJie authored Sep 21, 2024
2 parents 1128d0c + 54b815b commit 5bba820
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -75,8 +76,8 @@ public BaseResponse<NULLBody> 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> cimUserInfo = userInfoCacheService.loadUserInfoByUserId(groupReqVO.getUserId());
cimUserInfo.ifPresent(userInfo -> log.warn("过滤掉了发送者 userId={}", userInfo.toString()));
continue;
}

Expand Down Expand Up @@ -131,10 +132,12 @@ public BaseResponse<NULLBody> p2pRoute(@RequestBody P2PReqVO p2pRequest) throws
public BaseResponse<NULLBody> offLine(@RequestBody ChatReqVO groupReqVO) {
BaseResponse<NULLBody> res = new BaseResponse();

CIMUserInfo cimUserInfo = userInfoCacheService.loadUserInfoByUserId(groupReqVO.getUserId());
Optional<CIMUserInfo> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.crossoverjie.cim.common.pojo.CIMUserInfo;

import java.util.Optional;
import java.util.Set;

/**
Expand All @@ -19,7 +20,7 @@ public interface UserInfoCacheService {
* @return
* @throws Exception
*/
CIMUserInfo loadUserInfoByUserId(Long userId) ;
Optional<CIMUserInfo> loadUserInfoByUserId(Long userId) ;

/**
* 保存和检查用户登录情况
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -154,12 +155,18 @@ private void parseServerInfo(Map<Long, CIMServerResVO> 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> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -34,12 +35,12 @@ public class UserInfoCacheServiceImpl implements UserInfoCacheService {
private RedisTemplate<String,String> redisTemplate ;

@Override
public CIMUserInfo loadUserInfoByUserId(Long userId) {
public Optional<CIMUserInfo> loadUserInfoByUserId(Long userId) {

//优先从本地缓存获取
CIMUserInfo cimUserInfo = USER_INFO_MAP.get(userId);
if (cimUserInfo != null){
return cimUserInfo ;
return Optional.ofNullable(cimUserInfo);
}

//load redis
Expand All @@ -49,7 +50,7 @@ public CIMUserInfo loadUserInfoByUserId(Long userId) {
USER_INFO_MAP.put(userId,cimUserInfo) ;
}

return cimUserInfo;
return Optional.ofNullable(cimUserInfo);
}

@Override
Expand All @@ -71,8 +72,9 @@ public Set<CIMUserInfo> onlineUser() {
if (set == null){
set = new HashSet<>(64) ;
}
CIMUserInfo cimUserInfo = loadUserInfoByUserId(Long.valueOf(member)) ;
set.add(cimUserInfo) ;
Optional<CIMUserInfo> cimUserInfo = loadUserInfoByUserId(Long.valueOf(member)) ;

cimUserInfo.ifPresent(set::add);
}

return set;
Expand Down

0 comments on commit 5bba820

Please sign in to comment.