导包
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.6.Final</version>
</dependency>
服务端
package com.zhk.routine.nio;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
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.codec.http.websocketx.extensions.compression.WebSocketServerCompressionHandler;
public class Server {
public static void main(String[] args) {
//来接收客户端传进来的连接
NioEventLoopGroup bossGroup = new NioEventLoopGroup();
//来处理已被接收的连接
NioEventLoopGroup workerGroup = new NioEventLoopGroup();
// 创建 netty 服务
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup, workerGroup)
// 设置 NIO 模式
.channel(NioServerSocketChannel.class)
// 设置 tcp 缓冲区
.option(ChannelOption.SO_BACKLOG,1024)
// 设置发送缓冲区数据大小
.childOption(ChannelOption.SO_SNDBUF,64 * 1024)
// 设置接收缓冲区数据大小
.option(ChannelOption.SO_RCVBUF,64 * 1024)
//保持长连接
.childOption(ChannelOption.SO_KEEPALIVE, true)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
// HttpClient编解码器
pipeline.addLast(new HttpServerCodec());
// 设置最大内容长度
pipeline.addLast(new HttpObjectAggregator(65536));
// WebSocket 数据压缩扩展
pipeline.addLast(new WebSocketServerCompressionHandler());
// WebSocket 握手、控制帧处理 /hello是路径
pipeline.addLast(new WebSocketServerProtocolHandler("/hello", null, true));
// 通道的初始化,数据传输过来进行拦截及执行
pipeline.addLast(new ConnectHandler());
}
});
// 绑定端口启动服务
ChannelFuture channelFuture = null;
try {
//8081端口号
channelFuture = serverBootstrap.bind(8081).sync();
channelFuture.channel().closeFuture().sync();
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
}
接收消息
package com.zhk.routine.nio;
import com.google.common.collect.Lists;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import java.util.ArrayList;
public class ConnectHandler extends ChannelInboundHandlerAdapter {
private static ArrayList<ChannelHandlerContext> list = Lists.newArrayList();
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("通道激活(回调)");
if (!list.contains(ctx)) {
System.out.println("添加");
list.add(ctx);
}
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
// 仅处理 TextWebSocketFrame
if (msg instanceof TextWebSocketFrame) {
String request = ((TextWebSocketFrame) msg).text();
System.out.println("收到请求:" + request);
list.forEach(channelHandlerContext ->
{
System.out.println("来了");
channelHandlerContext.writeAndFlush(new TextWebSocketFrame("我回复消息"));
});
}
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
System.out.println("数据读取完成");
}
}
测试
http://www.easyswoole.com/wstool.html