Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 43108fa

Browse filesBrowse files
committed
代码优化
1 parent 73e7dd2 commit 43108fa
Copy full SHA for 43108fa

File tree

Expand file treeCollapse file tree

11 files changed

+139
-15
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

11 files changed

+139
-15
lines changed
Open diff view settings
Collapse file
+35Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.bruis.learnnetty.im.codec;
2+
3+
import com.bruis.learnnetty.im.model.Packet;
4+
import com.bruis.learnnetty.im.model.PacketCodeC;
5+
import io.netty.buffer.ByteBuf;
6+
import io.netty.channel.ChannelHandler;
7+
import io.netty.channel.ChannelHandlerContext;
8+
import io.netty.handler.codec.MessageToMessageCodec;
9+
10+
import java.util.List;
11+
12+
/**
13+
* @Description
14+
* @Author luohaiyang
15+
* @Date 2022/3/25
16+
*/
17+
@ChannelHandler.Sharable
18+
public class PacketCodecHandler extends MessageToMessageCodec<ByteBuf, Packet> {
19+
20+
public static final PacketCodecHandler INSTANCE = new PacketCodecHandler();
21+
22+
private PacketCodecHandler() {}
23+
24+
@Override
25+
protected void encode(ChannelHandlerContext ctx, Packet msg, List<Object> out) throws Exception {
26+
ByteBuf byteBuf = ctx.channel().alloc().ioBuffer();
27+
PacketCodeC.INSTANCE.encode(byteBuf, msg);
28+
out.add(byteBuf);
29+
}
30+
31+
@Override
32+
protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
33+
out.add(PacketCodeC.INSTANCE.decode(msg));
34+
}
35+
}
Collapse file

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

Copy file name to clipboardExpand all lines: Spring-Netty/src/main/java/com/bruis/learnnetty/im/server/NettyServer.java
+5-11Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.bruis.learnnetty.im.server;
22

3+
import com.bruis.learnnetty.im.codec.PacketCodecHandler;
34
import com.bruis.learnnetty.im.codec.PacketDecoder;
45
import com.bruis.learnnetty.im.codec.PacketEncoder;
56
import com.bruis.learnnetty.im.codec.Spliter;
@@ -36,17 +37,10 @@ public static void main(String[] args) {
3637
@Override
3738
protected void initChannel(NioSocketChannel ch) throws Exception {
3839
ch.pipeline().addLast(new Spliter());
39-
ch.pipeline().addLast(new PacketDecoder());
40-
ch.pipeline().addLast(new LoginRequestHandler());
41-
ch.pipeline().addLast(new AuthHandler());
42-
ch.pipeline().addLast(new MessageRequestHandler());
43-
ch.pipeline().addLast(new CreateGroupRequestHandler());
44-
ch.pipeline().addLast(new JoinGroupRequestHandler());
45-
ch.pipeline().addLast(new QuitGroupRequestHandler());
46-
ch.pipeline().addLast(new ListGroupMembersRequestHandler());
47-
ch.pipeline().addLast(new GroupMessageRequestHandler());
48-
ch.pipeline().addLast(new LogoutRequestHandler());
49-
ch.pipeline().addLast(new PacketEncoder());
40+
ch.pipeline().addLast(PacketCodecHandler.INSTANCE);
41+
ch.pipeline().addLast(LoginRequestHandler.INSTANCE);
42+
ch.pipeline().addLast(AuthHandler.INSTANCE);
43+
ch.pipeline().addLast(IMHandler.INSTANCE);
5044
}
5145
});
5246
bind(serverBootstrap, PORT);
Collapse file

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

Copy file name to clipboardExpand all lines: Spring-Netty/src/main/java/com/bruis/learnnetty/im/server/handler/AuthHandler.java
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.bruis.learnnetty.im.server.handler;
22

33
import com.bruis.learnnetty.im.util.SessionUtil;
4+
import io.netty.channel.ChannelHandler;
45
import io.netty.channel.ChannelHandlerContext;
56
import io.netty.channel.ChannelInboundHandlerAdapter;
67

