使用协议缓冲区和 Netty 4.1.6
Using Protocol Buffers and Netty 4.1.6
我有以下服务器和客户端初始化程序(两者都有极其相似的代码,其中只有 sch
更改为客户端的 cch
,都代表各自的处理程序)。
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast("handler", sch);
ch.pipeline().addLast(new CommonClassHandler());
ch.pipeline().addLast("frameDecoder",
new ProtobufVarint32FrameDecoder());
ch.pipeline().addLast("protobufDecoder",
new ProtobufDecoder(Server.MyMessage.getDefaultInstance()));
ch.pipeline().addLast("frameEncoder", new ProtobufVarint32LengthFieldPrepender());
ch.pipeline().addLast("protobufEncoder", new ProtobufEncoder());
}
我希望在向客户端或服务器发送 commands/actions 时使用二进制格式,因此,我使用 Google 的 Protocol Buffers。
这里是我在处理客户端输入时创建builder
的地方:
while (channel.isOpen()) {
Client.MyMessage.Builder builder = Client.MyMessage.newBuilder();
String input = in.readLine(); // Save console input
builder.setKeyword(input); // Set the value of keyword to said input
channel.writeAndFlush(builder.build()); // Send the build to the server
}
最后是服务器/客户端收到消息时自动调用的方法:
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
ByteBuf bb = (ByteBuf)msg;
String order = bb.toString(Charset.defaultCharset());
System.out.println(order); // Displays received data
Server.MyMessage.Builder builder = Server.MyMessage.newBuilder();
builder.setKeyword("301");
ctx.writeAndFlush(builder.build());
}
1) 当显示我的 ByteBuf
的内容时,它在我的消息之前显示两个未知字符和一个“\n”;也许我应该以另一种方式处理接收到的数据,以便正常显示?
2) 显示接收到的数据后,我的服务器 应该 向我的客户端发送答案“301”,但没有用,因为我的客户端不显示任何内容(方法甚至没有在客户端处理程序中调用),有明显的原因吗?
请原谅我的问题,但是关于在 Netty 4.1.6 中使用 Protocol Buffers 的文档很少。
您正在管道的开头添加处理程序 ch.pipeline().addLast("handler", sch);
,但您应该将其放在管道的末尾,在 protobufDecoder 之后。
完成更改后,您应该开始接收 MyMessage
作为您的 msg
而不是 ByteBuf
。我猜你现在看到的未知字符是你拥有的帧解码器去除的帧长度,但它不会 运行 直到你的处理程序按照你现在的方式设置。
我有以下服务器和客户端初始化程序(两者都有极其相似的代码,其中只有 sch
更改为客户端的 cch
,都代表各自的处理程序)。
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast("handler", sch);
ch.pipeline().addLast(new CommonClassHandler());
ch.pipeline().addLast("frameDecoder",
new ProtobufVarint32FrameDecoder());
ch.pipeline().addLast("protobufDecoder",
new ProtobufDecoder(Server.MyMessage.getDefaultInstance()));
ch.pipeline().addLast("frameEncoder", new ProtobufVarint32LengthFieldPrepender());
ch.pipeline().addLast("protobufEncoder", new ProtobufEncoder());
}
我希望在向客户端或服务器发送 commands/actions 时使用二进制格式,因此,我使用 Google 的 Protocol Buffers。
这里是我在处理客户端输入时创建builder
的地方:
while (channel.isOpen()) {
Client.MyMessage.Builder builder = Client.MyMessage.newBuilder();
String input = in.readLine(); // Save console input
builder.setKeyword(input); // Set the value of keyword to said input
channel.writeAndFlush(builder.build()); // Send the build to the server
}
最后是服务器/客户端收到消息时自动调用的方法:
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
ByteBuf bb = (ByteBuf)msg;
String order = bb.toString(Charset.defaultCharset());
System.out.println(order); // Displays received data
Server.MyMessage.Builder builder = Server.MyMessage.newBuilder();
builder.setKeyword("301");
ctx.writeAndFlush(builder.build());
}
1) 当显示我的 ByteBuf
的内容时,它在我的消息之前显示两个未知字符和一个“\n”;也许我应该以另一种方式处理接收到的数据,以便正常显示?
2) 显示接收到的数据后,我的服务器 应该 向我的客户端发送答案“301”,但没有用,因为我的客户端不显示任何内容(方法甚至没有在客户端处理程序中调用),有明显的原因吗?
请原谅我的问题,但是关于在 Netty 4.1.6 中使用 Protocol Buffers 的文档很少。
您正在管道的开头添加处理程序 ch.pipeline().addLast("handler", sch);
,但您应该将其放在管道的末尾,在 protobufDecoder 之后。
完成更改后,您应该开始接收 MyMessage
作为您的 msg
而不是 ByteBuf
。我猜你现在看到的未知字符是你拥有的帧解码器去除的帧长度,但它不会 运行 直到你的处理程序按照你现在的方式设置。