作者 crossoverJie

:sparkles: Introducing new features.私聊

... ... @@ -30,7 +30,7 @@ public interface MsgHandle {
* @param p2PReqVO 私聊请求
* @throws Exception
*/
void p2pChat(P2PReqVO p2PReqVO) ;
void p2pChat(P2PReqVO p2PReqVO) throws Exception;
// TODO: 2018/12/26 后续对消息的处理可以优化为责任链模式
... ...
... ... @@ -2,6 +2,7 @@ package com.crossoverjie.cim.client.service;
import com.crossoverjie.cim.client.vo.req.GroupReqVO;
import com.crossoverjie.cim.client.vo.req.LoginReqVO;
import com.crossoverjie.cim.client.vo.req.P2PReqVO;
import com.crossoverjie.cim.client.vo.res.CIMServerResVO;
import com.crossoverjie.cim.client.vo.res.OnlineUsersResVO;
... ... @@ -23,9 +24,18 @@ public interface RouteRequest {
*/
void sendGroupMsg(GroupReqVO groupReqVO) throws Exception;
/**
* 私聊
* @param p2PReqVO
* @throws Exception
*/
void sendP2PMsg(P2PReqVO p2PReqVO)throws Exception;
/**
* 获取服务器
* @return 服务ip+port
* @param loginReqVO
* @throws Exception
*/
CIMServerResVO.ServerInfo getCIMServer(LoginReqVO loginReqVO) throws Exception;
... ... @@ -35,4 +45,6 @@ public interface RouteRequest {
* @return 获取所有在线用户
*/
List<OnlineUsersResVO.DataBodyBean> onlineUsers()throws Exception ;
}
... ...
... ... @@ -50,7 +50,11 @@ public class MsgHandler implements MsgHandle {
p2PReqVO.setUserId(configuration.getUserId());
p2PReqVO.setReceiveUserId(Long.parseLong(totalMsg[0]));
p2PReqVO.setMsg(totalMsg[1]);
p2pChat(p2PReqVO);
try {
p2pChat(p2PReqVO);
} catch (Exception e) {
LOGGER.error("Exception",e);
}
} else {
//群聊
... ... @@ -69,7 +73,9 @@ public class MsgHandler implements MsgHandle {
}
@Override
public void p2pChat(P2PReqVO p2PReqVO) {
public void p2pChat(P2PReqVO p2PReqVO) throws Exception {
routeRequest.sendP2PMsg(p2PReqVO);
}
... ...
... ... @@ -6,6 +6,7 @@ import com.crossoverjie.cim.client.config.AppConfiguration;
import com.crossoverjie.cim.client.service.RouteRequest;
import com.crossoverjie.cim.client.vo.req.GroupReqVO;
import com.crossoverjie.cim.client.vo.req.LoginReqVO;
import com.crossoverjie.cim.client.vo.req.P2PReqVO;
import com.crossoverjie.cim.client.vo.res.CIMServerResVO;
import com.crossoverjie.cim.client.vo.res.OnlineUsersResVO;
import com.crossoverjie.cim.common.enums.StatusEnum;
... ... @@ -39,6 +40,9 @@ public class RouteRequestImpl implements RouteRequest {
@Value("${cim.group.route.request.url}")
private String groupRouteRequestUrl;
@Value("${cim.p2p.route.request.url}")
private String p2pRouteRequestUrl;
@Value("${cim.server.route.request.url}")
private String serverRouteRequestUrl;
... ... @@ -70,6 +74,25 @@ public class RouteRequestImpl implements RouteRequest {
}
@Override
public void sendP2PMsg(P2PReqVO p2PReqVO) throws Exception {
JSONObject jsonObject = new JSONObject();
jsonObject.put("msg",p2PReqVO.getMsg());
jsonObject.put("userId",p2PReqVO.getUserId());
jsonObject.put("receiveUserId",p2PReqVO.getReceiveUserId());
RequestBody requestBody = RequestBody.create(mediaType,jsonObject.toString());
Request request = new Request.Builder()
.url(p2pRouteRequestUrl)
.post(requestBody)
.build();
Response response = okHttpClient.newCall(request).execute() ;
if (!response.isSuccessful()){
throw new IOException("Unexpected code " + response);
}
}
@Override
public CIMServerResVO.ServerInfo getCIMServer(LoginReqVO loginReqVO) throws Exception {
JSONObject jsonObject = new JSONObject();
... ...
... ... @@ -13,6 +13,9 @@ logging.level.root=info
# 群发消息
cim.group.route.request.url=http://45.78.28.220:8083/groupRoute
# 私聊消息
cim.p2p.route.request.url=http://45.78.28.220:8083/p2pRoute
# 登录并获取服务器ip+port
cim.server.route.request.url=http://45.78.28.220:8083/login
... ... @@ -23,7 +26,10 @@ cim.server.online.user.url=http://45.78.28.220:8083/onlineUser
###=======本地模拟======###
## 群发消息
#cim.group.route.request.url=http://localhost:8083/groupRoute
#
# 私聊消息
#cim.p2p.route.request.url=http://localhost:8083/p2pRoute
## 登录并获取服务器ip+port
#cim.server.route.request.url=http://localhost:8083/login
... ...
... ... @@ -80,16 +80,29 @@ public class RouteController {
return res;
}
@ApiOperation("客户端下线")
@RequestMapping(value = "offLine", method = RequestMethod.POST)
// TODO: 2018/12/26 这些基于 HTTP 接口的远程通信都可以换为 SpringCloud
/**
* 私聊路由
*
* @param p2pRequest
* @return
*/
@ApiOperation("私聊 API")
@RequestMapping(value = "p2pRoute", method = RequestMethod.POST)
@ResponseBody()
public BaseResponse<NULLBody> offLine(@RequestBody ChatReqVO groupReqVO) throws Exception {
public BaseResponse<NULLBody> p2pRoute(@RequestBody P2PReqVO p2pRequest) throws Exception {
BaseResponse<NULLBody> res = new BaseResponse();
CIMUserInfo cimUserInfo = userInfoCacheService.loadUserInfoByUserId(groupReqVO.getUserId());
//获取接收消息用户的路由信息
CIMServerResVO cimServerResVO = accountService.loadRouteRelatedByUserId(p2pRequest.getReceiveUserId());
//推送消息
String url = "http://" + cimServerResVO.getIp() + ":" + cimServerResVO.getHttpPort() + "/sendMsg" ;
//p2pRequest.getReceiveUserId()==>消息接收者的 userID
ChatReqVO chatVO = new ChatReqVO(p2pRequest.getReceiveUserId(),p2pRequest.getMsg()) ;
accountService.pushMsg(url,p2pRequest.getUserId(),chatVO);
LOGGER.info("下线用户[{}]", cimUserInfo.toString());
accountService.offLine(groupReqVO.getUserId());
res.setCode(StatusEnum.SUCCESS.getCode());
res.setMessage(StatusEnum.SUCCESS.getMessage());
... ... @@ -97,18 +110,17 @@ public class RouteController {
}
/**
* 私聊路由
*
* @param p2pRequest
* @return
*/
@ApiOperation("私聊 API")
@RequestMapping(value = "p2pRoute", method = RequestMethod.POST)
@ApiOperation("客户端下线")
@RequestMapping(value = "offLine", method = RequestMethod.POST)
@ResponseBody()
public BaseResponse<NULLBody> p2pRoute(@RequestBody P2PReqVO p2pRequest) {
public BaseResponse<NULLBody> offLine(@RequestBody ChatReqVO groupReqVO) throws Exception {
BaseResponse<NULLBody> res = new BaseResponse();
CIMUserInfo cimUserInfo = userInfoCacheService.loadUserInfoByUserId(groupReqVO.getUserId());
LOGGER.info("下线用户[{}]", cimUserInfo.toString());
accountService.offLine(groupReqVO.getUserId());
res.setCode(StatusEnum.SUCCESS.getCode());
res.setMessage(StatusEnum.SUCCESS.getMessage());
return res;
... ...
... ... @@ -46,6 +46,13 @@ public interface AccountService {
*/
Map<Long,CIMServerResVO> loadRouteRelated() ;
/**
* 获取某个用户的路有关系
* @param userId
* @return 获取某个用户的路有关系
*/
CIMServerResVO loadRouteRelatedByUserId(Long userId) ;
/**
* 推送消息
... ...
... ... @@ -117,6 +117,14 @@ public class AccountServiceRedisImpl implements AccountService {
return routes;
}
@Override
public CIMServerResVO loadRouteRelatedByUserId(Long userId) {
String value = redisTemplate.opsForValue().get(ROUTE_PREFIX + userId);
String[] server = value.split(":");
CIMServerResVO cimServerResVO = new CIMServerResVO(server[0], Integer.parseInt(server[1]), Integer.parseInt(server[2]));
return cimServerResVO;
}
private void parseServerInfo(Map<Long, CIMServerResVO> routes, String key) {
long userId = Long.valueOf(key.split(":")[1]);
String value = redisTemplate.opsForValue().get(key);
... ...
... ... @@ -6,7 +6,7 @@ import io.swagger.annotations.ApiModelProperty;
import javax.validation.constraints.NotNull;
/**
* Function:
* Function: 单聊请求
*
* @author crossoverJie
* Date: 2018/05/21 15:56
... ... @@ -14,10 +14,39 @@ import javax.validation.constraints.NotNull;
*/
public class P2PReqVO extends BaseRequest {
@NotNull(message = "userId 不能为空")
@ApiModelProperty(required = true, value = "消息发送者的 userId", example = "1545574049323")
private Long userId ;
@NotNull(message = "userId 不能为空")
@ApiModelProperty(required = true, value = "消息接收者的 userId", example = "1545574049323")
private Long receiveUserId ;
@NotNull(message = "msg 不能为空")
@ApiModelProperty(required = true, value = "msg", example = "hello")
private String msg ;
public P2PReqVO() {
}
public P2PReqVO(Long userId, Long receiveUserId, String msg) {
this.userId = userId;
this.receiveUserId = receiveUserId;
this.msg = msg;
}
public Long getReceiveUserId() {
return receiveUserId;
}
public void setReceiveUserId(Long receiveUserId) {
this.receiveUserId = receiveUserId;
}
public String getMsg() {
return msg;
}
... ... @@ -25,4 +54,20 @@ public class P2PReqVO extends BaseRequest {
public void setMsg(String msg) {
this.msg = msg;
}
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
@Override
public String toString() {
return "GroupReqVO{" +
"userId=" + userId +
", msg='" + msg + '\'' +
"} " + super.toString();
}
}
... ...