fix:聊天项目

上级 5dafc46d
......@@ -37,9 +37,7 @@ public class TestHttp {
ch.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG));
// 做http服务器端 , 解码器对请求进行解码
ch.pipeline().addLast(new HttpServerCodec());
/**
* SimpleChannelInboundHandler 可以指定 只关心某一种类型的Handler
*/
//SimpleChannelInboundHandler 可以指定 只关心某一种类型的Handler
ch.pipeline().addLast(new SimpleChannelInboundHandler<HttpRequest>() { // 或者:HttpContent
@Override
protected void channelRead0(ChannelHandlerContext ctx, HttpRequest msg) throws Exception {
......
......@@ -62,7 +62,7 @@ public class TestRedisClient {
public void channelActive(ChannelHandlerContext ctx) throws Exception {
// 分配 ByteBuf
final ByteBuf buf = ctx.alloc().buffer();
buf.writeBytes("*3".getBytes());
buf.writeBytes("*3".getBytes());//数组长度
buf.writeBytes(LINE);
buf.writeBytes("$3".getBytes());
buf.writeBytes(LINE);
......
......@@ -38,9 +38,10 @@ public class TestMessageCodec {
protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
// 这里的 Object msg 已经 【被 MessageCodecSharable 解码成了 Message类型了】
LoginRequestMessage loginRequestMessage = (LoginRequestMessage) msg;
System.out.println(loginRequestMessage.getNickname() + "==================" + loginRequestMessage.getPassword());
System.out.println(loginRequestMessage.getUsername() + "==================" + loginRequestMessage.getPassword());
}
};
//相当于服务端
final EmbeddedChannel channel = new EmbeddedChannel(
LOGGIN_HANDLER, // 【移动到流水线的最上方 可以 打印出 半包情况】
new LengthFieldBasedFrameDecoder(
......@@ -48,6 +49,7 @@ public class TestMessageCodec {
MESSAGE_CODEC,
channelInboundHandler
);
//相当于客户端
LoginRequestMessage message = new LoginRequestMessage("张三", "123456");
final ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(); // 新建一个buf
new MessageCodec().encode(null, message, buf); // 【编码 入站】
......
......@@ -11,7 +11,6 @@ import lombok.ToString;
public class LoginRequestMessage extends Message {
private String username;
private String password;
private String nickname;
public LoginRequestMessage() {
}
......
......@@ -19,6 +19,8 @@ public abstract class Message implements Serializable {
public abstract int getMessageType();
private static final Map<Integer, Class<?>> messageClasses = new HashMap<>();
public static final int LoginRequestMessage = 0;
public static final int LoginResponseMessage = 1;
public static final int ChatRequestMessage = 2;
......@@ -33,22 +35,9 @@ public abstract class Message implements Serializable {
public static final int GroupChatResponseMessage = 11;
public static final int GroupMembersRequestMessage = 12;
public static final int GroupMembersResponseMessage = 13;
private static final Map<Integer, Class<?>> messageClasses = new HashMap<>();
static {
messageClasses.put(LoginRequestMessage, LoginRequestMessage.class);
/* messageClasses.put(LoginResponseMessage, LoginResponseMessage.class);
messageClasses.put(ChatRequestMessage, ChatRequestMessage.class);
messageClasses.put(ChatResponseMessage, ChatResponseMessage.class);
messageClasses.put(GroupCreateRequestMessage, GroupCreateRequestMessage.class);
messageClasses.put(GroupCreateResponseMessage, GroupCreateResponseMessage.class);
messageClasses.put(GroupJoinRequestMessage, GroupJoinRequestMessage.class);
messageClasses.put(GroupJoinResponseMessage, GroupJoinResponseMessage.class);
messageClasses.put(GroupQuitRequestMessage, GroupQuitRequestMessage.class);
messageClasses.put(GroupQuitResponseMessage, GroupQuitResponseMessage.class);
messageClasses.put(GroupChatRequestMessage, GroupChatRequestMessage.class);
messageClasses.put(GroupChatResponseMessage, GroupChatResponseMessage.class);
messageClasses.put(GroupMembersRequestMessage, GroupMembersRequestMessage.class);
messageClasses.put(GroupMembersResponseMessage, GroupMembersResponseMessage.class);*/
}
}
......@@ -62,14 +62,14 @@ public class ChatServer {
IdleStateEvent event = (IdleStateEvent) evt;
// 是否 读超时
if (event.state() == IdleState.READER_IDLE) {
log.debug("==============================已经12秒没读到数据了!====================================");
log.debug("===已经12秒没读到数据了!===");
ctx.channel().close();
}
if (event.state() == IdleState.WRITER_IDLE) {
log.debug("==============================写超时!====================================");
log.debug("===写超时!=========");
}
if (event.state() == IdleState.ALL_IDLE) {
log.debug("==============================读写超时!====================================");
log.debug("===读写超时!=========");
}
}
});
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册