|
@@ -1,97 +1,97 @@
|
|
|
-package com.fs.app.websocket.service;
|
|
|
-
|
|
|
-import com.fs.app.websocket.auth.AuthHandler;
|
|
|
-import com.fs.app.websocket.handle.LiveChatHandler;
|
|
|
-import io.netty.bootstrap.ServerBootstrap;
|
|
|
-import io.netty.channel.*;
|
|
|
-import io.netty.channel.nio.NioEventLoopGroup;
|
|
|
-import io.netty.channel.socket.SocketChannel;
|
|
|
-import io.netty.channel.socket.nio.NioServerSocketChannel;
|
|
|
-import io.netty.handler.codec.http.HttpObjectAggregator;
|
|
|
-import io.netty.handler.codec.http.HttpServerCodec;
|
|
|
-import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.boot.CommandLineRunner;
|
|
|
-import org.springframework.stereotype.Component;
|
|
|
-
|
|
|
-import javax.annotation.PreDestroy;
|
|
|
-import java.util.Objects;
|
|
|
-
|
|
|
-@Slf4j
|
|
|
-@Component
|
|
|
-public class NettyServerRunner implements CommandLineRunner {
|
|
|
-
|
|
|
- private static final int port = 17114;
|
|
|
- private EventLoopGroup bossGroup;
|
|
|
- private EventLoopGroup workerGroup;
|
|
|
- private Channel serverChannel;
|
|
|
- @Autowired
|
|
|
- private AuthHandler authHandler;
|
|
|
- @Autowired
|
|
|
- private LiveChatHandler liveChatHandler;
|
|
|
-
|
|
|
- @Override
|
|
|
- public void run(String... args) throws Exception {
|
|
|
- new Thread(this::startServer).start();
|
|
|
- }
|
|
|
-
|
|
|
- private void startServer() {
|
|
|
- bossGroup = new NioEventLoopGroup(); // 处理连接
|
|
|
- workerGroup = new NioEventLoopGroup(); // 处理I/O
|
|
|
- try {
|
|
|
- ServerBootstrap bootstrap = new ServerBootstrap();
|
|
|
- bootstrap.group(bossGroup, workerGroup)
|
|
|
- .channel(NioServerSocketChannel.class)
|
|
|
- .childHandler(new ChannelInitializer<SocketChannel>() {
|
|
|
-
|
|
|
- @Override
|
|
|
- protected void initChannel(SocketChannel socketChannel) throws Exception {
|
|
|
- ChannelPipeline pipeline = socketChannel.pipeline();
|
|
|
- // 编解码
|
|
|
- pipeline.addLast(new HttpServerCodec());
|
|
|
- // 集合消息
|
|
|
- pipeline.addLast(new HttpObjectAggregator(65536));
|
|
|
- // 安全校验
|
|
|
- pipeline.addLast(authHandler);
|
|
|
- // websocket握手
|
|
|
- pipeline.addLast(new WebSocketServerProtocolHandler("/app/webSocket", null, true, 65536, false, true));
|
|
|
- // 自定义聊天
|
|
|
- pipeline.addLast(liveChatHandler);
|
|
|
- }
|
|
|
- })
|
|
|
- .option(ChannelOption.SO_BACKLOG, 1024)
|
|
|
- .childOption(ChannelOption.SO_KEEPALIVE, true);
|
|
|
-
|
|
|
- ChannelFuture future = bootstrap.bind(port).sync();
|
|
|
- serverChannel = future.channel();
|
|
|
- log.info("netty server started [{}]", port);
|
|
|
- serverChannel.closeFuture().sync();
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("netty server error msg: {}", e.getMessage(), e);
|
|
|
- } finally {
|
|
|
- shutdown();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @PreDestroy
|
|
|
- public void destroy() {
|
|
|
- shutdown();
|
|
|
- log.info("netty server destroy");
|
|
|
- }
|
|
|
-
|
|
|
- private void shutdown() {
|
|
|
- if (Objects.nonNull(bossGroup)) {
|
|
|
- bossGroup.shutdownGracefully();
|
|
|
- }
|
|
|
-
|
|
|
- if (Objects.nonNull(workerGroup)) {
|
|
|
- workerGroup.shutdownGracefully();
|
|
|
- }
|
|
|
-
|
|
|
- if (Objects.nonNull(serverChannel)) {
|
|
|
- serverChannel.close();
|
|
|
- }
|
|
|
- log.info("netty server stopped");
|
|
|
- }
|
|
|
-}
|
|
|
+//package com.fs.app.websocket.service;
|
|
|
+//
|
|
|
+//import com.fs.app.websocket.auth.AuthHandler;
|
|
|
+//import com.fs.app.websocket.handle.LiveChatHandler;
|
|
|
+//import io.netty.bootstrap.ServerBootstrap;
|
|
|
+//import io.netty.channel.*;
|
|
|
+//import io.netty.channel.nio.NioEventLoopGroup;
|
|
|
+//import io.netty.channel.socket.SocketChannel;
|
|
|
+//import io.netty.channel.socket.nio.NioServerSocketChannel;
|
|
|
+//import io.netty.handler.codec.http.HttpObjectAggregator;
|
|
|
+//import io.netty.handler.codec.http.HttpServerCodec;
|
|
|
+//import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
|
|
|
+//import lombok.extern.slf4j.Slf4j;
|
|
|
+//import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+//import org.springframework.boot.CommandLineRunner;
|
|
|
+//import org.springframework.stereotype.Component;
|
|
|
+//
|
|
|
+//import javax.annotation.PreDestroy;
|
|
|
+//import java.util.Objects;
|
|
|
+//
|
|
|
+//@Slf4j
|
|
|
+//@Component
|
|
|
+//public class NettyServerRunner implements CommandLineRunner {
|
|
|
+//
|
|
|
+// private static final int port = 17114;
|
|
|
+// private EventLoopGroup bossGroup;
|
|
|
+// private EventLoopGroup workerGroup;
|
|
|
+// private Channel serverChannel;
|
|
|
+// @Autowired
|
|
|
+// private AuthHandler authHandler;
|
|
|
+// @Autowired
|
|
|
+// private LiveChatHandler liveChatHandler;
|
|
|
+//
|
|
|
+// @Override
|
|
|
+// public void run(String... args) throws Exception {
|
|
|
+// new Thread(this::startServer).start();
|
|
|
+// }
|
|
|
+//
|
|
|
+// private void startServer() {
|
|
|
+// bossGroup = new NioEventLoopGroup(); // 处理连接
|
|
|
+// workerGroup = new NioEventLoopGroup(); // 处理I/O
|
|
|
+// try {
|
|
|
+// ServerBootstrap bootstrap = new ServerBootstrap();
|
|
|
+// bootstrap.group(bossGroup, workerGroup)
|
|
|
+// .channel(NioServerSocketChannel.class)
|
|
|
+// .childHandler(new ChannelInitializer<SocketChannel>() {
|
|
|
+//
|
|
|
+// @Override
|
|
|
+// protected void initChannel(SocketChannel socketChannel) throws Exception {
|
|
|
+// ChannelPipeline pipeline = socketChannel.pipeline();
|
|
|
+// // 编解码
|
|
|
+// pipeline.addLast(new HttpServerCodec());
|
|
|
+// // 集合消息
|
|
|
+// pipeline.addLast(new HttpObjectAggregator(65536));
|
|
|
+// // 安全校验
|
|
|
+// pipeline.addLast(authHandler);
|
|
|
+// // websocket握手
|
|
|
+// pipeline.addLast(new WebSocketServerProtocolHandler("/app/webSocket", null, true, 65536, false, true));
|
|
|
+// // 自定义聊天
|
|
|
+// pipeline.addLast(liveChatHandler);
|
|
|
+// }
|
|
|
+// })
|
|
|
+// .option(ChannelOption.SO_BACKLOG, 1024)
|
|
|
+// .childOption(ChannelOption.SO_KEEPALIVE, true);
|
|
|
+//
|
|
|
+// ChannelFuture future = bootstrap.bind(port).sync();
|
|
|
+// serverChannel = future.channel();
|
|
|
+// log.info("netty server started [{}]", port);
|
|
|
+// serverChannel.closeFuture().sync();
|
|
|
+// } catch (Exception e) {
|
|
|
+// log.error("netty server error msg: {}", e.getMessage(), e);
|
|
|
+// } finally {
|
|
|
+// shutdown();
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// @PreDestroy
|
|
|
+// public void destroy() {
|
|
|
+// shutdown();
|
|
|
+// log.info("netty server destroy");
|
|
|
+// }
|
|
|
+//
|
|
|
+// private void shutdown() {
|
|
|
+// if (Objects.nonNull(bossGroup)) {
|
|
|
+// bossGroup.shutdownGracefully();
|
|
|
+// }
|
|
|
+//
|
|
|
+// if (Objects.nonNull(workerGroup)) {
|
|
|
+// workerGroup.shutdownGracefully();
|
|
|
+// }
|
|
|
+//
|
|
|
+// if (Objects.nonNull(serverChannel)) {
|
|
|
+// serverChannel.close();
|
|
|
+// }
|
|
|
+// log.info("netty server stopped");
|
|
|
+// }
|
|
|
+//}
|