From ef5a518c3e2c1fe41aa60f56ca31ddafa0cb1561 Mon Sep 17 00:00:00 2001 From: guoyuji <guoyujie@ng.com> Date: 星期四, 23 五月 2024 08:32:36 +0800 Subject: [PATCH] 添加产品汇总报表 --- north-glass-erp/src/main/java/com/example/erp/tools/netty/NettyServer.java | 62 +++++++++++++++++++++++++++++++ 1 files changed, 62 insertions(+), 0 deletions(-) diff --git a/north-glass-erp/src/main/java/com/example/erp/tools/netty/NettyServer.java b/north-glass-erp/src/main/java/com/example/erp/tools/netty/NettyServer.java new file mode 100644 index 0000000..b2e66bd --- /dev/null +++ b/north-glass-erp/src/main/java/com/example/erp/tools/netty/NettyServer.java @@ -0,0 +1,62 @@ +package com.example.erp.tools.netty; + +import io.netty.bootstrap.ServerBootstrap; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.ChannelOption; +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.http.HttpObjectAggregator; +import io.netty.handler.codec.http.HttpServerCodec; +import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler; +import io.netty.handler.stream.ChunkedWriteHandler; + +/** + * NettyServer Netty鏈嶅姟鍣ㄩ厤缃� + * @author zhengkai.blog.csdn.net + * @date 2019-06-12 + */ +public class NettyServer { + private final int port; + + public NettyServer(int port) { + this.port = port; + } + + public void start() throws Exception { + EventLoopGroup bossGroup = new NioEventLoopGroup(); + + EventLoopGroup group = new NioEventLoopGroup(); + try { + ServerBootstrap sb = new ServerBootstrap(); + sb.option(ChannelOption.SO_BACKLOG, 1024); + sb.group(group, bossGroup) // 缁戝畾绾跨▼姹� + .channel(NioServerSocketChannel.class) // 鎸囧畾浣跨敤鐨刢hannel + .localAddress(this.port)// 缁戝畾鐩戝惉绔彛 + .childHandler(new ChannelInitializer<SocketChannel>() { // 缁戝畾瀹㈡埛绔繛鎺ユ椂鍊欒Е鍙戞搷浣� + + @Override + protected void initChannel(SocketChannel ch) throws Exception { + // System.out.println("鏀跺埌鏂拌繛鎺�"); + //websocket鍗忚鏈韩鏄熀浜巋ttp鍗忚鐨勶紝鎵�浠ヨ繖杈逛篃瑕佷娇鐢╤ttp瑙g紪鐮佸櫒 + ch.pipeline().addLast(new HttpServerCodec()); + //浠ュ潡鐨勬柟寮忔潵鍐欑殑澶勭悊鍣� + ch.pipeline().addLast(new ChunkedWriteHandler()); + ch.pipeline().addLast(new HttpObjectAggregator(8192)); + ch.pipeline().addLast(new MyWebSocketHandler()); + ch.pipeline().addLast(new WebSocketServerProtocolHandler("/ws", null, true, 65536 * 10)); + } + }); + ChannelFuture cf = sb.bind().sync(); // 鏈嶅姟鍣ㄥ紓姝ュ垱寤虹粦瀹� + System.out.println(NettyServer.class + " 鍚姩姝e湪鐩戝惉锛� " + cf.channel().localAddress()); + cf.channel().closeFuture().sync(); // 鍏抽棴鏈嶅姟鍣ㄩ�氶亾 + } finally { + group.shutdownGracefully().sync(); // 閲婃斁绾跨▼姹犺祫婧� + bossGroup.shutdownGracefully().sync(); + } + } +} + + -- Gitblit v1.8.0