Skip to content

Commit d0c417f

Browse files
committed
实现群聊
1 parent 63b992a commit d0c417f

20 files changed

+434
-35
lines changed

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

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

3+
import com.bruis.learnnetty.im.client.handler.CreateGroupResponseHandler;
34
import com.bruis.learnnetty.im.client.handler.LoginResponseHandler;
5+
import com.bruis.learnnetty.im.client.handler.LogoutResponseHandler;
46
import com.bruis.learnnetty.im.client.handler.MessageResponseHandler;
57
import com.bruis.learnnetty.im.codec.PacketDecoder;
68
import com.bruis.learnnetty.im.codec.PacketEncoder;
79
import com.bruis.learnnetty.im.codec.Spliter;
10+
import com.bruis.learnnetty.im.console.ConsoleCommandManager;
11+
import com.bruis.learnnetty.im.console.LoginConsoleCommand;
812
import com.bruis.learnnetty.im.model.LoginRequestPacket;
913
import com.bruis.learnnetty.im.model.MessageRequestPacket;
1014
import com.bruis.learnnetty.im.util.SessionUtil;
@@ -51,8 +55,10 @@ public void initChannel(SocketChannel ch) {
5155
ch.pipeline().addLast(new PacketDecoder());
5256
// 登录响应
5357
ch.pipeline().addLast(new LoginResponseHandler());
58+
ch.pipeline().addLast(new LogoutResponseHandler());
5459
// 消息返回
5560
ch.pipeline().addLast(new MessageResponseHandler());
61+
ch.pipeline().addLast(new CreateGroupResponseHandler());
5662
// 解码
5763
ch.pipeline().addLast(new PacketEncoder());
5864
}
@@ -82,35 +88,18 @@ private static void connect(Bootstrap bootstrap, String host, int port, int retr
8288
}
8389

