| | |
| | | @Component |
| | | public class WebSocketServer { |
| | | |
| | | @Value("${netty.websocket.port:9102}") |
| | | private int port; |
| | | |
| | | @Autowired |
| | | private ServerBootstrap serverBootstrap; |
| | | |
| | | @Autowired |
| | | private EventLoopGroup bossGroup; |
| | | |
| | | @Autowired |
| | | private EventLoopGroup workerGroup; |
| | | |
| | | private ChannelFuture channelFuture; |
| | | |
| | | @PostConstruct |
| | | public void start() throws InterruptedException { |
| | | channelFuture = serverBootstrap.bind(port).sync(); |
| | | System.out.println("Netty WebSocket服务器启动在端口:" + port); |
| | | } |
| | | |
| | | @PreDestroy |
| | | public void stop() { |
| | | if (channelFuture != null) { |
| | | channelFuture.channel().close(); |
| | | } |
| | | bossGroup.shutdownGracefully(); |
| | | workerGroup.shutdownGracefully(); |
| | | System.out.println("Netty WebSocket服务器已停止"); |
| | | } |
| | | // @Value("${netty.websocket.port:9102}") |
| | | // private int port; |
| | | // |
| | | // @Autowired |
| | | // private ServerBootstrap serverBootstrap; |
| | | // |
| | | // @Autowired |
| | | // private EventLoopGroup bossGroup; |
| | | // |
| | | // @Autowired |
| | | // private EventLoopGroup workerGroup; |
| | | // |
| | | // private ChannelFuture channelFuture; |
| | | // |
| | | // @PostConstruct |
| | | // public void start() throws InterruptedException { |
| | | // channelFuture = serverBootstrap.bind(port).sync(); |
| | | // System.out.println("Netty WebSocket服务器启动在端口:" + port); |
| | | // } |
| | | // |
| | | // @PreDestroy |
| | | // public void stop() { |
| | | // if (channelFuture != null) { |
| | | // channelFuture.channel().close(); |
| | | // } |
| | | // bossGroup.shutdownGracefully(); |
| | | // workerGroup.shutdownGracefully(); |
| | | // System.out.println("Netty WebSocket服务器已停止"); |
| | | // } |
| | | } |