Skip to content

Commit 63b992a

Browse files
committed
客户端互相通信
1 parent f2f9a9a commit 63b992a

17 files changed

+248
-136
lines changed

‎Spring-Netty/src/main/java/com/bruis/learnnetty/im/client/NettyClient.java‎

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
packagecom.bruis.learnnetty.im.client;
22

3-
importcom.bruis.learnnetty.im.client.handler.FirstClientHandler;
43
importcom.bruis.learnnetty.im.client.handler.LoginResponseHandler;
54
importcom.bruis.learnnetty.im.client.handler.MessageResponseHandler;
65
importcom.bruis.learnnetty.im.codec.PacketDecoder;
76
importcom.bruis.learnnetty.im.codec.PacketEncoder;
87
importcom.bruis.learnnetty.im.codec.Spliter;
8+
importcom.bruis.learnnetty.im.model.LoginRequestPacket;
99
importcom.bruis.learnnetty.im.model.MessageRequestPacket;
10-
importcom.bruis.learnnetty.im.util.LoginUtil;
10+
importcom.bruis.learnnetty.im.util.SessionUtil;
1111
importio.netty.bootstrap.Bootstrap;
1212
importio.netty.channel.Channel;
1313
importio.netty.channel.ChannelFuture;
@@ -45,13 +45,15 @@ public static void main(String[] args){
4545
.handler(newChannelInitializer<SocketChannel>(){
4646
@Override
4747
publicvoidinitChannel(SocketChannelch){
48+
// 拆包粘包处理
4849
ch.pipeline().addLast(newSpliter());
49-
// ch.pipeline().addLast(new FirstClientHandler());
50-
// 解码
50+
// 编码
5151
ch.pipeline().addLast(newPacketDecoder());
52+
// 登录响应
5253
ch.pipeline().addLast(newLoginResponseHandler());
54+
// 消息返回
5355
ch.pipeline().addLast(newMessageResponseHandler());
54-
// 编码
56+
// 解码
5557
ch.pipeline().addLast(newPacketEncoder());
5658
}
5759
});
@@ -80,16 +82,35 @@ private static void connect(Bootstrap bootstrap, String host, int port, int retr
8082
}
8183

8284
privatestaticvoidstartConsoleThread(Channelchannel){
85+
Scannersc = newScanner(System.in);
86+
LoginRequestPacketloginRequestPacket = newLoginRequestPacket();
87+
8388
newThread(() ->{
8489
while (!Thread.interrupted()){
85-
if (LoginUtil.hasLogin(channel)){
86-
System.out.println("输入消息发送至服务端: ");
87-
Scannersc = newScanner(System.in);
88-
Stringline = sc.nextLine();
90+
if (!SessionUtil.hasLogin(channel)){
91+
System.out.print("输入用户名登录: ");
92+
Stringusername = sc.nextLine();
93+
loginRequestPacket.setUserName(username);
94+
95+
// 密码使用默认的
96+
loginRequestPacket.setPassword("pwd");
8997

90-
channel.writeAndFlush(newMessageRequestPacket(line));
98+
// 发送登录数据包
99+
channel.writeAndFlush(loginRequestPacket);
100+
waitForLoginResponse();
101+
} else{
102+
StringtoUserId = sc.next();
103+
Stringmessage = sc.next();
104+
channel.writeAndFlush(newMessageRequestPacket(toUserId, message));
91105
}
92106
}
93107
}).start();
94108
}
109+
110+
privatestaticvoidwaitForLoginResponse(){
111+
try{
112+
Thread.sleep(1000);
113+
} catch (InterruptedExceptionignored){
114+
}
115+
}
95116
}

‎Spring-Netty/src/main/java/com/bruis/learnnetty/im/client/handler/FirstClientHandler.java‎

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
packagecom.bruis.learnnetty.im.client.handler;
22

3-
importcom.bruis.learnnetty.im.model.LoginRequestPacket;
43
importcom.bruis.learnnetty.im.model.LoginResponsePacket;
5-
importcom.bruis.learnnetty.im.util.LoginUtil;
4+
importcom.bruis.learnnetty.im.session.Session;
5+
importcom.bruis.learnnetty.im.util.SessionUtil;
66
importio.netty.channel.ChannelHandlerContext;
77
importio.netty.channel.SimpleChannelInboundHandler;
88

99
importjava.util.Date;
10-
importjava.util.UUID;
1110

1211
/**
1312
* @Description 登录响应的reponse
@@ -17,24 +16,20 @@
1716
publicclassLoginResponseHandlerextendsSimpleChannelInboundHandler<LoginResponsePacket>{
1817

1918
@Override
20-
publicvoidchannelActive(ChannelHandlerContextctx) throwsException{
21-
// 创建登录对象
22-
LoginRequestPacketloginRequestPacket = newLoginRequestPacket();
23-
loginRequestPacket.setUserId(UUID.randomUUID().toString());
24-
loginRequestPacket.setUserName("flash");
25-
loginRequestPacket.setPassword("pwd");
19+
protectedvoidchannelRead0(ChannelHandlerContextctx, LoginResponsePacketloginResponsePacket) throwsException{
20+
StringuserId = loginResponsePacket.getUserId();
21+
StringuserName = loginResponsePacket.getUserName();
2622

27-
// 写数据-发起登录
28-
ctx.channel().writeAndFlush(loginRequestPacket);
29-
}
30-
31-
@Override
32-
protectedvoidchannelRead0(ChannelHandlerContextchannelHandlerContext, LoginResponsePacketloginResponsePacket) throwsException{
3323
if (loginResponsePacket.isSuccess()){
3424
System.out.println(newDate() + ": 客户端登录成功");
35-
LoginUtil.markAsLogin(channelHandlerContext.channel());
25+
SessionUtil.bindSession(newSession(userId, userName), ctx.channel());
3626
} else{
3727
System.out.println(newDate() + ": 客户端登录失败,原因:" + loginResponsePacket.getReason());
3828
}
3929
}
30+
31+
@Override
32+
publicvoidchannelInactive(ChannelHandlerContextctx) throwsException{
33+
System.out.println("客户端连接被关闭");
34+
}
4035
}

‎Spring-Netty/src/main/java/com/bruis/learnnetty/im/client/handler/MessageResponseHandler.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
importio.netty.channel.ChannelHandlerContext;
55
importio.netty.channel.SimpleChannelInboundHandler;
66

7-
importjava.util.Date;
8-
97
/**
108
* @Description
119
* @Author luohaiyang
@@ -14,6 +12,8 @@
1412
publicclassMessageResponseHandlerextendsSimpleChannelInboundHandler<MessageResponsePacket>{
1513
@Override
1614
protectedvoidchannelRead0(ChannelHandlerContextchannelHandlerContext, MessageResponsePacketmessageResponsePacket) throwsException{
17-
System.out.println(newDate() + ": 收到服务端的消息: " + messageResponsePacket.getMessage());
15+
StringfromUserId = messageResponsePacket.getFromUserId();
16+
StringfromUserName = messageResponsePacket.getFromUserName();
17+
System.out.println(fromUserId + ":" + fromUserName + " -> " + messageResponsePacket.getMessage());
1818
}
1919
}

‎Spring-Netty/src/main/java/com/bruis/learnnetty/im/codec/Spliter.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
importio.netty.handler.codec.LengthFieldBasedFrameDecoder;
77

88
/**
9-
* @Description
9+
* @Description 拆包、粘包处理
1010
* @Author luohaiyang
1111
* @Date 2022/3/23
1212
*/
@@ -20,11 +20,11 @@ public Spliter(){
2020

2121
@Override
2222
protectedObjectdecode(ChannelHandlerContextctx, ByteBufin) throwsException{
23+
// 校验协议
2324
if (in.getInt(in.readerIndex()) != PacketCodeC.MAGIC_NUMBER){
2425
ctx.channel().close();
2526
returnnull;
2627
}
27-
2828
returnsuper.decode(ctx, in);
2929
}
3030
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
packagecom.bruis.learnnetty.im.model;
22

3+
importcom.bruis.learnnetty.im.session.Session;
34
importio.netty.util.AttributeKey;
45

56
/**
@@ -8,5 +9,5 @@
89
* @Date 2022/3/22
910
*/
1011
publicinterfaceAttributes{
11-
AttributeKey<Boolean> LOGIN = AttributeKey.newInstance("login");
12+
AttributeKey<Session> SESSION = AttributeKey.newInstance("session");
1213
}

‎Spring-Netty/src/main/java/com/bruis/learnnetty/im/model/LoginResponsePacket.java‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
* @Date 2022/3/22
99
*/
1010
publicclassLoginResponsePacketextendsPacket{
11+
12+
privateStringuserId;
13+
14+
privateStringuserName;
15+
1116
privatebooleansuccess;
1217

1318
privateStringreason;
@@ -33,4 +38,20 @@ public String getReason(){
3338
publicvoidsetReason(Stringreason){
3439
this.reason = reason;
3540
}
41+
42+
publicStringgetUserId(){
43+
returnuserId;
44+
}
45+
46+
publicvoidsetUserId(StringuserId){
47+
this.userId = userId;
48+
}
49+
50+
publicStringgetUserName(){
51+
returnuserName;
52+
}
53+
54+
publicvoidsetUserName(StringuserName){
55+
this.userName = userName;
56+
}
3657
}

‎Spring-Netty/src/main/java/com/bruis/learnnetty/im/model/MessageRequestPacket.java‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@
99
*/
1010
publicclassMessageRequestPacketextendsPacket{
1111

12+
privateStringtoUserId;
13+
14+
privateStringmessage;
15+
1216
publicMessageRequestPacket(){}
1317

14-
publicMessageRequestPacket(Stringmessage){
18+
publicMessageRequestPacket(StringtoUserId, Stringmessage){
19+
this.toUserId = toUserId;
1520
this.message = message;
1621
}
1722

18-
privateStringmessage;
19-
2023
@Override
2124
publicBytegetCommand(){
2225
returnMESSAGE_REQUEST;
@@ -29,4 +32,12 @@ public String getMessage(){
2932
publicvoidsetMessage(Stringmessage){
3033
this.message = message;
3134
}
35+
36+
publicStringgetToUserId(){
37+
returntoUserId;
38+
}
39+
40+
publicvoidsetToUserId(StringtoUserId){
41+
this.toUserId = toUserId;
42+
}
3243
}

‎Spring-Netty/src/main/java/com/bruis/learnnetty/im/model/MessageResponsePacket.java‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
*/
1010
publicclassMessageResponsePacketextendsPacket{
1111

12+
privateStringfromUserId;
13+
14+
privateStringfromUserName;
15+
1216
privateStringmessage;
1317

1418
@Override
@@ -24,4 +28,20 @@ public String getMessage(){
2428
publicvoidsetMessage(Stringmessage){
2529
this.message = message;
2630
}
31+
32+
publicStringgetFromUserId(){
33+
returnfromUserId;
34+
}
35+
36+
publicvoidsetFromUserId(StringfromUserId){
37+
this.fromUserId = fromUserId;
38+
}
39+
40+
publicStringgetFromUserName(){
41+
returnfromUserName;
42+
}
43+
44+
publicvoidsetFromUserName(StringfromUserName){
45+
this.fromUserName = fromUserName;
46+
}
2747
}

‎Spring-Netty/src/main/java/com/bruis/learnnetty/im/server/NettyServer.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
importcom.bruis.learnnetty.im.codec.PacketDecoder;
44
importcom.bruis.learnnetty.im.codec.PacketEncoder;
55
importcom.bruis.learnnetty.im.codec.Spliter;
6-
importcom.bruis.learnnetty.im.server.handler.FirstServerHandler;
6+
importcom.bruis.learnnetty.im.server.handler.AuthHandler;
77
importcom.bruis.learnnetty.im.server.handler.LoginRequestHandler;
88
importcom.bruis.learnnetty.im.server.handler.MessageRequestHandler;
99
importio.netty.bootstrap.ServerBootstrap;
@@ -41,6 +41,7 @@ protected void initChannel(NioSocketChannel ch) throws Exception{
4141
// ch.pipeline().addLast(new FirstServerHandler());
4242
ch.pipeline().addLast(newPacketDecoder());
4343
ch.pipeline().addLast(newLoginRequestHandler());
44+
ch.pipeline().addLast(newAuthHandler());
4445
ch.pipeline().addLast(newMessageRequestHandler());
4546
ch.pipeline().addLast(newPacketEncoder());
4647
}

0 commit comments

Comments
(0)