Skip to content

Commit 73e7dd2

Browse files
committed
群聊消息的收发
1 parent d0c417f commit 73e7dd2

27 files changed

+666
-9
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package com.bruis.learnnetty.im.client;
22

3-
import com.bruis.learnnetty.im.client.handler.CreateGroupResponseHandler;
4-
import com.bruis.learnnetty.im.client.handler.LoginResponseHandler;
5-
import com.bruis.learnnetty.im.client.handler.LogoutResponseHandler;
6-
import com.bruis.learnnetty.im.client.handler.MessageResponseHandler;
3+
import com.bruis.learnnetty.im.client.handler.*;
74
import com.bruis.learnnetty.im.codec.PacketDecoder;
85
import com.bruis.learnnetty.im.codec.PacketEncoder;
96
import com.bruis.learnnetty.im.codec.Spliter;
@@ -55,10 +52,14 @@ public void initChannel(SocketChannel ch) {
5552
ch.pipeline().addLast(new PacketDecoder());
5653
// 登录响应
5754
ch.pipeline().addLast(new LoginResponseHandler());
58-
ch.pipeline().addLast(new LogoutResponseHandler());
5955
// 消息返回
6056
ch.pipeline().addLast(new MessageResponseHandler());
6157
ch.pipeline().addLast(new CreateGroupResponseHandler());
58+
ch.pipeline().addLast(new JoinGroupResponseHandler());
59+
ch.pipeline().addLast(new QuitGroupResponseHandler());
60+
ch.pipeline().addLast(new ListGroupMembersResponseHandler());
61+
ch.pipeline().addLast(new GroupMessageResponseHandler());
62+
ch.pipeline().addLast(new LogoutResponseHandler());
6263
// 解码
6364
ch.pipeline().addLast(new PacketEncoder());
6465
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.bruis.learnnetty.im.client.handler;
2+
3+
import com.bruis.learnnetty.im.model.GroupMessageResponsePacket;
4+
import com.bruis.learnnetty.im.session.Session;
5+
import io.netty.channel.ChannelHandlerContext;
6+
import io.netty.channel.SimpleChannelInboundHandler;
7+
8+
/**
9+
* @Description
10+
* @Author luohaiyang
11+
* @Date 2022/3/24
12+
*/
13+
public class GroupMessageResponseHandler extends SimpleChannelInboundHandler<GroupMessageResponsePacket> {
14+
@Override
15+
protected void channelRead0(ChannelHandlerContext ctx, GroupMessageResponsePacket responsePacket) {
16+
String fromGroupId = responsePacket.getFromGroupId();
17+
Session fromUser = responsePacket.getFromUser();
18+
System.out.println("收到群[" + fromGroupId + "]中[" + fromUser + "]发来的消息:" + responsePacket.getMessage());
19+
}
20+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.bruis.learnnetty.im.client.handler;
2+
3+
import com.bruis.learnnetty.im.model.JoinGroupResponsePacket;
4+
import io.netty.channel.ChannelHandlerContext;
5+
import io.netty.channel.SimpleChannelInboundHandler;
6+
7+
/**
8+
* @Description
9+
* @Author luohaiyang
10+
* @Date 2022/3/24
11+
*/
12+
public class JoinGroupResponseHandler extends SimpleChannelInboundHandler<JoinGroupResponsePacket> {
13+
@Override
14+
protected void channelRead0(ChannelHandlerContext ctx, JoinGroupResponsePacket responsePacket) throws Exception {
15+
if (responsePacket.isSuccess()) {
16+
System.out.println("加入群[" + responsePacket.getGroupId() + "]成功!");
17+
} else {
18+
System.err.println("加入群[" + responsePacket.getGroupId() + "]失败,原因为:" + responsePacket.getReason());
19+
}
20+
}
21+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.bruis.learnnetty.im.client.handler;
2+
3+
import com.bruis.learnnetty.im.model.ListGroupMembersResponsePacket;
4+
import io.netty.channel.ChannelHandlerContext;
5+
import io.netty.channel.SimpleChannelInboundHandler;
6+
7+
/**
8+
* @Description
9+
* @Author luohaiyang
10+
* @Date 2022/3/24
11+
*/
12+
public class ListGroupMembersResponseHandler extends SimpleChannelInboundHandler<ListGroupMembersResponsePacket> {
13+
14+
@Override
15+
protected void channelRead0(ChannelHandlerContext ctx, ListGroupMembersResponsePacket responsePacket) {
16+
System.out.println("群[" + responsePacket.getGroupId() + "]中的人包括:" + responsePacket.getSessionList());
17+
}
18+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.bruis.learnnetty.im.client.handler;
2+
3+
import com.bruis.learnnetty.im.model.QuitGroupResponsePacket;
4+
import io.netty.channel.ChannelHandlerContext;
5+
import io.netty.channel.SimpleChannelInboundHandler;
6+
7+
/**
8+
* @Description
9+
* @Author luohaiyang
10+
* @Date 2022/3/24
11+
*/
12+
public class QuitGroupResponseHandler extends SimpleChannelInboundHandler<QuitGroupResponsePacket> {
13+
14+
@Override
15+
protected void channelRead0(ChannelHandlerContext ctx, QuitGroupResponsePacket responsePacket) throws Exception {
16+
if (responsePacket.isSuccess()) {
17+
System.out.println("退出群聊[" + responsePacket.getGroupId() + "]成功!");
18+
} else {
19+
System.out.println("退出群聊[" + responsePacket.getGroupId() + "]失败!");
20+
}
21+
}
22+
}

Spring-Netty/src/main/java/com/bruis/learnnetty/im/console/ConsoleCommandManager.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ public ConsoleCommandManager() {
2121
consoleCommandMap.put("sendToUser", new SendToUserConsoleCommand());
2222
consoleCommandMap.put("logout", new LoginConsoleCommand());
2323
consoleCommandMap.put("createGroup", new CreateGroupConsoleCommand());
24+
consoleCommandMap.put("joinGroup", new JoinGroupConsoleCommand());
25+
consoleCommandMap.put("quitGroup", new QuitGroupConsoleCommand());
26+
consoleCommandMap.put("listGroup", new ListGroupMembersConsoleCommand());
27+
consoleCommandMap.put("sendToGroup", new SendToGroupConsoleCommand());
2428
}
2529

2630
@Override
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.bruis.learnnetty.im.console;
2+
3+
import com.bruis.learnnetty.im.model.JoinGroupRequestPacket;
4+
import io.netty.channel.Channel;
5+
6+
import java.util.Scanner;
7+
8+
/**
9+
* @Description
10+
* @Author luohaiyang
11+
* @Date 2022/3/24
12+
*/
13+
public class JoinGroupConsoleCommand implements ConsoleCommand{
14+
15+
@Override
16+
public void exec(Scanner scanner, Channel channel) {
17+
JoinGroupRequestPacket requestPacket = new JoinGroupRequestPacket();
18+
System.out.println("输入groupId, 加入群聊:");
19+
String groupId = scanner.next();
20+
requestPacket.setGroupId(groupId);
21+
channel.writeAndFlush(requestPacket);
22+
}
23+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.bruis.learnnetty.im.console;
2+
3+
import com.bruis.learnnetty.im.model.ListGroupMembersRequestPacket;
4+
import io.netty.channel.Channel;
5+
6+
import java.util.Scanner;
7+
8+
/**
9+
* @Description
10+
* @Author luohaiyang
11+
* @Date 2022/3/24
12+
*/
13+
public class ListGroupMembersConsoleCommand implements ConsoleCommand {
14+
15+
@Override
16+
public void exec(Scanner scanner, Channel channel) {
17+
ListGroupMembersRequestPacket listGroupMembersRequestPacket = new ListGroupMembersRequestPacket();
18+
19+
System.out.print("输入 groupId,获取群成员列表:");
20+
String groupId = scanner.next();
21+
22+
listGroupMembersRequestPacket.setGroupId(groupId);
23+
channel.writeAndFlush(listGroupMembersRequestPacket);
24+
}
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.bruis.learnnetty.im.console;
2+
3+
import com.bruis.learnnetty.im.model.QuitGroupRequestPacket;
4+
import io.netty.channel.Channel;
5+
6+
import java.util.Scanner;
7+
8+
/**
9+
* @Description
10+
* @Author luohaiyang
11+
* @Date 2022/3/24
12+
*/
13+
public class QuitGroupConsoleCommand implements ConsoleCommand {
14+
15+
@Override
16+
public void exec(Scanner scanner, Channel channel) {
17+
QuitGroupRequestPacket quitGroupRequestPacket = new QuitGroupRequestPacket();
18+
19+
System.out.print("输入 groupId,退出群聊:");
20+
String groupId = scanner.next();
21+
22+
quitGroupRequestPacket.setGroupId(groupId);
23+
channel.writeAndFlush(quitGroupRequestPacket);
24+
}
25+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.bruis.learnnetty.im.console;
2+
3+
import com.bruis.learnnetty.im.model.GroupMessageRequestPacket;
4+
import io.netty.channel.Channel;
5+
6+
import java.util.Scanner;
7+
8+
/**
9+
* @Description
10+
* @Author luohaiyang
11+
* @Date 2022/3/24
12+
*/
13+
public class SendToGroupConsoleCommand implements ConsoleCommand {
14+
15+
@Override
16+
public void exec(Scanner scanner, Channel channel) {
17+
System.out.print("发送消息给某个某个群组:");
18+
19+
String toGroupId = scanner.next();
20+
String message = scanner.next();
21+
channel.writeAndFlush(new GroupMessageRequestPacket(toGroupId, message));
22+
23+
}
24+
}

0 commit comments

Comments
 (0)