如何实际看到 protobuf 在网络上或文件中传递一些字节? [英] How to actually see protobuf passing some bytes across, either on the network or in a file?

查看:108
本文介绍了如何实际看到 protobuf 在网络上或文件中传递一些字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 protobuf 的新手,想尝试一下,通过网络或文件传递一些数据,例如

I am new to protobuf and want to experiment with it, by passing some data over the network or in a file, such as

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 编写).

Can the bytes be written by Python / Ruby in a file, and then read back by JavaScript? (or it can be all written in JavaScript).

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

I think being able to pass it on a local website is probably quite a bit more complicated? If so, writing it to a file and be able to read it back would be fine. How could it be done?

推荐答案

我发现使用 protobuf.js 简单很多,而且我们不需要使用 protoc 这是一个编译器命令行工具.我发现很难让 Python 或 C++ 版本工作,因为它涉及 Python2 与 Python3,以及 pip 和缺少的库.

I found that it is a lot simpler to use protobuf.js, and we don't need to use protoc which is a compiler command line tool. I found it difficult to make the Python or the C++ version to work, as it involves Python2 vs Python3, and pip and missing libraries.

所以只需关注 protobuf.js 网站,我已经包括了基本的最低要求让它工作.

So simply follow the protobuf.js website, and I have included the basic minimum to get it to work.

我们可以阅读关于如何制作.proto 来自 Google 网站的文件.

We can read all about how to make a .proto file from Google's website.

创建一个空文件夹并安装protobufjs.如果需要,请在 Google 上搜索 nvm(可选)以及 Node.js 和 npm.

Create an empty folder and install protobufjs. Google for nvm (optional) and Node.js and npm if you need to.

mkdir TryProtobufJS
cd TryProtobufJS
npm i protobufjs

现在,创建这 3 个文件:

Now, create these 3 files:

// awesome.proto
package awesomepackage;
syntax = "proto3";

message AwesomeMessage {
    int32 num = 1;
    string awesome_field = 20; // becomes awesomeField
}

// write.js

const fs = require("fs");
const protobuf = require("protobufjs");

protobuf.load("awesome.proto", function (err, root) {
  if (err) throw err;

  const AwesomeMessage = root.lookupType("awesomepackage.AwesomeMessage");

  const payload = { num: 15, awesomeField: "ABC" };
  console.log("payload", payload);

  const errMsg = AwesomeMessage.verify(payload);
  if (errMsg) throw Error(errMsg);

  const message = AwesomeMessage.create(payload); // or use .fromObject if conversion is necessary

  // Encode a message to an Uint8Array (browser) or Buffer (node)
  const buffer = AwesomeMessage.encode(message).finish();

  console.log(buffer);
  fs.writeFileSync("awesome.dat", buffer);
});

// read.js

const fs = require("fs");
const protobuf = require("protobufjs");

protobuf.load("awesome.proto", function (err, root) {
  if (err) throw err;

  const AwesomeMessage = root.lookupType("awesomepackage.AwesomeMessage");

  const buffer = fs.readFileSync("awesome.dat");
  console.log(buffer);

  // Decode an Uint8Array (browser) or Buffer (node) to a message
  const message = AwesomeMessage.decode(buffer);

  // Convert the message back to a plain object
  const object = AwesomeMessage.toObject(message, {
    longs: String,
    enums: String,
    bytes: String,
    // see ConversionOptions
  });
  console.log("object", object);
});

现在运行文件 write.js

node write.js

它会创建一个数据文件:awesome.dat.

and it will create a data file: awesome.dat.

# screen output

payload { num: 15, awesomeField: 'ABC' }
<Buffer 08 0f a2 01 03 41 42 43>

在 Mac 上,你可以 hex dump 来查看它:

On the Mac, you can hex dump it to look at it:

hexdump -C awesome.dat

现在,要取回数据",请使用

and now, to "get the data back", use

node read.js

# screen output

<Buffer 08 0f a2 01 03 41 42 43>
object { num: 15, awesomeField: 'ABC' }

如果我使用 Node.js v14,出于某种原因,它在 MacBook Air M1 上对我不起作用,但 Node v15 和 v16 起作用了.

If I use Node.js v14, it didn't work for me on a MacBook Air M1 for some reason, but Node v15 and v16 worked.

另外,注意我们在Node.js中写文件和读文件的时候,没有指定编码,原因如下:

Also, note that when we write the file and read the file in Node.js, we don't specify the encoding due to the following reasons:

  1. writeFileSync:如果数据是缓冲区,则忽略编码选项.立>
  2. readFileSync:如果指定了编码选项,则此函数返回一个字符串.否则它返回一个缓冲区.
  1. writeFileSync: The encoding option is ignored if data is a buffer.
  2. readFileSync: If the encoding option is specified then this function returns a string. Otherwise it returns a buffer.

这篇关于如何实际看到 protobuf 在网络上或文件中传递一些字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