|
|
@@ -1,7 +1,10 @@
|
|
|
package com.inspur.netty.server;
|
|
|
|
|
|
import com.inspur.netty.handler.*;
|
|
|
+import com.inspur.netty.util.NettyConstants;
|
|
|
import io.netty.bootstrap.ServerBootstrap;
|
|
|
+import io.netty.buffer.ByteBuf;
|
|
|
+import io.netty.buffer.Unpooled;
|
|
|
import io.netty.channel.ChannelFuture;
|
|
|
import io.netty.channel.ChannelInitializer;
|
|
|
import io.netty.channel.ChannelOption;
|
|
|
@@ -9,6 +12,7 @@ import io.netty.channel.EventLoopGroup;
|
|
|
import io.netty.channel.nio.NioEventLoopGroup;
|
|
|
import io.netty.channel.socket.SocketChannel;
|
|
|
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
|
|
+import io.netty.handler.codec.DelimiterBasedFrameDecoder;
|
|
|
import io.netty.handler.timeout.IdleStateHandler;
|
|
|
import lombok.SneakyThrows;
|
|
|
import org.slf4j.Logger;
|
|
|
@@ -16,6 +20,7 @@ import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
/**
|
|
|
@@ -28,6 +33,12 @@ public class NettyServer {
|
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(NettyServer.class);
|
|
|
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将字符串分隔符 "####" 转换为 ByteBuf
|
|
|
+ */
|
|
|
+ private static final ByteBuf delimiter = Unpooled.copiedBuffer(NettyConstants.DATA_PACK_SEPARATOR, StandardCharsets.UTF_8);
|
|
|
+
|
|
|
@Bean
|
|
|
public void startNettyServer(){
|
|
|
new Thread(new Runnable() {
|
|
|
@@ -54,6 +65,7 @@ public class NettyServer {
|
|
|
@Override
|
|
|
protected void initChannel(SocketChannel channel) throws Exception {
|
|
|
channel.pipeline().addLast(new IdleStateHandler(60, 0, 0, TimeUnit.SECONDS));
|
|
|
+ channel.pipeline().addLast(new DelimiterBasedFrameDecoder(1024, delimiter));
|
|
|
channel.pipeline().addLast(new AuthServerHandler());
|
|
|
channel.pipeline().addLast(new ConnectServerHandler());
|
|
|
channel.pipeline().addLast(new HeartServerHandler());
|
|
|
@@ -69,6 +81,7 @@ public class NettyServer {
|
|
|
channel.pipeline().addLast(new TaskPlayControlHandler());
|
|
|
channel.pipeline().addLast(new TaskOtaHandler());
|
|
|
channel.pipeline().addLast(new TaskLogPushStartHandler());
|
|
|
+ channel.pipeline().addLast(new MyFinalBusinessHandler());
|
|
|
}
|
|
|
});
|
|
|
ChannelFuture f = b.bind(18081).sync();
|