fix:聊天场景

上级 b4fde989
......@@ -27,17 +27,18 @@ import lombok.extern.slf4j.Slf4j;
*/
@Slf4j
public class ChatServer {
public static void main(String[] args) throws InterruptedException {
public static void main(String[] args) throws Exception {
final NioEventLoopGroup boss = new NioEventLoopGroup();
final NioEventLoopGroup worker = new NioEventLoopGroup();
// 局部变量
// 用来判断 是不是读 空闲时间过长,或写空闲时间过长 (读,写,读写空闲时间限制) 0表示不关心
final IdleStateHandler IDLE_STATE_HANDLER = new IdleStateHandler(12, 0, 0);
MessageCodecSharable MESSAGE_CODEC = new MessageCodecSharable();
LoggingHandler LOGGING_HANDLER = new LoggingHandler(LogLevel.DEBUG);
LoginRequestMessageHandler LOGIN_HANDLER = new LoginRequestMessageHandler(); //--登录---处理器
final ChatRequestMessageHandler CHAT_HANDLER = new ChatRequestMessageHandler();//--单聊---处理器
final GroupCreateRequestMessageHandler GROUP_CREATE_HANDLER = new GroupCreateRequestMessageHandler();//--创建群聊---处理器
final GroupChatRequestMessageHandler GROUP_CHAT_HANDLER = new GroupChatRequestMessageHandler(); //--群聊---处理器
final QuitHandler QUIT_HANDLER = new QuitHandler(); //--断开连接---处理器
LoggingHandler LOGGING_HANDLER = new LoggingHandler(LogLevel.DEBUG);//打印日志
LoginRequestMessageHandler LOGIN_HANDLER = new LoginRequestMessageHandler();
final ChatRequestMessageHandler CHAT_HANDLER = new ChatRequestMessageHandler();
final GroupCreateRequestMessageHandler GROUP_CREATE_HANDLER = new GroupCreateRequestMessageHandler();
final GroupChatRequestMessageHandler GROUP_CHAT_HANDLER = new GroupChatRequestMessageHandler();
final QuitHandler QUIT_HANDLER = new QuitHandler();
try {
final ServerBootstrap bs = new ServerBootstrap();
bs.channel(NioServerSocketChannel.class);
......@@ -45,8 +46,7 @@ public class ChatServer {
bs.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
// 用来判断 是不是读 空闲时间过长,或写空闲时间过长 (读,写,读写空闲时间限制) 0表示不关心
ch.pipeline().addLast(new IdleStateHandler(12, 0, 0));
ch.pipeline().addLast(IDLE_STATE_HANDLER);
/*
################################################################
##### ChannelDuplexHandler 可以同时作为 入站 和 出站处理器 #######
......@@ -65,6 +65,12 @@ public class ChatServer {
log.debug("==============================已经12秒没读到数据了!====================================");
ctx.channel().close();
}
if (event.state() == IdleState.WRITER_IDLE) {
log.debug("==============================写超时!====================================");
}
if (event.state() == IdleState.ALL_IDLE) {
log.debug("==============================读写超时!====================================");
}
}
});
ch.pipeline().addLast(QUIT_HANDLER); //--断开连接---处理器
......
......@@ -5,20 +5,15 @@ import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class OnlyTest {
/**
* 测试 集合的 computeIfPresent 如果存在key则计算
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/28 18:25
*/
public class ComputeIfPresentTest {
public static void main(String[] args) {
/*
// 【例题 1】
System.out.println(Config.getServerPort());
final int ordinal = Config.getMySerializerAlgorithm().ordinal();
System.out.println("ordinal = " + ordinal);
String s = "aaaaaaaaaa";
final byte[] bytes = Config.getMySerializerAlgorithm().serializ(s);
System.out.println(Arrays.toString(bytes));
*/
// 【例题 2】
// System.out.println(MySerializer.Algorithm.Json.ordinal());
// 【例题 3】 测试 集合的 computeIfPresent 如果存在key则计算
final Map<String, Set<String>> map = new HashMap<>();
final HashSet<String> set1 = new HashSet<>();
set1.add("张三");
......@@ -33,7 +28,6 @@ public class OnlyTest {
map.put("ql2", set2);
final boolean re = map.get("ql1").remove("abc");
System.out.println(re); // true or false
//map.get("ql6").add("赵六");
map.computeIfPresent("ql6", (k, v) -> {
v.add("赵六");
return v;
......
package com.kwan.shuyu.test;
import com.kwan.shuyu.config.Config;
import java.util.Arrays;
/**
* 测试config
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/28 18:18
*/
public class ConfigTest {
public static void main(String[] args) {
System.out.println(Config.getServerPort());
//ordinal返回枚举的位置下标
final int ordinal = Config.getMySerializerAlgorithm().ordinal();
System.out.println("ordinal = " + ordinal);
String s = "aaaaaaaaaa";
final byte[] bytes = Config.getMySerializerAlgorithm().serializ(s);
System.out.println(Arrays.toString(bytes));
}
}
\ No newline at end of file
package com.kwan.shuyu.test;
import com.kwan.shuyu.protocol.MySerializer;
/**
* ordinal测试
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/28 18:25
*/
public class OrdinalTest {
public static void main(String[] args) {
System.out.println(MySerializer.Algorithm.Json.ordinal());
}
}
\ No newline at end of file
......@@ -10,6 +10,14 @@ import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
/**
* 测试序列化算法
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/28 18:28
*/
public class TestAlgorithm {
public static void main(String[] args) {
// 局部变量
......@@ -17,24 +25,25 @@ public class TestAlgorithm {
LoggingHandler LOGGING_HANDLER = new LoggingHandler(LogLevel.DEBUG);
final EmbeddedChannel embeddedChannel = new EmbeddedChannel(
LOGGING_HANDLER,
// new ProcotolFrameDecoder(), // 帧解码器 【与自定义编解码器 MessageCodecSharable一起配置参数】
MESSAGE_CODEC,
LOGGING_HANDLER
);
LoginRequestMessage message = new LoginRequestMessage("张三", "123456");
/* ########################################################
####### 出站测试 【出站 自动编码】 encode ##########
########################################################*/
// embeddedChannel.writeOutbound(message);
/* #########################################################
####### 入站测试 【入站 自动解码】 decode ############
#########################################################*/
final ByteBuf buf = messageToByteBuf(message);
embeddedChannel.writeInbound(buf);
}
/**
* 消息转 ByteBuf
*
* @version : 2.2.0
* @author : qinyingjie
* @date : 2023/4/28 18:29
*/
private static ByteBuf messageToByteBuf(Message msg) {
ByteBuf out = ByteBufAllocator.DEFAULT.buffer();
//依次写入ByteBuf
out.writeBytes(new byte[]{1, 2, 3, 4}); // 4字节的 魔数
out.writeByte(1); // 1字节的 版本
out.writeByte(Config.getMySerializerAlgorithm().ordinal()); // 1字节的 序列化方式 0-jdk,1-json
......
......@@ -32,7 +32,7 @@ public class TestBacklogServer {
/**
* 调整 全连接队列大小 【注意这里和系统的取最小值为准,所以系统也要配】
*/
bs.option(ChannelOption.SO_BACKLOG, 5);// 超出报错:java.net.ConnectException: Connection refused: no further information
bs.option(ChannelOption.SO_BACKLOG, 2);// 超出报错:java.net.ConnectException: Connection refused: no further information
bs.group(new NioEventLoopGroup());
bs.channel(NioServerSocketChannel.class);
bs.childHandler(new ChannelInitializer<SocketChannel>() {
......
......@@ -10,12 +10,14 @@ import lombok.extern.slf4j.Slf4j;
@Slf4j
public class TestConnectionTimeout {
/*
配置参数:
1. 客户端通过 .option() 方法配置参数 给 SocketChannel 配置餐年数 (因为对于客户端来说就一个Socket)
2. 服务端
new ServerBootstrap().option() // 给 ServerSocketChannel 配置参数
new ServerBootstrap().childHandler() // 给 SocketChannel 配置参数
/**
* 配置参数:
* 1. 客户端通过 .option() 方法配置参数 给 SocketChannel 配置参数 (因为对于客户端来说就一个Socket)
* 2. 服务端
* new ServerBootstrap().option() // 给 ServerSocketChannel 配置参数
* new ServerBootstrap().childHandler() // 给 SocketChannel 配置参数
*
* @param args
*/
public static void main(String[] args) {
final NioEventLoopGroup group = new NioEventLoopGroup();
......
#端口号
server.port = 6000
# Java 选择对象流 序列化;Json 选择Json序列化
mySerializer.algorithm=Json
#\u7AEF\u53E3\u53F7
server.port=6000
# \u5E8F\u5217\u5316\u65B9\u5F0F Java Json
mySerializer.algorithm=Java
com.rpc.server.service.HelloService=com.rpc.server.service.HelloServiceImpl2
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册