8490
private static void startConsoleThread(Channel channel) {
85-
Scanner sc = new Scanner(System.in);
86-
LoginRequestPacket loginRequestPacket = new LoginRequestPacket();
91+
ConsoleCommandManager consoleCommandManager = new ConsoleCommandManager();
92+
LoginConsoleCommand loginConsoleCommand = new LoginConsoleCommand();
93+
Scanner scanner = new Scanner(System.in);
8794

8895
new Thread(() -> {
8996
while (!Thread.interrupted()) {
9097
if (!SessionUtil.hasLogin(channel)) {
91-
System.out.print("输入用户名登录: ");
92-
String username = sc.nextLine();
93-
loginRequestPacket.setUserName(username);
94-
95-
// 密码使用默认的
96-
loginRequestPacket.setPassword("pwd");
97-
98-
// 发送登录数据包
99-
channel.writeAndFlush(loginRequestPacket);
100-
waitForLoginResponse();
98+
loginConsoleCommand.exec(scanner, channel);
10199
} else {
102-
String toUserId = sc.next();
103-
String message = sc.next();
104-
channel.writeAndFlush(new MessageRequestPacket(toUserId, message));
100+
consoleCommandManager.exec(scanner, channel);
105101
}
106102
}
107103
}).start();
108104
}
109-
110-
private static void waitForLoginResponse() {
111-
try {
112-
Thread.sleep(1000);
113-
} catch (InterruptedException ignored) {
114-
}
115-
}
116105
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.bruis.learnnetty.im.client.handler;
2+
3+
import com.bruis.learnnetty.im.model.CreateGroupResponsePacket;
4+
import io.netty.channel.ChannelHandlerContext;
5+
import io.netty.channel.SimpleChannelInboundHandler;
6+
7+
/**
8+
* @Description
9+
* @Author luohaiyang
10+
* @Date 2022/3/23
11+
*/
12+
public class CreateGroupResponseHandler extends SimpleChannelInboundHandler<CreateGroupResponsePacket> {
13+
14+
@Override
15+
protected void channelRead0(ChannelHandlerContext ctx, CreateGroupResponsePacket msg) throws Exception {
16+
System.out.print("群创建成功,id 为[" + msg.getGroupId() + "], ");
17+
System.out.println("群里面有:" + msg.getUserNameList());
18+
}
19+
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
import io.netty.channel.ChannelHandlerContext;
77
import io.netty.channel.SimpleChannelInboundHandler;
88

9-
import java.util.Date;
10-
119
/**
1210
* @Description 登录响应的reponse
1311
* @Author luohaiyang
@@ -21,10 +19,10 @@ protected void channelRead0(ChannelHandlerContext ctx, LoginResponsePacket login
2119
String userName = loginResponsePacket.getUserName();
2220

2321
if (loginResponsePacket.isSuccess()) {
24-
System.out.println(new Date() + ": 客户端登录成功");
22+
System.out.println("[" + userName + "]登录成功,userId 为: " + loginResponsePacket.getUserId());
2523
SessionUtil.bindSession(new Session(userId, userName), ctx.channel());
2624
} else {
27-
System.out.println(new Date() + ": 客户端登录失败,原因:" + loginResponsePacket.getReason());
25+
System.out.println("[" + userName + "]登录失败,原因:" + loginResponsePacket.getReason());
2826
}
2927
}
3028

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.bruis.learnnetty.im.client.handler;
2+
3+
import com.bruis.learnnetty.im.model.LogoutResponsePacket;
4+
import com.bruis.learnnetty.im.util.SessionUtil;
5+
import io.netty.channel.ChannelHandlerContext;
6+
import io.netty.channel.SimpleChannelInboundHandler;
7+
8+
/**
9+
* @Description
10+
* @Author luohaiyang
11+
* @Date 2022/3/23
12+
*/
13+
public class LogoutResponseHandler extends SimpleChannelInboundHandler<LogoutResponsePacket> {
14+
15+
@Override
16+
protected void channelRead0(ChannelHandlerContext ctx, LogoutResponsePacket logoutResponsePacket) {
17+
SessionUtil.unBindSession(ctx.channel());
18+
}
19+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.bruis.learnnetty.im.console;
2+
3+
import io.netty.channel.Channel;
4+
5+
import java.util.Scanner;
6+
7+
/**
8+
* @Description 指令接口
9+
* @Author luohaiyang
10+
* @Date 2022/3/23
11+
*/
12+
public interface ConsoleCommand {
13+
void exec(Scanner scanner, Channel channel);
14+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.bruis.learnnetty.im.console;
2+
3+
import com.bruis.learnnetty.im.util.SessionUtil;
4+
import io.netty.channel.Channel;
5+
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
import java.util.Scanner;
9+
10+
/**
11+
* @Description
12+
* @Author luohaiyang
13+
* @Date 2022/3/23
14+
*/
15+
public class ConsoleCommandManager implements ConsoleCommand {
16+
17+
private Map<String, ConsoleCommand> consoleCommandMap;
18+
19+
public ConsoleCommandManager() {
20+
consoleCommandMap = new HashMap<>();
21+
consoleCommandMap.put("sendToUser", new SendToUserConsoleCommand());
22+
consoleCommandMap.put("logout", new LoginConsoleCommand());
23+
consoleCommandMap.put("createGroup", new CreateGroupConsoleCommand());
24+
}
25+
26+
@Override
27+
public void exec(Scanner scanner, Channel channel) {
28+
String command = scanner.next();
29+
if (!SessionUtil.hasLogin(channel)) {
30+
return;
31+
}
32+
ConsoleCommand consoleCommand = consoleCommandMap.get(command);
33+
if (null != consoleCommand) {
34+
consoleCommand.exec(scanner, channel);
35+
} else {
36+
System.err.println("无法识别[" + command + "]指令,请重新输入");
37+
}
38+
}
39+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.bruis.learnnetty.im.console;
2+
3+
import com.bruis.learnnetty.im.model.CreateGroupRequestPacket;
4+
import io.netty.channel.Channel;
5+
6+
import java.util.Arrays;
7+
import java.util.Scanner;
8+
9+
/**
10+
* @Description
11+
* @Author luohaiyang
12+
* @Date 2022/3/23
13+
*/
14+
public class CreateGroupConsoleCommand implements ConsoleCommand {
15+
16+
private static final String USER_ID_SPLITER = ",";
17+
18+
@Override
19+
public void exec(Scanner scanner, Channel channel) {
20+
CreateGroupRequestPacket createGroupRequestPacket = new CreateGroupRequestPacket();
21+
22+
System.out.print("【拉人群聊】输入 userId 列表,userId 之间英文逗号隔开:");
23+
String userIds = scanner.next();
24+
createGroupRequestPacket.setUserIdList(Arrays.asList(userIds.split(USER_ID_SPLITER)));
25+
channel.writeAndFlush(createGroupRequestPacket);
26+
}
27+
28+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.bruis.learnnetty.im.console;
2+
3+
import com.bruis.learnnetty.im.model.LoginRequestPacket;
4+
import io.netty.channel.Channel;
5+
6+
import java.util.Scanner;
7+
8+
/**
9+
* @Description
10+
* @Author luohaiyang
11+
* @Date 2022/3/23
12+
*/
13+
public class LoginConsoleCommand implements ConsoleCommand {
14+
15+
@Override
16+
public void exec(Scanner scanner, Channel channel) {
17+
LoginRequestPacket loginRequestPacket = new LoginRequestPacket();
18+
19+
System.out.print("输入用户名登录: ");
20+
loginRequestPacket.setUserName(scanner.nextLine());
21+
loginRequestPacket.setPassword("pwd");
22+
23+
// 发送登录数据包
24+
channel.writeAndFlush(loginRequestPacket);
25+
waitForLoginResponse();
26+
}
27+
28+
private static void waitForLoginResponse() {
29+
try {
30+
Thread.sleep(1000);
31+
} catch (InterruptedException ignored) {
32+
}
33+
}
34+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.bruis.learnnetty.im.console;
2+
3+
import com.bruis.learnnetty.im.model.LogoutRequestPacket;
4+
import io.netty.channel.Channel;
5+
6+
import java.util.Scanner;
7+
8+
/**
9+
* @Description
10+
* @Author luohaiyang
11+
* @Date 2022/3/23
12+
*/
13+
public class LogoutConsoleCommand implements ConsoleCommand {
14+
@Override
15+
public void exec(Scanner scanner, Channel channel) {
16+
LogoutRequestPacket logoutRequestPacket = new LogoutRequestPacket();
17+
channel.writeAndFlush(logoutRequestPacket);
18+
}
19+
}
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.MessageRequestPacket;
4+
import io.netty.channel.Channel;
5+
6+
import java.util.Scanner;
7+
8+
/**
9+
* @Description
10+
* @Author luohaiyang
11+
* @Date 2022/3/23
12+
*/
13+
public class SendToUserConsoleCommand implements ConsoleCommand {
14+
15+
@Override
16+
public void exec(Scanner scanner, Channel channel) {
17+
System.out.print("发送消息给某个某个用户:");
18+
19+
String toUserId = scanner.next();
20+
String message = scanner.next();
21+
channel.writeAndFlush(new MessageRequestPacket(toUserId, message));
22+
}
23+
}

0 commit comments

Comments
 (0)