Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Protocol buffers 如何实际看到protobuf在网络或文件中传递一些字节?_Protocol Buffers_Protobuf.js - Fatal编程技术网

Protocol buffers 如何实际看到protobuf在网络或文件中传递一些字节?

Protocol buffers 如何实际看到protobuf在网络或文件中传递一些字节?,protocol-buffers,protobuf.js,Protocol Buffers,Protobuf.js,我是protobuf的新手,希望通过网络或文件传递一些数据,例如 2-byte unsigned int: 15 2-byte unsigned int: 15 and -1 4-byte unsigned int: 256, and then a string "Peter" 4-byte unsigned int: 256, and then two strings "Peter", "Mary" 4-byte signed int

我是protobuf的新手,希望通过网络或文件传递一些数据,例如

2-byte unsigned int: 15
2-byte unsigned int: 15 and -1
4-byte unsigned int: 256, and then a string "Peter"
4-byte unsigned int: 256, and then two strings "Peter", "Mary"
4-byte signed int: (3, 4) as a point
4-byte signed int: a point above twice, such as (3, 4) and (10, 11) as a line
4-byte signed int and a string: the line above, and a name for this line
字节是否可以由Python/Ruby写入文件,然后由JavaScript读回?(也可以全部用JavaScript编写)


我认为能够在本地网站上传递可能要复杂一些?如果是这样,将其写入文件并能够读回就可以了。怎么做呢?

我发现使用protobuf.js要简单得多,我们不需要使用编译器命令行工具protoc。我发现很难让Python或C++版本工作,因为它涉及Python 2和Python 3,并且 PIP和缺少库。 因此,请简单地遵循,我已经包括了使其工作的基本最低要求

我们可以阅读所有关于

步骤: 创建一个空文件夹并安装protobufjs。谷歌搜索
nvm
(可选)以及Node.js和npm(如果需要)

mkdir TryProtobufJS
cd锥虫
npm i协议
现在,创建以下3个文件:

//awesome.proto
包装包装;
syntax=“proto3”;
消息令人敬畏的消息{
int32num=1;
字符串awesome\u field=20;//变为awesomeField
}
//write.js
常数fs=要求(“fs”);
const protobuf=require(“protobufjs”);
protobuf.load(“awesome.proto”,函数(err,root){
如果(错误)抛出错误;
常量AwesomeMessage=root.lookupType(“awesomepackage.AwesomeMessage”);
const payload={num:15,awesomeField:“ABC”};
控制台日志(“有效载荷”,有效载荷);
const errMsg=AwesomeMessage.verify(有效负载);
if(errMsg)抛出错误(errMsg);
const message=AwesomeMessage.create(payload);//如果需要转换,则使用.fromObject
//将消息编码到UINT8阵列(浏览器)或缓冲区(节点)
const buffer=AwesomeMessage.encode(message.finish();
日志(缓冲区);
fs.writeFileSync(“awesome.dat”,buffer);
});
//read.js
常数fs=要求(“fs”);
const protobuf=require(“protobufjs”);
protobuf.load(“awesome.proto”,函数(err,root){
如果(错误)抛出错误;
常量AwesomeMessage=root.lookupType(“awesomepackage.AwesomeMessage”);
const buffer=fs.readFileSync(“awesome.dat”);
日志(缓冲区);
//将UINT8阵列(浏览器)或缓冲区(节点)解码为消息
const message=AwesomeMessage.decode(缓冲区);
//将消息转换回普通对象
const object=AwesomeMessage.toObject(消息{
长:字符串,
枚举:字符串,
字节:字符串,
//请参见转换选项
});
console.log(“object”,object);
});
现在运行文件
write.js

node write.js
它将创建一个数据文件:
awesome.dat

# screen output

payload { num: 15, awesomeField: 'ABC' }
<Buffer 08 0f a2 01 03 41 42 43>
现在,要“找回数据”,请使用

node read.js
#屏幕输出
对象{num:15,awesomeField:'ABC'}
如果我使用Node.js v14,出于某种原因,它在MacBook Air M1上不起作用,但Node v15和v16起作用

另外,请注意,在Node.js中写入文件和读取文件时,由于以下原因,我们没有指定编码:

  • :如果数据是缓冲区,则忽略编码选项
  • :如果指定了编码选项,则此函数返回字符串。否则它将返回一个缓冲区
  • # screen output
    
    <Buffer 08 0f a2 01 03 41 42 43>
    object { num: 15, awesomeField: 'ABC' }