作者 crossoverJie

:recycle: 重构代码2

@@ -52,9 +52,7 @@ public class CIMClient { @@ -52,9 +52,7 @@ public class CIMClient {
52 @PostConstruct 52 @PostConstruct
53 public void start() throws Exception { 53 public void start() throws Exception {
54 //登录 + 获取可以使用的服务器 ip+port 54 //登录 + 获取可以使用的服务器 ip+port
55 - LoginReqVO loginReqVO = new LoginReqVO(userId,userName) ;  
56 - CIMServerResVO.ServerInfo cimServer = routeRequest.getCIMServer(loginReqVO);  
57 - LOGGER.info("cimServer=[{}]",cimServer.toString()); 55 + CIMServerResVO.ServerInfo cimServer = userLogin();
58 56
59 //启动客户端 57 //启动客户端
60 startClient(cimServer); 58 startClient(cimServer);
@@ -64,20 +62,6 @@ public class CIMClient { @@ -64,20 +62,6 @@ public class CIMClient {
64 } 62 }
65 63
66 /** 64 /**
67 - * 向服务器注册  
68 - */  
69 - private void loginCIMServer() {  
70 - CIMRequestProto.CIMReqProtocol login = CIMRequestProto.CIMReqProtocol.newBuilder()  
71 - .setRequestId(userId)  
72 - .setReqMsg(userName)  
73 - .setType(Constants.CommandType.LOGIN)  
74 - .build();  
75 - ChannelFuture future = channel.writeAndFlush(login);  
76 - future.addListener((ChannelFutureListener) channelFuture ->  
77 - LOGGER.info("注册成功={}", login.toString()));  
78 - }  
79 -  
80 - /**  
81 * 启动客户端 65 * 启动客户端
82 * @param cimServer 66 * @param cimServer
83 * @throws InterruptedException 67 * @throws InterruptedException
@@ -97,6 +81,32 @@ public class CIMClient { @@ -97,6 +81,32 @@ public class CIMClient {
97 } 81 }
98 82
99 /** 83 /**
  84 + * 登录+路由服务器
  85 + * @return 路由服务器信息
  86 + * @throws Exception
  87 + */
  88 + private CIMServerResVO.ServerInfo userLogin() throws Exception {
  89 + LoginReqVO loginReqVO = new LoginReqVO(userId,userName) ;
  90 + CIMServerResVO.ServerInfo cimServer = routeRequest.getCIMServer(loginReqVO);
  91 + LOGGER.info("cimServer=[{}]",cimServer.toString());
  92 + return cimServer;
  93 + }
  94 +
  95 + /**
  96 + * 向服务器注册
  97 + */
  98 + private void loginCIMServer() {
  99 + CIMRequestProto.CIMReqProtocol login = CIMRequestProto.CIMReqProtocol.newBuilder()
  100 + .setRequestId(userId)
  101 + .setReqMsg(userName)
  102 + .setType(Constants.CommandType.LOGIN)
  103 + .build();
  104 + ChannelFuture future = channel.writeAndFlush(login);
  105 + future.addListener((ChannelFutureListener) channelFuture ->
  106 + LOGGER.info("注册成功={}", login.toString()));
  107 + }
  108 +
  109 + /**
100 * 发送消息字符串 110 * 发送消息字符串
101 * 111 *
102 * @param msg 112 * @param msg
@@ -2,10 +2,11 @@ package com.crossoverjie.cim.client.scanner; @@ -2,10 +2,11 @@ package com.crossoverjie.cim.client.scanner;
2 2
3 import com.crossoverjie.cim.client.client.CIMClient; 3 import com.crossoverjie.cim.client.client.CIMClient;
4 import com.crossoverjie.cim.client.config.AppConfiguration; 4 import com.crossoverjie.cim.client.config.AppConfiguration;
  5 +import com.crossoverjie.cim.client.service.MsgHandle;
5 import com.crossoverjie.cim.client.service.RouteRequest; 6 import com.crossoverjie.cim.client.service.RouteRequest;
6 import com.crossoverjie.cim.client.util.SpringBeanFactory; 7 import com.crossoverjie.cim.client.util.SpringBeanFactory;
7 -import com.crossoverjie.cim.client.vo.req.GoogleProtocolVO;  
8 import com.crossoverjie.cim.client.vo.req.GroupReqVO; 8 import com.crossoverjie.cim.client.vo.req.GroupReqVO;
  9 +import com.crossoverjie.cim.client.vo.req.P2PReqVO;
9 import com.crossoverjie.cim.common.util.StringUtil; 10 import com.crossoverjie.cim.common.util.StringUtil;
10 import org.slf4j.Logger; 11 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory; 12 import org.slf4j.LoggerFactory;
@@ -29,17 +30,19 @@ public class Scan implements Runnable { @@ -29,17 +30,19 @@ public class Scan implements Runnable {
29 30
30 private AppConfiguration configuration; 31 private AppConfiguration configuration;
31 32
  33 + private MsgHandle msgHandle ;
  34 +
32 public Scan() { 35 public Scan() {
33 this.configuration = SpringBeanFactory.getBean(AppConfiguration.class); 36 this.configuration = SpringBeanFactory.getBean(AppConfiguration.class);
34 this.heartbeatClient = SpringBeanFactory.getBean(CIMClient.class); 37 this.heartbeatClient = SpringBeanFactory.getBean(CIMClient.class);
35 this.routeRequest = SpringBeanFactory.getBean(RouteRequest.class); 38 this.routeRequest = SpringBeanFactory.getBean(RouteRequest.class);
  39 + this.msgHandle = SpringBeanFactory.getBean(MsgHandle.class) ;
36 } 40 }
37 41
38 @Override 42 @Override
39 public void run() { 43 public void run() {
40 Scanner sc = new Scanner(System.in); 44 Scanner sc = new Scanner(System.in);
41 String[] totalMsg; 45 String[] totalMsg;
42 - GoogleProtocolVO vo;  
43 while (true) { 46 while (true) {
44 String msg = sc.nextLine(); 47 String msg = sc.nextLine();
45 if (checkMsg(msg)) { 48 if (checkMsg(msg)) {
@@ -49,22 +52,36 @@ public class Scan implements Runnable { @@ -49,22 +52,36 @@ public class Scan implements Runnable {
49 //单聊 52 //单聊
50 totalMsg = msg.split("><"); 53 totalMsg = msg.split("><");
51 if (totalMsg.length > 1) { 54 if (totalMsg.length > 1) {
52 - vo = new GoogleProtocolVO();  
53 - vo.setRequestId(Integer.parseInt(totalMsg[0]));  
54 - vo.setMsg(totalMsg[1]);  
55 - heartbeatClient.sendGoogleProtocolMsg(vo); 55 + //私聊
  56 + p2pChat(totalMsg);
56 } else { 57 } else {
57 //群聊 58 //群聊
  59 + groupChat(msg);
  60 + }
  61 +
  62 +
  63 + LOGGER.info("{}:【{}】", configuration.getUserName(), msg);
  64 + }
  65 + }
  66 +
  67 + private void p2pChat(String[] totalMsg) {
  68 + P2PReqVO p2PReqVO = new P2PReqVO();
  69 + p2PReqVO.setUserId(configuration.getUserId());
  70 + p2PReqVO.setReceiveUserId(Long.parseLong(totalMsg[0]));
  71 + p2PReqVO.setMsg(totalMsg[1]);
58 try { 72 try {
59 - GroupReqVO groupReqVO = new GroupReqVO(configuration.getUserId(), msg);  
60 - routeRequest.sendGroupMsg(groupReqVO); 73 + msgHandle.p2pChat(p2PReqVO);
61 } catch (Exception e) { 74 } catch (Exception e) {
62 LOGGER.error("Exception", e); 75 LOGGER.error("Exception", e);
63 } 76 }
64 } 77 }
65 78
66 -  
67 - LOGGER.info("{}:【{}】", configuration.getUserName(), msg); 79 + private void groupChat(String msg) {
  80 + GroupReqVO groupReqVO = new GroupReqVO(configuration.getUserId(), msg);
  81 + try {
  82 + msgHandle.groupChat(groupReqVO);
  83 + } catch (Exception e) {
  84 + LOGGER.error("Exception", e);
68 } 85 }
69 } 86 }
70 87
  1 +package com.crossoverjie.cim.client.service;
  2 +
  3 +import com.crossoverjie.cim.client.vo.req.GroupReqVO;
  4 +import com.crossoverjie.cim.client.vo.req.P2PReqVO;
  5 +
  6 +/**
  7 + * Function:消息处理器
  8 + *
  9 + * @author crossoverJie
  10 + * Date: 2018/12/26 11:11
  11 + * @since JDK 1.8
  12 + */
  13 +public interface MsgHandle {
  14 +
  15 +
  16 + /**
  17 + * 群聊
  18 + * @param groupReqVO 群聊消息 其中的 userId 为发送者的 userID
  19 + */
  20 + void groupChat(GroupReqVO groupReqVO) throws Exception ;
  21 +
  22 + /**
  23 + * 私聊
  24 + * @param p2PReqVO 私聊请求
  25 + * @throws Exception
  26 + */
  27 + void p2pChat(P2PReqVO p2PReqVO) throws Exception;
  28 +}
  1 +package com.crossoverjie.cim.client.service.impl;
  2 +
  3 +import com.crossoverjie.cim.client.service.MsgHandle;
  4 +import com.crossoverjie.cim.client.service.RouteRequest;
  5 +import com.crossoverjie.cim.client.vo.req.GroupReqVO;
  6 +import com.crossoverjie.cim.client.vo.req.P2PReqVO;
  7 +import org.springframework.beans.factory.annotation.Autowired;
  8 +import org.springframework.stereotype.Service;
  9 +
  10 +/**
  11 + * Function:
  12 + *
  13 + * @author crossoverJie
  14 + * Date: 2018/12/26 11:15
  15 + * @since JDK 1.8
  16 + */
  17 +@Service
  18 +public class MsgHandler implements MsgHandle {
  19 +
  20 + @Autowired
  21 + private RouteRequest routeRequest ;
  22 +
  23 + @Override
  24 + public void groupChat(GroupReqVO groupReqVO) throws Exception {
  25 + routeRequest.sendGroupMsg(groupReqVO);
  26 + }
  27 +
  28 + @Override
  29 + public void p2pChat(P2PReqVO p2PReqVO) throws Exception {
  30 +
  31 + }
  32 +}
@@ -6,7 +6,7 @@ import io.swagger.annotations.ApiModelProperty; @@ -6,7 +6,7 @@ import io.swagger.annotations.ApiModelProperty;
6 import javax.validation.constraints.NotNull; 6 import javax.validation.constraints.NotNull;
7 7
8 /** 8 /**
9 - * Function: Google Protocol 编解码发送 9 + * Function: 群发请求
10 * 10 *
11 * @author crossoverJie 11 * @author crossoverJie
12 * Date: 2018/05/21 15:56 12 * Date: 2018/05/21 15:56
  1 +package com.crossoverjie.cim.client.vo.req;
  2 +
  3 +import com.crossoverjie.cim.common.req.BaseRequest;
  4 +import io.swagger.annotations.ApiModelProperty;
  5 +
  6 +import javax.validation.constraints.NotNull;
  7 +
  8 +/**
  9 + * Function: 单聊请求
  10 + *
  11 + * @author crossoverJie
  12 + * Date: 2018/05/21 15:56
  13 + * @since JDK 1.8
  14 + */
  15 +public class P2PReqVO extends BaseRequest {
  16 +
  17 + @NotNull(message = "userId 不能为空")
  18 + @ApiModelProperty(required = true, value = "消息发送者的 userId", example = "1545574049323")
  19 + private Long userId ;
  20 +
  21 +
  22 + @NotNull(message = "userId 不能为空")
  23 + @ApiModelProperty(required = true, value = "消息接收者的 userId", example = "1545574049323")
  24 + private Long receiveUserId ;
  25 +
  26 +
  27 +
  28 +
  29 + @NotNull(message = "msg 不能为空")
  30 + @ApiModelProperty(required = true, value = "msg", example = "hello")
  31 + private String msg ;
  32 +
  33 + public P2PReqVO() {
  34 + }
  35 +
  36 + public P2PReqVO(Long userId, Long receiveUserId, String msg) {
  37 + this.userId = userId;
  38 + this.receiveUserId = receiveUserId;
  39 + this.msg = msg;
  40 + }
  41 +
  42 + public Long getReceiveUserId() {
  43 + return receiveUserId;
  44 + }
  45 +
  46 + public void setReceiveUserId(Long receiveUserId) {
  47 + this.receiveUserId = receiveUserId;
  48 + }
  49 +
  50 + public String getMsg() {
  51 + return msg;
  52 + }
  53 +
  54 + public void setMsg(String msg) {
  55 + this.msg = msg;
  56 + }
  57 +
  58 + public Long getUserId() {
  59 + return userId;
  60 + }
  61 +
  62 + public void setUserId(Long userId) {
  63 + this.userId = userId;
  64 + }
  65 +
  66 + @Override
  67 + public String toString() {
  68 + return "GroupReqVO{" +
  69 + "userId=" + userId +
  70 + ", msg='" + msg + '\'' +
  71 + "} " + super.toString();
  72 + }
  73 +}