作者 crossoverJie

:recycle: 重构代码2

... ... @@ -52,9 +52,7 @@ public class CIMClient {
@PostConstruct
public void start() throws Exception {
//登录 + 获取可以使用的服务器 ip+port
LoginReqVO loginReqVO = new LoginReqVO(userId,userName) ;
CIMServerResVO.ServerInfo cimServer = routeRequest.getCIMServer(loginReqVO);
LOGGER.info("cimServer=[{}]",cimServer.toString());
CIMServerResVO.ServerInfo cimServer = userLogin();
//启动客户端
startClient(cimServer);
... ... @@ -64,20 +62,6 @@ public class CIMClient {
}
/**
* 向服务器注册
*/
private void loginCIMServer() {
CIMRequestProto.CIMReqProtocol login = CIMRequestProto.CIMReqProtocol.newBuilder()
.setRequestId(userId)
.setReqMsg(userName)
.setType(Constants.CommandType.LOGIN)
.build();
ChannelFuture future = channel.writeAndFlush(login);
future.addListener((ChannelFutureListener) channelFuture ->
LOGGER.info("注册成功={}", login.toString()));
}
/**
* 启动客户端
* @param cimServer
* @throws InterruptedException
... ... @@ -97,6 +81,32 @@ public class CIMClient {
}
/**
* 登录+路由服务器
* @return 路由服务器信息
* @throws Exception
*/
private CIMServerResVO.ServerInfo userLogin() throws Exception {
LoginReqVO loginReqVO = new LoginReqVO(userId,userName) ;
CIMServerResVO.ServerInfo cimServer = routeRequest.getCIMServer(loginReqVO);
LOGGER.info("cimServer=[{}]",cimServer.toString());
return cimServer;
}
/**
* 向服务器注册
*/
private void loginCIMServer() {
CIMRequestProto.CIMReqProtocol login = CIMRequestProto.CIMReqProtocol.newBuilder()
.setRequestId(userId)
.setReqMsg(userName)
.setType(Constants.CommandType.LOGIN)
.build();
ChannelFuture future = channel.writeAndFlush(login);
future.addListener((ChannelFutureListener) channelFuture ->
LOGGER.info("注册成功={}", login.toString()));
}
/**
* 发送消息字符串
*
* @param msg
... ...
... ... @@ -2,10 +2,11 @@ package com.crossoverjie.cim.client.scanner;
import com.crossoverjie.cim.client.client.CIMClient;
import com.crossoverjie.cim.client.config.AppConfiguration;
import com.crossoverjie.cim.client.service.MsgHandle;
import com.crossoverjie.cim.client.service.RouteRequest;
import com.crossoverjie.cim.client.util.SpringBeanFactory;
import com.crossoverjie.cim.client.vo.req.GoogleProtocolVO;
import com.crossoverjie.cim.client.vo.req.GroupReqVO;
import com.crossoverjie.cim.client.vo.req.P2PReqVO;
import com.crossoverjie.cim.common.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
... ... @@ -29,17 +30,19 @@ public class Scan implements Runnable {
private AppConfiguration configuration;
private MsgHandle msgHandle ;
public Scan() {
this.configuration = SpringBeanFactory.getBean(AppConfiguration.class);
this.heartbeatClient = SpringBeanFactory.getBean(CIMClient.class);
this.routeRequest = SpringBeanFactory.getBean(RouteRequest.class);
this.msgHandle = SpringBeanFactory.getBean(MsgHandle.class) ;
}
@Override
public void run() {
Scanner sc = new Scanner(System.in);
String[] totalMsg;
GoogleProtocolVO vo;
while (true) {
String msg = sc.nextLine();
if (checkMsg(msg)) {
... ... @@ -49,18 +52,11 @@ public class Scan implements Runnable {
//单聊
totalMsg = msg.split("><");
if (totalMsg.length > 1) {
vo = new GoogleProtocolVO();
vo.setRequestId(Integer.parseInt(totalMsg[0]));
vo.setMsg(totalMsg[1]);
heartbeatClient.sendGoogleProtocolMsg(vo);
//私聊
p2pChat(totalMsg);
} else {
//群聊
try {
GroupReqVO groupReqVO = new GroupReqVO(configuration.getUserId(), msg);
routeRequest.sendGroupMsg(groupReqVO);
} catch (Exception e) {
LOGGER.error("Exception", e);
}
groupChat(msg);
}
... ... @@ -68,6 +64,27 @@ public class Scan implements Runnable {
}
}
private void p2pChat(String[] totalMsg) {
P2PReqVO p2PReqVO = new P2PReqVO();
p2PReqVO.setUserId(configuration.getUserId());
p2PReqVO.setReceiveUserId(Long.parseLong(totalMsg[0]));
p2PReqVO.setMsg(totalMsg[1]);
try {
msgHandle.p2pChat(p2PReqVO);
} catch (Exception e) {
LOGGER.error("Exception", e);
}
}
private void groupChat(String msg) {
GroupReqVO groupReqVO = new GroupReqVO(configuration.getUserId(), msg);
try {
msgHandle.groupChat(groupReqVO);
} catch (Exception e) {
LOGGER.error("Exception", e);
}
}
/**
* 校验消息
* @param msg
... ...
package com.crossoverjie.cim.client.service;
import com.crossoverjie.cim.client.vo.req.GroupReqVO;
import com.crossoverjie.cim.client.vo.req.P2PReqVO;
/**
* Function:消息处理器
*
* @author crossoverJie
* Date: 2018/12/26 11:11
* @since JDK 1.8
*/
public interface MsgHandle {
/**
* 群聊
* @param groupReqVO 群聊消息 其中的 userId 为发送者的 userID
*/
void groupChat(GroupReqVO groupReqVO) throws Exception ;
/**
* 私聊
* @param p2PReqVO 私聊请求
* @throws Exception
*/
void p2pChat(P2PReqVO p2PReqVO) throws Exception;
}
... ...
package com.crossoverjie.cim.client.service.impl;
import com.crossoverjie.cim.client.service.MsgHandle;
import com.crossoverjie.cim.client.service.RouteRequest;
import com.crossoverjie.cim.client.vo.req.GroupReqVO;
import com.crossoverjie.cim.client.vo.req.P2PReqVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* Function:
*
* @author crossoverJie
* Date: 2018/12/26 11:15
* @since JDK 1.8
*/
@Service
public class MsgHandler implements MsgHandle {
@Autowired
private RouteRequest routeRequest ;
@Override
public void groupChat(GroupReqVO groupReqVO) throws Exception {
routeRequest.sendGroupMsg(groupReqVO);
}
@Override
public void p2pChat(P2PReqVO p2PReqVO) throws Exception {
}
}
... ...
... ... @@ -6,7 +6,7 @@ import io.swagger.annotations.ApiModelProperty;
import javax.validation.constraints.NotNull;
/**
* Function: Google Protocol 编解码发送
* Function: 群发请求
*
* @author crossoverJie
* Date: 2018/05/21 15:56
... ...
package com.crossoverjie.cim.client.vo.req;
import com.crossoverjie.cim.common.req.BaseRequest;
import io.swagger.annotations.ApiModelProperty;
import javax.validation.constraints.NotNull;
/**
* Function: 单聊请求
*
* @author crossoverJie
* Date: 2018/05/21 15:56
* @since JDK 1.8
*/
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;
}
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();
}
}
... ...