From 3db26019d7de562f89fd7e3fa8677b3f0550d60c Mon Sep 17 00:00:00 2001 From: wxs <18771603711@sina.cn> Date: Wed, 31 May 2023 14:23:45 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0netty-java=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E7=AB=AF=EF=BC=8C=E7=94=A8=E4=BA=8E=E6=8E=A5=E6=94=B6hook?= =?UTF-8?q?=E6=B6=88=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- java/WeChatHookNettyServer.java | 88 +++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 java/WeChatHookNettyServer.java diff --git a/java/WeChatHookNettyServer.java b/java/WeChatHookNettyServer.java new file mode 100644 index 0000000..59787cf --- /dev/null +++ b/java/WeChatHookNettyServer.java @@ -0,0 +1,88 @@ +package com.rongan.cloud.netty.server; + +import com.alibaba.fastjson.JSON; +import io.netty.bootstrap.ServerBootstrap; +import io.netty.channel.Channel; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.ChannelOption; +import io.netty.channel.SimpleChannelInboundHandler; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.SocketChannel; +import io.netty.channel.socket.nio.NioServerSocketChannel; +import io.netty.handler.codec.DelimiterBasedFrameDecoder; +import io.netty.handler.codec.Delimiters; +import io.netty.handler.codec.string.StringDecoder; +import io.netty.handler.codec.string.StringEncoder; +import io.netty.util.CharsetUtil; + +import java.net.InetSocketAddress; + +/** + * @PACKAGE_NAME: com.rongan.cloud.netty.server + * @NAME: NettyServer + * @AUTHOR: wxs + * @DATE: 2023/5/31 13:31 + * @PROJECT_NAME: weChatHook + **/ +public class WeChatHookNettyServer { + + /** + * netty 依赖 + * + * io.netty + * netty-all + * 4.1.51.Final + * + */ + private static final int PORT = 19099; + + public static void main(String[] args) { + NioEventLoopGroup bossGroup = new NioEventLoopGroup(); + NioEventLoopGroup workerGroup = new NioEventLoopGroup(); + try { + ServerBootstrap serverBootstrap = new ServerBootstrap(); + serverBootstrap.group(bossGroup, workerGroup) + .channel(NioServerSocketChannel.class) + .localAddress(new InetSocketAddress(PORT)) + .childHandler(new ChannelInitializer() { + @Override + protected void initChannel(SocketChannel ch) { + ch.pipeline().addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter())); + ch.pipeline().addLast(new StringDecoder(CharsetUtil.UTF_8)); + ch.pipeline().addLast(new StringEncoder(CharsetUtil.UTF_8)); + ch.pipeline().addLast(new ReceiveMsgHandler()); + } + }) + .option(ChannelOption.SO_BACKLOG, 128) + .childOption(ChannelOption.SO_KEEPALIVE, true); + + Channel channel = serverBootstrap.bind().sync().channel(); + System.out.println("Server started on port " + PORT); + channel.closeFuture().sync(); + } catch (InterruptedException e) { + e.printStackTrace(); + } finally { + bossGroup.shutdownGracefully(); + workerGroup.shutdownGracefully(); + } + } + + private static class ReceiveMsgHandler extends SimpleChannelInboundHandler { + @Override + protected void channelRead0(ChannelHandlerContext ctx, String msg) { + JSON.parseObject(msg).forEach((k,v)->{ + System.out.println(k+" = " + v); + }); + System.out.println("----------end----------"); + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { + cause.printStackTrace(); + ctx.close(); + } + } + + +}