Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix:User_Logout_NPE #153

Merged
merged 7 commits into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading