|
@@ -1,51 +1,51 @@
|
|
|
-package com.zhentao.information.netty;
|
|
|
-
|
|
|
-import com.zhentao.information.handler.WebSocketHandler;
|
|
|
-import io.netty.bootstrap.ServerBootstrap;
|
|
|
-import io.netty.channel.ChannelInitializer;
|
|
|
-import io.netty.channel.socket.SocketChannel;
|
|
|
-import io.netty.handler.codec.http.HttpObjectAggregator;
|
|
|
-import io.netty.handler.codec.http.HttpServerCodec;
|
|
|
-import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
|
|
|
-import io.netty.handler.stream.ChunkedWriteHandler;
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.springframework.beans.factory.annotation.Value;
|
|
|
-import org.springframework.stereotype.Component;
|
|
|
-
|
|
|
-import javax.annotation.PostConstruct;
|
|
|
-import javax.annotation.Resource;
|
|
|
-
|
|
|
-/**
|
|
|
- * Netty服务器启动类
|
|
|
- */
|
|
|
-@Slf4j
|
|
|
-@Component
|
|
|
-public class NettyServer {
|
|
|
-
|
|
|
- @Value("${netty.port:8888}")
|
|
|
- private int port;
|
|
|
-
|
|
|
- @Resource
|
|
|
- private ServerBootstrap serverBootstrap;
|
|
|
-
|
|
|
- @Resource
|
|
|
- private WebSocketHandler webSocketHandler;
|
|
|
-
|
|
|
- @PostConstruct
|
|
|
- public void start() throws Exception {
|
|
|
- serverBootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
|
|
|
- @Override
|
|
|
- protected void initChannel(SocketChannel ch) {
|
|
|
- ch.pipeline()
|
|
|
- .addLast(new HttpServerCodec())
|
|
|
- .addLast(new ChunkedWriteHandler())
|
|
|
- .addLast(new HttpObjectAggregator(65536))
|
|
|
- .addLast(new WebSocketServerProtocolHandler("/ws"))
|
|
|
- .addLast(webSocketHandler);
|
|
|
- }
|
|
|
- });
|
|
|
-
|
|
|
- serverBootstrap.bind(port).sync();
|
|
|
- log.info("Netty服务器启动成功,端口:{}", port);
|
|
|
- }
|
|
|
-}
|
|
|
+//package com.zhentao.information.netty;
|
|
|
+//
|
|
|
+//import com.zhentao.information.handler.WebSocketHandler;
|
|
|
+//import io.netty.bootstrap.ServerBootstrap;
|
|
|
+//import io.netty.channel.ChannelInitializer;
|
|
|
+//import io.netty.channel.socket.SocketChannel;
|
|
|
+//import io.netty.handler.codec.http.HttpObjectAggregator;
|
|
|
+//import io.netty.handler.codec.http.HttpServerCodec;
|
|
|
+//import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
|
|
|
+//import io.netty.handler.stream.ChunkedWriteHandler;
|
|
|
+//import lombok.extern.slf4j.Slf4j;
|
|
|
+//import org.springframework.beans.factory.annotation.Value;
|
|
|
+//import org.springframework.stereotype.Component;
|
|
|
+//
|
|
|
+//import javax.annotation.PostConstruct;
|
|
|
+//import javax.annotation.Resource;
|
|
|
+//
|
|
|
+///**
|
|
|
+// * Netty服务器启动类
|
|
|
+// */
|
|
|
+//@Slf4j
|
|
|
+//@Component
|
|
|
+//public class NettyServer {
|
|
|
+//
|
|
|
+// @Value("${netty.port:8888}")
|
|
|
+// private int port;
|
|
|
+//
|
|
|
+// @Resource
|
|
|
+// private ServerBootstrap serverBootstrap;
|
|
|
+//
|
|
|
+// @Resource
|
|
|
+// private WebSocketHandler webSocketHandler;
|
|
|
+//
|
|
|
+// @PostConstruct
|
|
|
+// public void start() throws Exception {
|
|
|
+// serverBootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
|
|
|
+// @Override
|
|
|
+// protected void initChannel(SocketChannel ch) {
|
|
|
+// ch.pipeline()
|
|
|
+// .addLast(new HttpServerCodec())
|
|
|
+// .addLast(new ChunkedWriteHandler())
|
|
|
+// .addLast(new HttpObjectAggregator(65536))
|
|
|
+// .addLast(new WebSocketServerProtocolHandler("/ws"))
|
|
|
+// .addLast(webSocketHandler);
|
|
|
+// }
|
|
|
+// });
|
|
|
+//
|
|
|
+// serverBootstrap.bind(port).sync();
|
|
|
+// log.info("Netty服务器启动成功,端口:{}", port);
|
|
|
+// }
|
|
|
+//}
|