Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Kwan的解忧杂货铺@新空间代码工作室
netty-demo
提交
105091b1
N
netty-demo
项目概览
Kwan的解忧杂货铺@新空间代码工作室
/
netty-demo
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
N
netty-demo
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
105091b1
编写于
5月 04, 2023
作者:
Kwan的解忧杂货铺@新空间代码工作室
🐭
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix:聊天项目
上级
5dafc46d
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
11 addition
and
23 deletion
+11
-23
netty-03-jinjie/src/main/java/com/kwan/shuyu/advance_02_protocol/TestHttp.java
...ain/java/com/kwan/shuyu/advance_02_protocol/TestHttp.java
+1
-3
netty-03-jinjie/src/main/java/com/kwan/shuyu/advance_02_protocol/TestRedisClient.java
...a/com/kwan/shuyu/advance_02_protocol/TestRedisClient.java
+1
-1
netty-03-jinjie/src/main/java/com/kwan/shuyu/advance_02_protocol/custom/TestMessageCodec.java
...an/shuyu/advance_02_protocol/custom/TestMessageCodec.java
+3
-1
netty-03-jinjie/src/main/java/com/kwan/shuyu/advance_02_protocol/custom/message/LoginRequestMessage.java
...vance_02_protocol/custom/message/LoginRequestMessage.java
+0
-1
netty-03-jinjie/src/main/java/com/kwan/shuyu/advance_02_protocol/custom/message/Message.java
...wan/shuyu/advance_02_protocol/custom/message/Message.java
+3
-14
netty-04-chat/src/main/java/com/kwan/shuyu/server/ChatServer.java
...-chat/src/main/java/com/kwan/shuyu/server/ChatServer.java
+3
-3
未找到文件。
netty-03-jinjie/src/main/java/com/kwan/shuyu/advance_02_protocol/TestHttp.java
浏览文件 @
105091b1
...
...
@@ -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
{
...
...
netty-03-jinjie/src/main/java/com/kwan/shuyu/advance_02_protocol/TestRedisClient.java
浏览文件 @
105091b1
...
...
@@ -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
);
...
...
netty-03-jinjie/src/main/java/com/kwan/shuyu/advance_02_protocol/custom/TestMessageCodec.java
浏览文件 @
105091b1
...
...
@@ -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
.
get
Nick
name
()
+
"=================="
+
loginRequestMessage
.
getPassword
());
System
.
out
.
println
(
loginRequestMessage
.
get
User
name
()
+
"=================="
+
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
);
// 【编码 入站】
...
...
netty-03-jinjie/src/main/java/com/kwan/shuyu/advance_02_protocol/custom/message/LoginRequestMessage.java
浏览文件 @
105091b1
...
...
@@ -11,7 +11,6 @@ import lombok.ToString;
public
class
LoginRequestMessage
extends
Message
{
private
String
username
;
private
String
password
;
private
String
nickname
;
public
LoginRequestMessage
()
{
}
...
...
netty-03-jinjie/src/main/java/com/kwan/shuyu/advance_02_protocol/custom/message/Message.java
浏览文件 @
105091b1
...
...
@@ -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);*/
}
}
netty-04-chat/src/main/java/com/kwan/shuyu/server/ChatServer.java
浏览文件 @
105091b1
...
...
@@ -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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录