作者 crossoverJie

:recycle: 重构代码

... ... @@ -5,11 +5,9 @@ import com.crossoverjie.cim.client.service.MsgHandle;
import com.crossoverjie.cim.client.util.SpringBeanFactory;
import com.crossoverjie.cim.client.vo.req.GroupReqVO;
import com.crossoverjie.cim.client.vo.req.P2PReqVO;
import com.crossoverjie.cim.common.enums.SystemCommandEnumType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Map;
import java.util.Scanner;
/**
... ... @@ -52,43 +50,14 @@ public class Scan implements Runnable {
continue;
}
//单聊
totalMsg = msg.split("><");
if (totalMsg.length > 1) {
//私聊
p2pChat(totalMsg);
} else {
//群聊
groupChat(msg);
}
//真正的发送消息
msgHandle.sendMsg(msg) ;
LOGGER.info("{}:【{}】", configuration.getUserName(), msg);
}
}
/**
* 处理内置函数
*/
private void innerCommand(String msg) {
Map<String, String> allStatusCode = SystemCommandEnumType.getAllStatusCode();
if (SystemCommandEnumType.QUIT.getCommandType().equals(msg)){
LOGGER.info("系统关闭中。。。。");
System.exit(0);
}else {
LOGGER.warn("====================================");
for (Map.Entry<String, String> stringStringEntry : allStatusCode.entrySet()) {
String key = stringStringEntry.getKey();
String value = stringStringEntry.getValue();
LOGGER.warn(key + "----->" + value);
}
LOGGER.warn("====================================");
}
return;
}
private void p2pChat(String[] totalMsg) {
P2PReqVO p2PReqVO = new P2PReqVO();
... ...
... ... @@ -12,6 +12,11 @@ import com.crossoverjie.cim.client.vo.req.P2PReqVO;
*/
public interface MsgHandle {
/**
* 统一的发送接口,包含了 groupChat p2pChat
* @param msg
*/
void sendMsg(String msg) ;
/**
* 群聊
... ... @@ -25,7 +30,7 @@ public interface MsgHandle {
* @param p2PReqVO 私聊请求
* @throws Exception
*/
void p2pChat(P2PReqVO p2PReqVO) throws Exception;
void p2pChat(P2PReqVO p2PReqVO) ;
// TODO: 2018/12/26 后续对消息的处理可以优化为责任链模式
... ...
package com.crossoverjie.cim.client.service.impl;
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.vo.req.GroupReqVO;
... ... @@ -29,6 +30,8 @@ public class MsgHandler implements MsgHandle {
@Autowired
private RouteRequest routeRequest ;
@Autowired
private AppConfiguration configuration;
@Autowired
private ThreadPoolExecutor executor ;
... ... @@ -37,12 +40,34 @@ public class MsgHandler implements MsgHandle {
private CIMClient cimClient ;
@Override
public void sendMsg(String msg) {
String[] totalMsg = msg.split("><");
if (totalMsg.length > 1) {
//私聊
P2PReqVO p2PReqVO = new P2PReqVO();
p2PReqVO.setUserId(configuration.getUserId());
p2PReqVO.setReceiveUserId(Long.parseLong(totalMsg[0]));
p2PReqVO.setMsg(totalMsg[1]);
p2pChat(p2PReqVO);
} else {
//群聊
GroupReqVO groupReqVO = new GroupReqVO(configuration.getUserId(), msg);
try {
groupChat(groupReqVO);
} catch (Exception e) {
LOGGER.error("Exception",e);
}
}
}
@Override
public void groupChat(GroupReqVO groupReqVO) throws Exception {
routeRequest.sendGroupMsg(groupReqVO);
}
@Override
public void p2pChat(P2PReqVO p2PReqVO) throws Exception {
public void p2pChat(P2PReqVO p2PReqVO) {
}
... ...