|
@@ -1,82 +1,82 @@
|
|
|
-package com.dzdy.dim.service;
|
|
|
-
|
|
|
-import com.dzdy.dim.handler.IMIdleStateHandler;
|
|
|
-import com.dzdy.dim.handler.PacketCodecHandler;
|
|
|
-import com.dzdy.dim.handler.Spliter;
|
|
|
-import com.dzdy.dim.service.handler.AuthHandler;
|
|
|
-import com.dzdy.dim.service.handler.ConfirmRequestHandler;
|
|
|
-import com.dzdy.dim.service.handler.HeartBeatRequestHandler;
|
|
|
-import com.dzdy.dim.service.handler.LoginRequestHandler;
|
|
|
-import io.netty.bootstrap.ServerBootstrap;
|
|
|
-import io.netty.channel.ChannelInitializer;
|
|
|
-import io.netty.channel.ChannelOption;
|
|
|
-import io.netty.channel.nio.NioEventLoopGroup;
|
|
|
-import io.netty.channel.socket.nio.NioServerSocketChannel;
|
|
|
-import io.netty.channel.socket.nio.NioSocketChannel;
|
|
|
-
|
|
|
-/**
|
|
|
- * @author : wangzhiyong
|
|
|
- * @date : 2019/5/23 17:36
|
|
|
- * description :
|
|
|
- */
|
|
|
-public class NettyServer {
|
|
|
-
|
|
|
- private static final int PORT = 9001;
|
|
|
-
|
|
|
- public static void main(String[] args) {
|
|
|
- final ServerBootstrap serverBootstrap = new ServerBootstrap();
|
|
|
-
|
|
|
- //接受连接
|
|
|
- NioEventLoopGroup boss = new NioEventLoopGroup();
|
|
|
- //异步非阻塞处理数据
|
|
|
- NioEventLoopGroup worker = new NioEventLoopGroup();
|
|
|
- serverBootstrap.group(boss, worker)
|
|
|
- //指定我们服务端的 IO 模型为NIO
|
|
|
- .channel(NioServerSocketChannel.class)
|
|
|
- // 临时存放已完成三次握手的请求的队列的最大长度 如果连接建立频繁,服务器处理创建新连接较慢,可以适当调大这个参数
|
|
|
- .option(ChannelOption.SO_BACKLOG, 1024)
|
|
|
- // TCP 心跳机制 true 开启
|
|
|
- .childOption(ChannelOption.SO_KEEPALIVE, true)
|
|
|
- // 是否关闭Nagle算法 true 高实时性,有消息立即发送 false 减少发送次数减少网络交互 提升性能(Nagle 算法)
|
|
|
- .childOption(ChannelOption.TCP_NODELAY, true)
|
|
|
- //指定启动前的逻辑操作
|
|
|
- .handler(new ChannelInitializer<NioServerSocketChannel>() {
|
|
|
- @Override
|
|
|
- protected void initChannel(NioServerSocketChannel ch) throws Exception {
|
|
|
- System.out.println("服务端启动中");
|
|
|
- }
|
|
|
- })
|
|
|
- //用于指定处理新连接数据
|
|
|
- .childHandler(new ChannelInitializer<NioSocketChannel>() {
|
|
|
- @Override
|
|
|
- protected void initChannel(NioSocketChannel ch) {
|
|
|
- ch.pipeline().addLast(new IMIdleStateHandler());
|
|
|
- ch.pipeline().addLast(new Spliter());
|
|
|
- ch.pipeline().addLast(PacketCodecHandler.INSTANCE);
|
|
|
- ch.pipeline().addLast(LoginRequestHandler.INSTANCE);
|
|
|
- ch.pipeline().addLast(HeartBeatRequestHandler.INSTANCE);
|
|
|
- ch.pipeline().addLast(AuthHandler.INSTANCE);
|
|
|
- ch.pipeline().addLast(ConfirmRequestHandler.INSTANCE);
|
|
|
- }
|
|
|
- });
|
|
|
- bind(serverBootstrap, PORT);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 异步监听 绑定端口是否成功 并加大端口号继续绑定
|
|
|
- *
|
|
|
- * @param bootstrap
|
|
|
- * @param port
|
|
|
- */
|
|
|
- private static void bind(ServerBootstrap bootstrap, int port) {
|
|
|
- bootstrap.bind(port).addListener(future -> {
|
|
|
- if (future.isSuccess()) {
|
|
|
- System.out.println("端口[" + port + "]绑定成功");
|
|
|
- } else {
|
|
|
- System.out.println("端口[" + port + "]绑定失败");
|
|
|
- bind(bootstrap, port + 1);
|
|
|
- }
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
-}
|
|
|
+//package com.dzdy.dim.service;
|
|
|
+//
|
|
|
+//import com.dzdy.dim.handler.IMIdleStateHandler;
|
|
|
+//import com.dzdy.dim.handler.PacketCodecHandler;
|
|
|
+//import com.dzdy.dim.handler.Spliter;
|
|
|
+//import com.dzdy.dim.service.handler.AuthHandler;
|
|
|
+//import com.dzdy.dim.service.handler.ConfirmRequestHandler;
|
|
|
+//import com.dzdy.dim.service.handler.HeartBeatRequestHandler;
|
|
|
+//import com.dzdy.dim.service.handler.LoginRequestHandler;
|
|
|
+//import io.netty.bootstrap.ServerBootstrap;
|
|
|
+//import io.netty.channel.ChannelInitializer;
|
|
|
+//import io.netty.channel.ChannelOption;
|
|
|
+//import io.netty.channel.nio.NioEventLoopGroup;
|
|
|
+//import io.netty.channel.socket.nio.NioServerSocketChannel;
|
|
|
+//import io.netty.channel.socket.nio.NioSocketChannel;
|
|
|
+//
|
|
|
+///**
|
|
|
+// * @author : wangzhiyong
|
|
|
+// * @date : 2019/5/23 17:36
|
|
|
+// * description :
|
|
|
+// */
|
|
|
+//public class NettyServer {
|
|
|
+//
|
|
|
+// private static final int PORT = 9001;
|
|
|
+//
|
|
|
+// public static void main(String[] args) {
|
|
|
+// final ServerBootstrap serverBootstrap = new ServerBootstrap();
|
|
|
+//
|
|
|
+// //接受连接
|
|
|
+// NioEventLoopGroup boss = new NioEventLoopGroup();
|
|
|
+// //异步非阻塞处理数据
|
|
|
+// NioEventLoopGroup worker = new NioEventLoopGroup();
|
|
|
+// serverBootstrap.group(boss, worker)
|
|
|
+// //指定我们服务端的 IO 模型为NIO
|
|
|
+// .channel(NioServerSocketChannel.class)
|
|
|
+// // 临时存放已完成三次握手的请求的队列的最大长度 如果连接建立频繁,服务器处理创建新连接较慢,可以适当调大这个参数
|
|
|
+// .option(ChannelOption.SO_BACKLOG, 1024)
|
|
|
+// // TCP 心跳机制 true 开启
|
|
|
+// .childOption(ChannelOption.SO_KEEPALIVE, true)
|
|
|
+// // 是否关闭Nagle算法 true 高实时性,有消息立即发送 false 减少发送次数减少网络交互 提升性能(Nagle 算法)
|
|
|
+// .childOption(ChannelOption.TCP_NODELAY, true)
|
|
|
+// //指定启动前的逻辑操作
|
|
|
+// .handler(new ChannelInitializer<NioServerSocketChannel>() {
|
|
|
+// @Override
|
|
|
+// protected void initChannel(NioServerSocketChannel ch) {
|
|
|
+// System.out.println("服务端启动中");
|
|
|
+// }
|
|
|
+// })
|
|
|
+// //用于指定处理新连接数据
|
|
|
+// .childHandler(new ChannelInitializer<NioSocketChannel>() {
|
|
|
+// @Override
|
|
|
+// protected void initChannel(NioSocketChannel ch) {
|
|
|
+// ch.pipeline().addLast(new IMIdleStateHandler());
|
|
|
+// ch.pipeline().addLast(new Spliter());
|
|
|
+// ch.pipeline().addLast(PacketCodecHandler.INSTANCE);
|
|
|
+// ch.pipeline().addLast(LoginRequestHandler.INSTANCE);
|
|
|
+// ch.pipeline().addLast(HeartBeatRequestHandler.INSTANCE);
|
|
|
+// ch.pipeline().addLast(AuthHandler.INSTANCE);
|
|
|
+// ch.pipeline().addLast(ConfirmRequestHandler.INSTANCE);
|
|
|
+// }
|
|
|
+// });
|
|
|
+// bind(serverBootstrap, PORT);
|
|
|
+// }
|
|
|
+//
|
|
|
+// /**
|
|
|
+// * 异步监听 绑定端口是否成功 并加大端口号继续绑定
|
|
|
+// *
|
|
|
+// * @param bootstrap
|
|
|
+// * @param port
|
|
|
+// */
|
|
|
+// private static void bind(ServerBootstrap bootstrap, int port) {
|
|
|
+// bootstrap.bind(port).addListener(future -> {
|
|
|
+// if (future.isSuccess()) {
|
|
|
+// System.out.println("端口[" + port + "]绑定成功");
|
|
|
+// } else {
|
|
|
+// System.out.println("端口[" + port + "]绑定失败");
|
|
|
+// bind(bootstrap, port + 1);
|
|
|
+// }
|
|
|
+// });
|
|
|
+// }
|
|
|
+//
|
|
|
+//}
|