@@ -9,8 +10,13 @@
910
* @Author luohaiyang
1011
* @Date 2022/3/23
1112
*/
13+
@ChannelHandler.Sharable
1214
public class AuthHandler extends ChannelInboundHandlerAdapter {
1315

16+
public static final AuthHandler INSTANCE = new AuthHandler();
17+
18+
protected AuthHandler() {}
19+
1420
@Override
1521
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
1622
if (!SessionUtil.hasLogin(ctx.channel())) {
Collapse file

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

Copy file name to clipboardExpand all lines: Spring-Netty/src/main/java/com/bruis/learnnetty/im/server/handler/CreateGroupRequestHandler.java
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.bruis.learnnetty.im.util.IDUtil;
66
import com.bruis.learnnetty.im.util.SessionUtil;
77
import io.netty.channel.Channel;
8+
import io.netty.channel.ChannelHandler;
89
import io.netty.channel.ChannelHandlerContext;
910
import io.netty.channel.SimpleChannelInboundHandler;
1011
import io.netty.channel.group.ChannelGroup;
@@ -19,8 +20,13 @@
1920
* @Author luohaiyang
2021
* @Date 2022/3/24
2122
*/
23+
@ChannelHandler.Sharable
2224
public class CreateGroupRequestHandler extends SimpleChannelInboundHandler<CreateGroupRequestPacket> {
2325

26+
public static final CreateGroupRequestHandler INSTANCE = new CreateGroupRequestHandler();
27+
28+
protected CreateGroupRequestHandler() {}
29+
2430
@Override
2531
protected void channelRead0(ChannelHandlerContext ctx, CreateGroupRequestPacket msg) throws Exception {
2632
List<String> userIdList = msg.getUserIdList();
Collapse file
+42Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.bruis.learnnetty.im.server.handler;
2+
3+
import io.netty.channel.ChannelHandler;
4+
import io.netty.channel.ChannelHandlerContext;
5+
import io.netty.channel.SimpleChannelInboundHandler;
6+
import com.bruis.learnnetty.im.model.Packet;
7+
8+
import java.util.HashMap;
9+
import java.util.Map;
10+
11+
import static com.bruis.learnnetty.im.model.Command.*;
12+
13+
14+
/**
15+
* @Description
16+
* @Author luohaiyang
17+
* @Date 2022/3/25
18+
*/
19+
@ChannelHandler.Sharable
20+
public class IMHandler extends SimpleChannelInboundHandler<Packet> {
21+
22+
public static final IMHandler INSTANCE = new IMHandler();
23+
24+
private Map<Byte, SimpleChannelInboundHandler<? extends Packet>> handlerMap;
25+
26+
private IMHandler() {
27+
handlerMap = new HashMap<>();
28+
29+
handlerMap.put(MESSAGE_REQUEST, MessageRequestHandler.INSTANCE);
30+
handlerMap.put(CREATE_GROUP_REQUEST, CreateGroupRequestHandler.INSTANCE);
31+
handlerMap.put(JOIN_GROUP_REQUEST, JoinGroupRequestHandler.INSTANCE);
32+
handlerMap.put(QUIT_GROUP_REQUEST, QuitGroupRequestHandler.INSTANCE);
33+
handlerMap.put(LIST_GROUP_MEMBERS_REQUEST, ListGroupMembersRequestHandler.INSTANCE);
34+
handlerMap.put(GROUP_MESSAGE_REQUEST, GroupMessageRequestHandler.INSTANCE);
35+
handlerMap.put(LOGOUT_REQUEST, LogoutRequestHandler.INSTANCE);
36+
}
37+
38+
@Override
39+
protected void channelRead0(ChannelHandlerContext ctx, Packet packet) throws Exception {
40+
handlerMap.get(packet.getCommand()).channelRead(ctx, packet);
41+
}
42+
}
Collapse file

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

Copy file name to clipboardExpand all lines: Spring-Netty/src/main/java/com/bruis/learnnetty/im/server/handler/JoinGroupRequestHandler.java
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.bruis.learnnetty.im.model.JoinGroupRequestPacket;
44
import com.bruis.learnnetty.im.model.JoinGroupResponsePacket;
55
import com.bruis.learnnetty.im.util.SessionUtil;
6+
import io.netty.channel.ChannelHandler;
67
import io.netty.channel.ChannelHandlerContext;
78
import io.netty.channel.SimpleChannelInboundHandler;
89
import io.netty.channel.group.ChannelGroup;
@@ -12,8 +13,13 @@
1213
* @Author luohaiyang
1314
* @Date 2022/3/24
1415
*/
16+
@ChannelHandler.Sharable
1517
public class JoinGroupRequestHandler extends SimpleChannelInboundHandler<JoinGroupRequestPacket> {
1618

19+
public static final JoinGroupRequestHandler INSTANCE = new JoinGroupRequestHandler();
20+
21+
protected JoinGroupRequestHandler() {}
22+
1723
@Override
1824
protected void channelRead0(ChannelHandlerContext ctx, JoinGroupRequestPacket msg) throws Exception {
1925
// 目标群聊id
Collapse file

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

Copy file name to clipboardExpand all lines: Spring-Netty/src/main/java/com/bruis/learnnetty/im/server/handler/ListGroupMembersRequestHandler.java
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.bruis.learnnetty.im.session.Session;
66
import com.bruis.learnnetty.im.util.SessionUtil;
77
import io.netty.channel.Channel;
8+
import io.netty.channel.ChannelHandler;
89
import io.netty.channel.ChannelHandlerContext;
910
import io.netty.channel.SimpleChannelInboundHandler;
1011
import io.netty.channel.group.ChannelGroup;
@@ -17,7 +18,13 @@
1718
* @Author luohaiyang
1819
* @Date 2022/3/24
1920
*/
21+
@ChannelHandler.Sharable
2022
public class ListGroupMembersRequestHandler extends SimpleChannelInboundHandler<ListGroupMembersRequestPacket> {
23+
24+
public static final ListGroupMembersRequestHandler INSTANCE = new ListGroupMembersRequestHandler();
25+
26+
protected ListGroupMembersRequestHandler() {}
27+
2128
@Override
2229
protected void channelRead0(ChannelHandlerContext ctx, ListGroupMembersRequestPacket requestPacket) {
2330
// 1. 获取群的 ChannelGroup
Collapse file

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

Copy file name to clipboardExpand all lines: Spring-Netty/src/main/java/com/bruis/learnnetty/im/server/handler/LoginRequestHandler.java
+14-4Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,23 @@
55
import com.bruis.learnnetty.im.session.Session;
66
import com.bruis.learnnetty.im.util.IDUtil;
77
import com.bruis.learnnetty.im.util.SessionUtil;
8-
import io.netty.channel.ChannelHandlerContext;
9-
import io.netty.channel.SimpleChannelInboundHandler;
8+
import io.netty.channel.*;
109

10+
import java.util.Arrays;
1111
import java.util.Date;
12-
import java.util.UUID;
1312

1413
/**
1514
* @Description 接收客户端登录请求
1615
* @Author luohaiyang
1716
* @Date 2022/3/23
1817
*/
18+
@ChannelHandler.Sharable
1919
public class LoginRequestHandler extends SimpleChannelInboundHandler<LoginRequestPacket> {
2020

21+
public static final LoginRequestHandler INSTANCE = new LoginRequestHandler();
22+
23+
protected LoginRequestHandler() {}
24+
2125
@Override
2226
protected void channelRead0(ChannelHandlerContext ctx, LoginRequestPacket loginRequestPacket) {
2327
// 登录校验响应
@@ -38,7 +42,13 @@ protected void channelRead0(ChannelHandlerContext ctx, LoginRequestPacket loginR
3842
}
3943

4044
// 登录响应
41-
ctx.channel().writeAndFlush(loginResponsePacket);
45+
ctx.writeAndFlush(loginResponsePacket).addListener((ChannelFutureListener) future -> {
46+
// 关闭channel成功
47+
Throwable cause = future.cause();
48+
if (null != cause) {
49+
System.out.println(Arrays.toString(cause.getStackTrace()));
50+
}
51+
});
4252
}
4353

4454
private boolean valid(LoginRequestPacket loginRequestPacket) {
Collapse file

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

Copy file name to clipboardExpand all lines: Spring-Netty/src/main/java/com/bruis/learnnetty/im/server/handler/LogoutRequestHandler.java
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.bruis.learnnetty.im.model.LogoutRequestPacket;
44
import com.bruis.learnnetty.im.model.LogoutResponsePacket;
55
import com.bruis.learnnetty.im.util.SessionUtil;
6+
import io.netty.channel.ChannelHandler;
67
import io.netty.channel.ChannelHandlerContext;
78
import io.netty.channel.SimpleChannelInboundHandler;
89

@@ -11,8 +12,13 @@
1112
* @Author luohaiyang
1213
* @Date 2022/3/24
1314
*/
15+
@ChannelHandler.Sharable
1416
public class LogoutRequestHandler extends SimpleChannelInboundHandler<LogoutRequestPacket> {
1517

18+
public static final LogoutRequestHandler INSTANCE = new LogoutRequestHandler();
19+
20+
protected LogoutRequestHandler () {}
21+
1622
@Override
1723
protected void channelRead0(ChannelHandlerContext ctx, LogoutRequestPacket msg) {
1824
SessionUtil.unBindSession(ctx.channel());
Collapse file

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

Copy file name to clipboardExpand all lines: Spring-Netty/src/main/java/com/bruis/learnnetty/im/server/handler/MessageRequestHandler.java
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.bruis.learnnetty.im.session.Session;
66
import com.bruis.learnnetty.im.util.SessionUtil;
77
import io.netty.channel.Channel;
8+
import io.netty.channel.ChannelHandler;
89
import io.netty.channel.ChannelHandlerContext;
910
import io.netty.channel.SimpleChannelInboundHandler;
1011

@@ -13,8 +14,13 @@
1314
* @Author luohaiyang
1415
* @Date 2022/3/23
1516
*/
17+
@ChannelHandler.Sharable
1618
public class MessageRequestHandler extends SimpleChannelInboundHandler<MessageRequestPacket> {
1719

20+
public static final MessageRequestHandler INSTANCE = new MessageRequestHandler();
21+
22+
protected MessageRequestHandler() {}
23+
1824
@Override
1925
protected void channelRead0(ChannelHandlerContext channelHandlerContext, MessageRequestPacket messageRequestPacket) throws Exception {
2026
// 1.拿到消息发送方的会话信息

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.