创建服务端
NioEventLoopGroup serverGroup = new NioEventLoopGroup();
NioEventLoopGroup clientGroup = new NioEventLoopGroup();
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(serverGroup, clientGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_KEEPALIVE, true)
.childHandler(new ChannelInitializer<NioSocketChannel>() {
protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
nioSocketChannel.pipeline()
.addLast(new IdleStateHandler(0, 0, 30, TimeUnit.SECONDS))
.addLast(new LineBasedFrameDecoder(1024))
.addLast(new StringEncoder())
.addLast(new StringDecoder())
.addLast(new ServerHandler()); //自己实现处理器
}
}).bind(8181);
// 实现处理器
public class ServerHandler extends SimpleChannelInboundHandler<String> {
@Override
public void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
log.info("netty接收消息:" + msg);
ctx.channel().eventLoop().execute(() -> {
// 业务逻辑
});
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
log.error("netty客户端[{}]建立连接", ctx.channel().remoteAddress());
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
log.error("netty客户端[{}]和远程断开连接", ctx.channel().remoteAddress());
ctx.pipeline().remove(this);
ctx.channel().close();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
log.error("netty客户端异常[{}]", ctx.channel().remoteAddress(), cause);
ctx.pipeline().remove(this);
ctx.close();
}
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
if (evt instanceof IdleStateEvent) {
IdleState state = ((IdleStateEvent) evt).state();
if (state == IdleState.ALL_IDLE) {
ctx.writeAndFlush("heartbeatbeat!!");
}
} else {
super.userEventTriggered(ctx, evt);
}
}
}
创建客户端
EventLoopGroup group = new NioEventLoopGroup();
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(group)
.channel(NioSocketChannel.class)
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000)
.option(ChannelOption.SO_KEEPALIVE, true)
.handler(new ChannelInitializer<NioSocketChannel>() {
@Override
protected void initChannel(NioSocketChannel channel) throws Exception {
channel.pipeline()
.addLast(new IdleStateHandler(20, 0, 0, TimeUnit.SECONDS))
.addLast(new LineBasedFrameDecoder(1024))
.addLast(new StringEncoder())
.addLast(new StringDecoder())
.addLast(new ClientHandle()); //自定义处理器
}
});
ChannelFuture connect = bootstrap.connect("127.0.0.1", 2000);
connect.sync();
// 自定义处理器
public class ClientHandle extends SimpleChannelInboundHandler<String> {
@Override
public void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
System.out.println("读取" + msg);
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
System.out.println("channelInactive");
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("channelActive");
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
System.out.println("exceptionCaught");
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
System.out.println("channelReadComplete");
}
}
pipeline执行顺序
接收数据:从上到下执行实现ChannelInboundHandler 处理器
发送数据:从下到上执行实现ChannelOutboundHandler 处理器