netty自定义解码器 netty
程序员文章站
2022-05-06 20:29:04
...
ByteToMessageDecoder
public class CustomDecoder extends ByteToMessageDecoder {
protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> list) throws Exception {
try {
ByteBuffer buffer = buf.nioBuffer();
int length = buffer.limit();
byte[] data = new byte[length];
buffer.get(data);
String json = new String(data, "utf-8");
System.out.println("json:"+json);
if(!json.startsWith("{")) {
return ;
}
User user = JSON.parseObject(json, User.class);
list.add(user); //解码成User对象,从ByteBuf里读取字节流
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class NettyServer {
private int port=8088;
public static void main(String[] args) {
NettyServer server = new NettyServer();
server.run();
}
void run() {
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
public void initChannel(SocketChannel ch) {
ChannelPipeline p =ch.pipeline();
p.addLast(new CustomDecoder()); //handler里的Object msg可以转换成解码后的对象
p.addLast(new SeverHandler());
}}
).option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true);
ChannelFuture f = b.bind(port).sync();
f.channel().closeFuture().sync();
}catch(Exception e) {
e.printStackTrace();
}finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
public class SeverHandler extends ChannelInboundHandlerAdapter {
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ctx.writeAndFlush("this is a test!");
User user = (User)msg;
System.out.println("user: "+JSON.toJSONString(user));
}
}
public class CustomDecoder extends ByteToMessageDecoder {
protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> list) throws Exception {
try {
ByteBuffer buffer = buf.nioBuffer();
int length = buffer.limit();
byte[] data = new byte[length];
buffer.get(data);
String json = new String(data, "utf-8");
System.out.println("json:"+json);
if(!json.startsWith("{")) {
return ;
}
User user = JSON.parseObject(json, User.class);
list.add(user); //解码成User对象,从ByteBuf里读取字节流
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class NettyServer {
private int port=8088;
public static void main(String[] args) {
NettyServer server = new NettyServer();
server.run();
}
void run() {
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
public void initChannel(SocketChannel ch) {
ChannelPipeline p =ch.pipeline();
p.addLast(new CustomDecoder()); //handler里的Object msg可以转换成解码后的对象
p.addLast(new SeverHandler());
}}
).option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true);
ChannelFuture f = b.bind(port).sync();
f.channel().closeFuture().sync();
}catch(Exception e) {
e.printStackTrace();
}finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
public class SeverHandler extends ChannelInboundHandlerAdapter {
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ctx.writeAndFlush("this is a test!");
User user = (User)msg;
System.out.println("user: "+JSON.toJSONString(user));
}
}
上一篇: C/C++笔试题目(深圳某公司)
下一篇: 小米公司笔试题