增加netty-java服务端,用于接收hook消息
This commit is contained in:
parent
8348a2f8db
commit
3db26019d7
88
java/WeChatHookNettyServer.java
Normal file
88
java/WeChatHookNettyServer.java
Normal file
@ -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 依赖
|
||||
* <dependency>
|
||||
* <groupId>io.netty</groupId>
|
||||
* <artifactId>netty-all</artifactId>
|
||||
* <version>4.1.51.Final</version>
|
||||
* </dependency>
|
||||
*/
|
||||
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<SocketChannel>() {
|
||||
@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<String> {
|
||||
@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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user