功能调整
This commit is contained in:
parent
b137dbdd93
commit
3eae0c46b5
@ -67,15 +67,15 @@ public class MessageSocketClient implements Runnable {
|
||||
public static void main(String[] args) {
|
||||
MessageSocketClientConfig messageSocketClientConfig = new MessageSocketClientConfig();
|
||||
messageSocketClientConfig.setHost("127.0.0.1");
|
||||
messageSocketClientConfig.setPort(9999);
|
||||
messageSocketClientConfig.setPort(8888);
|
||||
messageSocketClientConfig.setDelayPingSeconds(3);
|
||||
messageSocketClientConfig.setMaxReconnectCount(10);
|
||||
messageSocketClientConfig.setMaxReconnectCount(5);
|
||||
messageSocketClientConfig.setReconnectTimeStep(1);
|
||||
|
||||
AbstractMessageClientService abstractMessageClientService = new AbstractMessageClientService() {
|
||||
@Override
|
||||
public void readMessage(String message) {
|
||||
System.out.println(message);
|
||||
System.out.println("读取数据:" + message);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -3,6 +3,7 @@ package com.cm.socket.client.handler;
|
||||
import com.cm.socket.client.MessageSocketClient;
|
||||
import com.cm.socket.client.config.MessageSocketClientConfig;
|
||||
import com.cm.socket.enums.SocketMessageEnum;
|
||||
import com.cm.socket.enums.SocketTypeMessageEnum;
|
||||
import com.cm.socket.pojo.Message;
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
@ -42,10 +43,10 @@ public class MessageClientPingHandler extends SimpleChannelInboundHandler<Messag
|
||||
|
||||
@Override
|
||||
protected void channelRead0(ChannelHandlerContext ctx, Message msg) throws Exception {
|
||||
if (msg.getStart() != SocketMessageEnum.MESSAGE_TYPE_START.getType()) {
|
||||
if (msg.getStart() != SocketTypeMessageEnum.MESSAGE_TYPE_START.getType()) {
|
||||
return;
|
||||
}
|
||||
if (msg.getType() == SocketMessageEnum.MESSAGE_TYPE_PONG.getType()) {
|
||||
if (msg.getType() == SocketTypeMessageEnum.MESSAGE_TYPE_PONG.getType()) {
|
||||
System.out.println("server pong");
|
||||
return;
|
||||
}
|
||||
@ -61,8 +62,8 @@ public class MessageClientPingHandler extends SimpleChannelInboundHandler<Messag
|
||||
ScheduledFuture<?> scheduledFuture = channel.eventLoop().schedule(() -> {
|
||||
if (channel.isActive()) {
|
||||
Message ping = new Message();
|
||||
ping.setStart(SocketMessageEnum.MESSAGE_TYPE_START.getType());
|
||||
ping.setType(SocketMessageEnum.MESSAGE_TYPE_PING.getType());
|
||||
ping.setStart(SocketTypeMessageEnum.MESSAGE_TYPE_START.getType());
|
||||
ping.setType(SocketTypeMessageEnum.MESSAGE_TYPE_PING.getType());
|
||||
ping.setContent(SocketMessageEnum.MESSAGE_PING.getMessage());
|
||||
channel.writeAndFlush(ping);
|
||||
} else {
|
||||
|
@ -1,11 +1,9 @@
|
||||
package com.cm.socket.client.service;
|
||||
|
||||
import com.cm.socket.enums.SocketMessageEnum;
|
||||
import com.cm.socket.enums.SocketTypeMessageEnum;
|
||||
import com.cm.socket.pojo.Message;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
|
||||
import java.nio.channels.Channel;
|
||||
|
||||
/**
|
||||
* When you feel like quitting. Think about why you started
|
||||
* 当你想要放弃的时候,想想当初你为何开始
|
||||
@ -30,8 +28,8 @@ public abstract class AbstractMessageClientService {
|
||||
return;
|
||||
}
|
||||
Message message = new Message();
|
||||
message.setStart(SocketMessageEnum.MESSAGE_TYPE_START.getType());
|
||||
message.setType(SocketMessageEnum.MESSAGE_TYPE_MESSAGE.getType());
|
||||
message.setStart(SocketTypeMessageEnum.MESSAGE_TYPE_START.getType());
|
||||
message.setType(SocketTypeMessageEnum.MESSAGE_TYPE_MESSAGE.getType());
|
||||
message.setContent(sendMessage);
|
||||
channelHandlerContext.writeAndFlush(message);
|
||||
}
|
||||
|
@ -12,4 +12,13 @@ package com.cm.socket.consts;
|
||||
**/
|
||||
public interface ISocketConst {
|
||||
|
||||
/**
|
||||
* 客户端ID
|
||||
*/
|
||||
String CLIENT_ID = "clientId";
|
||||
/**
|
||||
* 客户端密码
|
||||
*/
|
||||
String CLIENT_SECRET = "clientSecret";
|
||||
|
||||
}
|
||||
|
@ -21,13 +21,13 @@ import java.util.List;
|
||||
**/
|
||||
public class MessageDecoder extends ByteToMessageDecoder {
|
||||
/**
|
||||
* 头部长度 1byte + 1byte + 4byte
|
||||
* 头部长度 1byte + 1byte + 4byte + 4byte
|
||||
*/
|
||||
private static final int HEADER_LENGTH = 6;
|
||||
private static final int HEADER_LENGTH = 10;
|
||||
|
||||
@Override
|
||||
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
|
||||
if(in == null) {
|
||||
if (in == null) {
|
||||
return;
|
||||
}
|
||||
if (in.readableBytes() < HEADER_LENGTH) {
|
||||
@ -36,21 +36,53 @@ public class MessageDecoder extends ByteToMessageDecoder {
|
||||
in.markReaderIndex();
|
||||
byte start = in.readByte();
|
||||
byte type = in.readByte();
|
||||
// 消息体长度
|
||||
int tokenBytesLength = in.readInt();
|
||||
int contentBytesLength = in.readInt();
|
||||
if (contentBytesLength <= 0) {
|
||||
return;
|
||||
}
|
||||
if (in.readableBytes() < contentBytesLength) {
|
||||
in.resetReaderIndex();
|
||||
return;
|
||||
}
|
||||
byte[] contentBytes = new byte[contentBytesLength];
|
||||
in.readBytes(contentBytes);
|
||||
String token = getToken(in, tokenBytesLength);
|
||||
String content = getContent(in, contentBytesLength);
|
||||
Message message = new Message();
|
||||
message.setStart(start);
|
||||
message.setType(type);
|
||||
message.setContent(new String(contentBytes, CharsetUtil.UTF_8));
|
||||
message.setToken(token);
|
||||
message.setContent(content);
|
||||
out.add(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取ID
|
||||
*
|
||||
* @param in
|
||||
* @return
|
||||
*/
|
||||
private String getToken(ByteBuf in, int tokenBytesLength) {
|
||||
if (tokenBytesLength <= 0) {
|
||||
return null;
|
||||
}
|
||||
if (in.readableBytes() < tokenBytesLength) {
|
||||
in.resetReaderIndex();
|
||||
return null;
|
||||
}
|
||||
byte[] tokenBytes = new byte[tokenBytesLength];
|
||||
in.readBytes(tokenBytes);
|
||||
return new String(tokenBytes, CharsetUtil.UTF_8);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取内容
|
||||
*
|
||||
* @param in
|
||||
* @return
|
||||
*/
|
||||
private String getContent(ByteBuf in, int contentBytesLength) {
|
||||
if (contentBytesLength <= 0) {
|
||||
return null;
|
||||
}
|
||||
if (in.readableBytes() < contentBytesLength) {
|
||||
in.resetReaderIndex();
|
||||
return null;
|
||||
}
|
||||
byte[] contentBytes = new byte[contentBytesLength];
|
||||
in.readBytes(contentBytes);
|
||||
return new String(contentBytes, CharsetUtil.UTF_8);
|
||||
}
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.MessageToByteEncoder;
|
||||
import io.netty.util.CharsetUtil;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
@ -22,10 +23,13 @@ public class MessageEncoder extends MessageToByteEncoder<Message> {
|
||||
|
||||
@Override
|
||||
protected void encode(ChannelHandlerContext ctx, Message msg, ByteBuf out) throws Exception {
|
||||
byte[] contentBytes = msg.getContent().getBytes(CharsetUtil.UTF_8);
|
||||
byte[] tokenBytes = StringUtils.isBlank(msg.getToken()) ? new byte[0] : msg.getToken().getBytes(CharsetUtil.UTF_8);
|
||||
byte[] contentBytes = StringUtils.isBlank(msg.getContent()) ? new byte[0] : msg.getContent().getBytes(CharsetUtil.UTF_8);
|
||||
out.writeByte(msg.getStart());
|
||||
out.writeByte(msg.getType());
|
||||
out.writeInt(tokenBytes.length);
|
||||
out.writeInt(contentBytes.length);
|
||||
out.writeBytes(tokenBytes);
|
||||
out.writeBytes(contentBytes);
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,27 @@
|
||||
package com.cm.socket.enums;
|
||||
|
||||
/**
|
||||
* When you feel like quitting. Think about why you started
|
||||
* 当你想要放弃的时候,想想当初你为何开始
|
||||
*
|
||||
* @ClassName: SocketCodeEnum
|
||||
* @Description:
|
||||
* @Author: WangGeng
|
||||
* @Date: 2020/7/28 4:19 下午
|
||||
* @Version: 1.0
|
||||
**/
|
||||
public enum SocketCodeEnum {
|
||||
|
||||
SUCCESS(200),
|
||||
ERROR(400);
|
||||
|
||||
private int code;
|
||||
|
||||
SocketCodeEnum(int code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
}
|
@ -4,36 +4,23 @@ package com.cm.socket.enums;
|
||||
* When you feel like quitting. Think about why you started
|
||||
* 当你想要放弃的时候,想想当初你为何开始
|
||||
*
|
||||
* @ClassName: ISocketMessageEnum
|
||||
* @Description:
|
||||
* @ClassName: SocketMessageEnum
|
||||
* @Description: socket信息
|
||||
* @Author: WangGeng
|
||||
* @Date: 2020/7/6 9:26 上午
|
||||
* @Date: 2020/7/28 7:38 下午
|
||||
* @Version: 1.0
|
||||
**/
|
||||
public enum SocketMessageEnum {
|
||||
|
||||
MESSAGE_TYPE_START((byte) (0xAA << 4)),
|
||||
MESSAGE_TYPE_PING((byte) 0x01),
|
||||
MESSAGE_TYPE_PONG((byte) 0x02),
|
||||
MESSAGE_TYPE_MESSAGE((byte) 0x03),
|
||||
MESSAGE_PING("PING"),
|
||||
MESSAGE_PONG("PONG");
|
||||
|
||||
private byte type;
|
||||
private String message;
|
||||
|
||||
SocketMessageEnum(byte type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
SocketMessageEnum(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public byte getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message == null ? "" : message.trim();
|
||||
}
|
||||
|
@ -0,0 +1,34 @@
|
||||
package com.cm.socket.enums;
|
||||
|
||||
/**
|
||||
* When you feel like quitting. Think about why you started
|
||||
* 当你想要放弃的时候,想想当初你为何开始
|
||||
*
|
||||
* @ClassName: ISocketMessageEnum
|
||||
* @Description:
|
||||
* @Author: WangGeng
|
||||
* @Date: 2020/7/6 9:26 上午
|
||||
* @Version: 1.0
|
||||
**/
|
||||
public enum SocketTypeMessageEnum {
|
||||
|
||||
MESSAGE_TYPE_START((byte) (0xAA << 4)),
|
||||
MESSAGE_TYPE_PING((byte) 0x01),
|
||||
MESSAGE_TYPE_PONG((byte) 0x02),
|
||||
MESSAGE_TYPE_MESSAGE((byte) 0x03),
|
||||
MESSAGE_TYPE_EXCEPTION((byte) 0x04),
|
||||
MESSAGE_TYPE_ERROR((byte) 0x05),
|
||||
MESSAGE_TYPE_CLIENT_INFO((byte) 0x06),
|
||||
MESSAGE_TYPE_CLIENT_LOGIN((byte) 0x07);
|
||||
|
||||
private byte type;
|
||||
|
||||
SocketTypeMessageEnum(byte type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public byte getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
}
|
@ -1,5 +1,7 @@
|
||||
package com.cm.socket.pojo;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* When you feel like quitting. Think about why you started
|
||||
* 当你想要放弃的时候,想想当初你为何开始
|
||||
@ -10,10 +12,12 @@ package com.cm.socket.pojo;
|
||||
* @Date: 2020/7/4 15:56
|
||||
* @Version: 1.0
|
||||
**/
|
||||
public class Message {
|
||||
public class Message implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 2041189621546143865L;
|
||||
private byte start;
|
||||
private byte type;
|
||||
private String token;
|
||||
private String content;
|
||||
|
||||
public byte getStart() {
|
||||
@ -32,6 +36,14 @@ public class Message {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public String getToken() {
|
||||
return token == null ? "" : token.trim();
|
||||
}
|
||||
|
||||
public void setToken(String token) {
|
||||
this.token = token;
|
||||
}
|
||||
|
||||
public String getContent() {
|
||||
return content == null ? "" : content.trim();
|
||||
}
|
||||
@ -39,5 +51,4 @@ public class Message {
|
||||
public void setContent(String content) {
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,36 @@
|
||||
package com.cm.socket.pojo;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* When you feel like quitting. Think about why you started
|
||||
* 当你想要放弃的时候,想想当初你为何开始
|
||||
*
|
||||
* @ClassName: SocketClient
|
||||
* @Description: 客户端
|
||||
* @Author: WangGeng
|
||||
* @Date: 2020/7/28 3:56 下午
|
||||
* @Version: 1.0
|
||||
**/
|
||||
public class SocketClient implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = -8167337825837171106L;
|
||||
private String clientId;
|
||||
private String clientSecret;
|
||||
|
||||
public String getClientId() {
|
||||
return clientId == null ? "" : clientId.trim();
|
||||
}
|
||||
|
||||
public void setClientId(String clientId) {
|
||||
this.clientId = clientId;
|
||||
}
|
||||
|
||||
public String getClientSecret() {
|
||||
return clientSecret == null ? "" : clientSecret.trim();
|
||||
}
|
||||
|
||||
public void setClientSecret(String clientSecret) {
|
||||
this.clientSecret = clientSecret;
|
||||
}
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
package com.cm.socket.pojo;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import org.apache.poi.ss.formula.functions.T;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* When you feel like quitting. Think about why you started
|
||||
* 当你想要放弃的时候,想想当初你为何开始
|
||||
*
|
||||
* @ClassName: SocketResult
|
||||
* @Description: socket结果
|
||||
* @Author: WangGeng
|
||||
* @Date: 2020/7/28 4:26 下午
|
||||
* @Version: 1.0
|
||||
**/
|
||||
public class SocketResult<T> {
|
||||
|
||||
private int code;
|
||||
private T data;
|
||||
private List<T> list;
|
||||
private String errorMsg;
|
||||
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public void setCode(int code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public T getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public void setData(T data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public List<T> getList() {
|
||||
return list;
|
||||
}
|
||||
|
||||
public void setList(List<T> list) {
|
||||
this.list = list;
|
||||
}
|
||||
|
||||
public String getErrorMsg() {
|
||||
return errorMsg == null ? "" : errorMsg.trim();
|
||||
}
|
||||
|
||||
public void setErrorMsg(String errorMsg) {
|
||||
this.errorMsg = errorMsg;
|
||||
}
|
||||
}
|
@ -0,0 +1,123 @@
|
||||
package com.cm.socket.service;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.cm.common.base.AbstractService;
|
||||
import com.cm.socket.enums.SocketCodeEnum;
|
||||
import com.cm.socket.enums.SocketTypeMessageEnum;
|
||||
import com.cm.socket.pojo.Message;
|
||||
import com.cm.socket.pojo.SocketResult;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* When you feel like quitting. Think about why you started
|
||||
* 当你想要放弃的时候,想想当初你为何开始
|
||||
*
|
||||
* @ClassName: BaseService
|
||||
* @Description: service
|
||||
* @Author: WangGeng
|
||||
* @Date: 2020/7/28 2:13 下午
|
||||
* @Version: 1.0
|
||||
**/
|
||||
public abstract class AbstractSocketService extends AbstractService implements ISocketService {
|
||||
|
||||
/**
|
||||
* 读取信息业务
|
||||
*
|
||||
* @param channelHandlerContext
|
||||
* @param readMessage
|
||||
*/
|
||||
protected abstract void readMessageService(ChannelHandlerContext channelHandlerContext, Message readMessage);
|
||||
|
||||
/**
|
||||
* 输出信息
|
||||
*
|
||||
* @param channelHandlerContext
|
||||
* @param socketTypeMessageEnum
|
||||
* @param socketResult
|
||||
*/
|
||||
protected void writeMessage(ChannelHandlerContext channelHandlerContext, SocketTypeMessageEnum socketTypeMessageEnum, SocketResult socketResult) {
|
||||
writeMessage(channelHandlerContext, socketTypeMessageEnum, JSONObject.toJSONString(socketResult));
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出信息
|
||||
*
|
||||
* @param channelHandlerContext
|
||||
* @param socketTypeMessageEnum
|
||||
* @param content
|
||||
*/
|
||||
protected void writeMessage(ChannelHandlerContext channelHandlerContext, SocketTypeMessageEnum socketTypeMessageEnum, String content) {
|
||||
Message message = new Message();
|
||||
message.setStart(SocketTypeMessageEnum.MESSAGE_TYPE_START.getType());
|
||||
message.setType(socketTypeMessageEnum.getType());
|
||||
message.setToken(channelHandlerContext.channel().id().asShortText());
|
||||
message.setContent(content);
|
||||
write(channelHandlerContext, message);
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出错误信息
|
||||
*
|
||||
* @param channelHandlerContext
|
||||
* @param errorMessage
|
||||
*/
|
||||
protected void writeError(ChannelHandlerContext channelHandlerContext, String errorMessage) {
|
||||
Message message = new Message();
|
||||
message.setStart(SocketTypeMessageEnum.MESSAGE_TYPE_START.getType());
|
||||
message.setType(SocketTypeMessageEnum.MESSAGE_TYPE_ERROR.getType());
|
||||
message.setContent(JSONObject.toJSONString(errorResult(errorMessage)));
|
||||
write(channelHandlerContext, message);
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出数据
|
||||
*
|
||||
* @param channelHandlerContext
|
||||
* @param writeMessage
|
||||
*/
|
||||
protected void write(ChannelHandlerContext channelHandlerContext, Message writeMessage) {
|
||||
channelHandlerContext.channel().writeAndFlush(writeMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* 成功对象结果
|
||||
*
|
||||
* @param data
|
||||
* @return
|
||||
*/
|
||||
protected <T> SocketResult successResult(T data) {
|
||||
SocketResult<T> socketResult = new SocketResult<>();
|
||||
socketResult.setCode(SocketCodeEnum.SUCCESS.getCode());
|
||||
socketResult.setData(data);
|
||||
return socketResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* 成功列表结果
|
||||
*
|
||||
* @param list
|
||||
* @return
|
||||
*/
|
||||
protected <T> SocketResult successResultList(List<T> list) {
|
||||
SocketResult<T> socketResult = new SocketResult<>();
|
||||
socketResult.setCode(SocketCodeEnum.SUCCESS.getCode());
|
||||
socketResult.setList(list);
|
||||
return socketResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* 错误结果
|
||||
*
|
||||
* @param errorMsg
|
||||
* @return
|
||||
*/
|
||||
protected SocketResult errorResult(String errorMsg) {
|
||||
SocketResult<String> socketResult = new SocketResult<>();
|
||||
socketResult.setCode(SocketCodeEnum.ERROR.getCode());
|
||||
socketResult.setErrorMsg(errorMsg);
|
||||
return socketResult;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package com.cm.socket.service;
|
||||
|
||||
import com.cm.socket.pojo.Message;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
|
||||
/**
|
||||
* When you feel like quitting. Think about why you started
|
||||
* 当你想要放弃的时候,想想当初你为何开始
|
||||
*
|
||||
* @ClassName: ISocketServer
|
||||
* @Description: Socket业务接口
|
||||
* @Author: WangGeng
|
||||
* @Date: 2020/7/28 2:41 下午
|
||||
* @Version: 1.0
|
||||
**/
|
||||
public interface ISocketService {
|
||||
|
||||
/**
|
||||
* 读取数据
|
||||
*
|
||||
* @param channelHandlerContext
|
||||
* @param readMessage
|
||||
*/
|
||||
void readMessage(ChannelHandlerContext channelHandlerContext, Message readMessage);
|
||||
|
||||
/**
|
||||
* 自动回复数据
|
||||
*
|
||||
* @param channelHandlerContext
|
||||
*/
|
||||
void autoReply(ChannelHandlerContext channelHandlerContext);
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user