gpt4 book ai didi

java - 如何解析Netty(java)中的各种对象?

转载 作者:行者123 更新时间:2023-12-01 09:29:47 24 4
gpt4 key购买 nike

我使用 netty,我想将各种对象从客户端发送到服务器,反之亦然。

我在客户端和服务器上创建了相同的解码器和编码器类;

解码器:

public class UserInfoDecoder extends ByteToMessageDecoder {
@Override
protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
...
list.add(new UserInfo(...))
}
}

编码器:

public class UserInfoEncoder extends MessageToByteEncoder<UserInfo> {
@Override
protected void encode(ChannelHandlerContext ctx, UserInfo msg, ByteBuf out) {
...
out.writeBytes(...);
}

这是我的服务器 initChannel 方法:

public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
if (sslCtx != null) {
p.addLast(sslCtx.newHandler(ch.alloc()));
}
p.addLast(
// new ObjectEncoder(),
// new ObjectDecoder(ClassResolvers.cacheDisabled(null)),

new UserInfoDecoder(),
new UserInfoEncoder(),

new ObjectEchoServerHandler());
}

服务器处理程序类中有一个方法channelRead

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {


...
}

如何区分客户端发送的对象?例如现在我只有“UserInfo”类,我可以将channelRead中的“Object msg”转换为“UserInfo”,但我也想发送“UsersCar”对象,例如,如何按发送的类型来区分对象?

最佳答案

按照当前的实现方式,实现此目的的最简单方法是在通过 channel 发送编码字节之前向其添加“魔术字节”前缀。

public class UserInfoEncoder extends MessageToByteEncoder<UserInfo> {
@Override
protected void encode(ChannelHandlerContext ctx, UserInfo msg, ByteBuf out) {
final int USER_BYTE = 0;
out.writeBytes(USER_BYTE);
out.writeBytes(msg.toBytes());
}

然后在服务器端,当消息被解码时,检查这个魔术字节并根据读取的值分配适当的解码器。例如。如果第一个未解码字节的值为 0,则使用 UserInfo 解码器。如果第一个未解码字节的值为1,则使用UsersCar解码器。

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
final ByteBuf message = (ByteBuf) msg;
final byte magicByte= message.getByte(0);

if(magicByte == 0){
new UserInfoDecoder().decode(message);
}else if {
....
}
}

不是最巧妙的解决方案,而是最简单的。

注意:这假设您使用的处理程序正在扩展 ChannelInboundHandlerAdapter

一个了解 Netty 编码/解码的好例子:http://shengwangi.blogspot.ie/2016/03/netty-tutorial-hello-world-example.html

关于java - 如何解析Netty(java)中的各种对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39531627/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com