gpt4 book ai didi

javascript - 错误 : Illegal wire type for field Message. 字段 .protobuf.MessageTypeAck.sourceModuleID:1(预期为 0)

转载 作者:搜寻专家 更新时间:2023-11-01 00:00:10 26 4
gpt4 key购买 nike

我的应用程序可以使用 kafka 和 Protocol Buffer 生成和使用消息,并且一切正常。我正在使用 SerializeAsString() 序列化 Protocol Buffer (此应用程序是用 C++ 编写的)。

现在,我添加了新的 node.js 网站,该网站也使用消息并尝试对其进行解码。

我的 js 代码(使用很棒的 ProtoBuf.js 模块):

var builder = ProtoBuf.loadProtoFile("/home/aii/general/proto/All.proto"),
protobuf = builder.build("protobuf"),
Trace = protobuf.Trace,
MessageType = protobuf.MessageType,
MessageTypeAck = protobuf.MessageTypeAck,
MessageTypeKeepAlive = protobuf.MessageTypeKeepAlive;

function getMessageType(val) {
return Object.keys(MessageType).filter(function(key) {return MessageType[key] === val})[0]
}

consumer.on('message', function (message) {
try{
switch(getMessageType(message.key[0])) {
case 'MESSAGE_TYPE_ACK':
console.log(MessageTypeAck.decode(message.value));
break;
case 'MESSAGE_TYPE_KEEP_ALIVE':
console.log(MessageTypeKeepAlive.decode(message.value));
break;
default:
console.log("Unknown message type");
}
} catch (e){
if (e.decoded) {
var err = e.decoded;
console.log(err);
}
else {
console.log(e);
}
}
});

结果:

[Error: Illegal wire type for field Message.Field .protobuf.MessageTypeAck.sourceModuleID: 1 (0 expected)]

我的原型(prototype)文件:

Trace.proto:

package protobuf;

message Trace {
optional string topic = 1;
optional int32 partition = 2;
optional int64 offset = 3;
}

MessageType.proto

package protobuf;

enum MessageType {
MESSAGE_TYPE_ACK = 1;
MESSAGE_TYPE_KEEP_ALIVE = 2;
}

Messages.proto:

import "Trace.proto";

package protobuf;

message MessageTypeAck {
repeated Trace trace = 1;

optional string sourceModuleName = 2;
optional int32 sourceModuleID = 3;
}

message MessageTypeKeepAlive {
repeated Trace trace = 1;

optional string sourceModuleName = 2;
optional int32 sourceModuleID = 3;
}

All.proto

import "Trace.proto"
import "MessageType.proto";
import "Messages.proto"

我做错了什么? (解码?)

最佳答案

所以,多亏了这个SO question&answer , 我想到了!问题与我使用缓冲区(通过 kafka)的方式有关 - 作为 utf-8(默认)。它实际上与我没有附加的代码有关:

var kafka = require('kafka-node'),
Consumer = kafka.Consumer,
client = new kafka.Client('localhost:2181'),
consumer = new Consumer(
client,
[
{ topic: 'Genesis', partition: 0 }
],
{
autoCommit: false,
encoding: 'buffer'
}
);

解决方案是添加 encoding: 'buffer' 行(默认为 'utf-8',如 here 所述)。

关于javascript - 错误 : Illegal wire type for field Message. 字段 .protobuf.MessageTypeAck.sourceModuleID:1(预期为 0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36969295/

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