Netty,ProtoBuf,WebSocket;如何将BinaryWebSocketFr。
。。
温馨提⽰:将⿏标放在语句上可以显⽰对应的英⽂。  或者
我有⼀台使⽤protobuf的服务器。 处理程序定义为 -
public class ServerHandler extends SimpleChannelInboundHandler<MyProtoBufType> {
管道如下 -
pipeline.addLast(new ProtobufVarint32FrameDecoder());
pipeline.addLast(new DefaultInstance()));
pipeline.addLast(new ProtobufVarint32LengthFieldPrepender());
pipeline.addLast(new ProtobufEncoder());
pipeline.addLast("ServerHandler", new ServerHandler());
有了它和Java客户端⼀切正常。
现在,为了能够使⽤Javascript websocket进⾏连接,我修改了服务器管道,如下所⽰ -
pipeline.addLast(new HttpServerCodec());
pipeline.addLast(new HttpObjectAggregator(65536));
pipeline.addLast(new WebSocketServerCompressionHandler());
pipeline.addLast(new WebSocketServerProtocolHandler(WEBSOCKET_PATH, null, true));
pipeline.addLast(new MessageToMessageDecoder<WebSocketFrame>() {
@Override
protected void decode(ChannelHandlerContext ctx, WebSocketFrame frame, List<Object> objs) throws Exception {
ByteBuf buf = ((BinaryWebSocketFrame) frame).content();
objs.add(buf);
}
});
pipeline.addLast(new ProtobufVarint32FrameDecoder());
pipeline.addLast(new DefaultInstance()));
pipeline.addLast(new ProtobufVarint32LengthFieldPrepender());
pipeline.addLast(new ProtobufEncoder());
pipeline.addLast("ServerHandler", new ServerHandler());
以下是基于库的Javascript客户端代码 -
var ProtoBuf = dcodeIO.ProtoBuf,
builder = ProtoBuf.loadProtoFile("./example.proto"),
pkg = builder.build("ample");
socket = new WebSocket("ws://localhost:8484");
socket.binaryType = "arraybuffer";
var login = new pkg.Login({"userName": "hello", "password": "pass"});
var wrapper = new pkg.MyProtoBufType();
wrapper.set("login", login);
socket.ArrayBuffer());
};
运⾏上述代码时,服务器在protobuf解码器上失败。 以下是堆栈跟踪 -
WARN [nioEventLoopGroup-3-6] (DefaultChannelPipeline.java:1144) - An exceptionCaught() event was fired, and it reached at the tail of the pipeline. It us
dec.DecoderException: le.protobuf.InvalidProtocolBufferException: While parsing a protocol message, the input ended unexpectedly    at dec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:98)
at ioty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:349)
at ioty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:335)
at ioty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:327)
at dec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:288)
at dec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:275)
at dec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:391)
at dec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:243)
at ioty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:349)
at ioty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:335)
at ioty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:327)
at dec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:102)
at ioty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:349)
at ioty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:335)
at ioty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:327)
at dec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:102)
at ioty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:349)
at ioty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:335)
at ioty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:327)
at ioty.channel.ChannelInboundHandlerAdapter.channelRead(ChannelInboundHandlerAdapter.java:86)
at dec.http.websocketx.Utf8FrameValidator.channelRead(Utf8FrameValidator.java:77)
at ioty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:349)
at ioty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:335)
at ioty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:327)
at dec.http.websocketx.WebSocketServerProtocolHandler$1.channelRead(WebSocketServerProtocolHandler.java:159)
at ioty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:349)
at ioty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:335)
at ioty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:327)
at dec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:102)
at ioty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:349)
at ioty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:335)
at ioty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:327)
at dec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:288)
at dec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:262)
at ioty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:349)
at ioty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:335)
at ioty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:327)
at ioty.handler.logging.LoggingHandler.channelRead(LoggingHandler.java:233)
at ioty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:349)
at ioty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:335)
at ioty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:327)
at ioty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1336)
at ioty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:349)
at ioty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:335)
at ioty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:911)
at ioty.channel.nio.ad(AbstractNioByteChannel.java:123)
at ioty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:544)
at ioty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:485)
at ioty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:399)
at ioty.channel.nio.NioEventLoop.run(NioEventLoop.java:371)
at urrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:742)
at urrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:145)
at java.lang.Thread.run(Unknown Source)
Caused by: le.protobuf.InvalidProtocolBufferException: While parsing a protocol message, the input ended unexpectedly in the middle of a field.  This co    le.uncatedMessage(InvalidProtocolBufferException.java:70)
le.adRawBytesSlowPath(CodedInputStream.java:1187)
le.adBytes(CodedInputStream.java:517)
le.protobuf.FieldFrom(UnknownFieldSet.java:501)
le.protobuf.From(UnknownFieldSet.java:478)
le.protobuf.From(UnknownFieldSet.java:596)
le.protobuf.From(UnknownFieldSet.java:281)
le.protobuf.From(UnknownFieldSet.java:281)
le.adGroup(CodedInputStream.java:424)
le.protobuf.FieldFrom(UnknownFieldSet.java:505)
le.protobuf.GeneratedMessage.parseUnknownField(GeneratedMessage.java:205)
aple.MyProtoBufType.<init>(CallBreakProtocol.java:15290)
aple.MyProtoBufType.<init>(CallBreakProtocol.java:15273)websocket和socket
aple.MyProtoBufType$1.parsePartialFrom(CallBreakProtocol.java:15638)
aple.MyProtoBufType$1.parsePartialFrom(CallBreakProtocol.java:1)
le.protobuf.AbstractParser.parsePartialFrom(AbstractParser.java:137)
le.protobuf.AbstractParser.parseFrom(AbstractParser.java:168)
le.protobuf.AbstractParser.parseFrom(AbstractParser.java:174)
le.protobuf.AbstractParser.parseFrom(AbstractParser.java:49)
at dec.protobuf.ProtobufDecoder.decode(ProtobufDecoder.java:121)
at dec.protobuf.ProtobufDecoder.decode(ProtobufDecoder.java:64)
at dec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:88)    ... 52 more
有没有⼈⼀起使⽤Netty,ProtoBuf和WebSocket? 怎么能实现这⼀⽬标?
1 个回复
===============>>#1 票数:0 已采纳
我只需删除以下内容 -
pipeline.addLast(new ProtobufVarint32FrameDecoder());
现在⼯作正常。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。