forked from lightningMan/flash-netty
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
闪电侠
committed
Oct 4, 2018
1 parent
1acb274
commit ca4bfca
Showing
26 changed files
with
356 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package the.flash.client.console; | ||
|
||
import io.netty.channel.Channel; | ||
|
||
import java.util.Scanner; | ||
|
||
public interface ConsoleCommand { | ||
void exec(Scanner scanner, Channel channel); | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/the/flash/client/console/ConsoleManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package the.flash.client.console; | ||
|
||
import io.netty.channel.Channel; | ||
import the.flash.util.SessionUtil; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Scanner; | ||
|
||
public class ConsoleManager implements ConsoleCommand { | ||
private Map<String, ConsoleCommand> consoleCommandMap; | ||
|
||
public ConsoleManager() { | ||
consoleCommandMap = new HashMap<>(); | ||
consoleCommandMap.put("sendToUser", new SendToUserConsoleCommand()); | ||
consoleCommandMap.put("logout", new LogoutConsoleCommand()); | ||
consoleCommandMap.put("createGroup", new CreateGroupConsoleCommand()); | ||
} | ||
|
||
@Override | ||
public void exec(Scanner scanner, Channel channel) { | ||
// 获取第一个指令 | ||
String command = scanner.next(); | ||
|
||
if (!SessionUtil.hasLogin(channel)) { | ||
return; | ||
} | ||
|
||
ConsoleCommand consoleCommand = consoleCommandMap.get(command); | ||
|
||
if (consoleCommand != null) { | ||
consoleCommand.exec(scanner, channel); | ||
} else { | ||
System.err.println("无法识别[" + command + "]指令,请重新输入!"); | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/the/flash/client/console/CreateGroupConsoleCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package the.flash.client.console; | ||
|
||
import io.netty.channel.Channel; | ||
import the.flash.protocol.request.CreateGroupRequestPacket; | ||
|
||
import java.util.Arrays; | ||
import java.util.Scanner; | ||
|
||
public class CreateGroupConsoleCommand implements ConsoleCommand { | ||
|
||
private static final String USER_ID_SPLITER = ","; | ||
|
||
@Override | ||
public void exec(Scanner scanner, Channel channel) { | ||
CreateGroupRequestPacket createGroupRequestPacket = new CreateGroupRequestPacket(); | ||
|
||
System.out.print("【拉人群聊】输入 userId 列表,userId 之间英文逗号隔开:"); | ||
String userIds = scanner.next(); | ||
createGroupRequestPacket.setUserIdList(Arrays.asList(userIds.split(USER_ID_SPLITER))); | ||
channel.writeAndFlush(createGroupRequestPacket); | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/the/flash/client/console/LoginConsoleCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package the.flash.client.console; | ||
|
||
import io.netty.channel.Channel; | ||
import the.flash.protocol.request.LoginRequestPacket; | ||
|
||
import java.util.Scanner; | ||
|
||
public class LoginConsoleCommand implements ConsoleCommand { | ||
|
||
@Override | ||
public void exec(Scanner scanner, Channel channel) { | ||
LoginRequestPacket loginRequestPacket = new LoginRequestPacket(); | ||
|
||
System.out.print("输入用户名登录: "); | ||
loginRequestPacket.setUserName(scanner.nextLine()); | ||
loginRequestPacket.setPassword("pwd"); | ||
|
||
// 发送登录数据包 | ||
channel.writeAndFlush(loginRequestPacket); | ||
waitForLoginResponse(); | ||
} | ||
|
||
private static void waitForLoginResponse() { | ||
try { | ||
Thread.sleep(1000); | ||
} catch (InterruptedException ignored) { | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/the/flash/client/console/LogoutConsoleCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package the.flash.client.console; | ||
|
||
import io.netty.channel.Channel; | ||
import the.flash.protocol.request.LogoutRequestPacket; | ||
|
||
import java.util.Scanner; | ||
|
||
public class LogoutConsoleCommand implements ConsoleCommand { | ||
@Override | ||
public void exec(Scanner scanner, Channel channel) { | ||
LogoutRequestPacket logoutRequestPacket = new LogoutRequestPacket(); | ||
channel.writeAndFlush(logoutRequestPacket); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/the/flash/client/console/SendToUserConsoleCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package the.flash.client.console; | ||
|
||
import io.netty.channel.Channel; | ||
import the.flash.protocol.request.MessageRequestPacket; | ||
|
||
import java.util.Scanner; | ||
|
||
public class SendToUserConsoleCommand implements ConsoleCommand { | ||
@Override | ||
public void exec(Scanner scanner, Channel channel) { | ||
System.out.print("发送消息给某个某个用户:"); | ||
|
||
String toUserId = scanner.next(); | ||
String message = scanner.next(); | ||
channel.writeAndFlush(new MessageRequestPacket(toUserId, message)); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/the/flash/client/handler/CreateGroupResponseHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package the.flash.client.handler; | ||
|
||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.channel.SimpleChannelInboundHandler; | ||
import the.flash.protocol.response.CreateGroupResponsePacket; | ||
|
||
public class CreateGroupResponseHandler extends SimpleChannelInboundHandler<CreateGroupResponsePacket> { | ||
|
||
@Override | ||
protected void channelRead0(ChannelHandlerContext ctx, CreateGroupResponsePacket createGroupResponsePacket) { | ||
System.out.print("群创建成功,id 为[" + createGroupResponsePacket.getGroupId() + "], "); | ||
System.out.println("群里面有:" + createGroupResponsePacket.getUserNameList()); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/the/flash/client/handler/LogoutResponseHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package the.flash.client.handler; | ||
|
||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.channel.SimpleChannelInboundHandler; | ||
import the.flash.protocol.response.LogoutResponsePacket; | ||
import the.flash.util.SessionUtil; | ||
|
||
public class LogoutResponseHandler extends SimpleChannelInboundHandler<LogoutResponsePacket> { | ||
|
||
@Override | ||
protected void channelRead0(ChannelHandlerContext ctx, LogoutResponsePacket logoutResponsePacket) { | ||
SessionUtil.unBindSession(ctx.channel()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/main/java/the/flash/protocol/request/CreateGroupRequestPacket.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package the.flash.protocol.request; | ||
|
||
import lombok.Data; | ||
import the.flash.protocol.Packet; | ||
|
||
import java.util.List; | ||
|
||
import static the.flash.protocol.command.Command.CREATE_GROUP_REQUEST; | ||
|
||
@Data | ||
public class CreateGroupRequestPacket extends Packet { | ||
|
||
private List<String> userIdList; | ||
|
||
@Override | ||
public Byte getCommand() { | ||
|
||
return CREATE_GROUP_REQUEST; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/the/flash/protocol/request/LogoutRequestPacket.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package the.flash.protocol.request; | ||
|
||
import lombok.Data; | ||
import the.flash.protocol.Packet; | ||
|
||
import static the.flash.protocol.command.Command.LOGOUT_REQUEST; | ||
|
||
@Data | ||
public class LogoutRequestPacket extends Packet { | ||
@Override | ||
public Byte getCommand() { | ||
|
||
return LOGOUT_REQUEST; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/the/flash/protocol/response/CreateGroupResponsePacket.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package the.flash.protocol.response; | ||
|
||
import lombok.Data; | ||
import the.flash.protocol.Packet; | ||
|
||
import java.util.List; | ||
|
||
import static the.flash.protocol.command.Command.CREATE_GROUP_RESPONSE; | ||
|
||
@Data | ||
public class CreateGroupResponsePacket extends Packet { | ||
private boolean success; | ||
|
||
private String groupId; | ||
|
||
private List<String> userNameList; | ||
|
||
@Override | ||
public Byte getCommand() { | ||
|
||
return CREATE_GROUP_RESPONSE; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/main/java/the/flash/protocol/response/LogoutResponsePacket.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package the.flash.protocol.response; | ||
|
||
import lombok.Data; | ||
import the.flash.protocol.Packet; | ||
|
||
import static the.flash.protocol.command.Command.LOGOUT_RESPONSE; | ||
|
||
@Data | ||
public class LogoutResponsePacket extends Packet { | ||
|
||
private boolean success; | ||
|
||
private String reason; | ||
|
||
|
||
@Override | ||
public Byte getCommand() { | ||
|
||
return LOGOUT_RESPONSE; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
.../flash/serialize/SerializerAlogrithm.java → .../flash/serialize/SerializerAlgorithm.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.