簡體   English   中英

如何實際看到 protobuf 在網絡或文件中傳遞一些字節?

[英]How to actually see protobuf passing some bytes across, either on the network or in a file?

我是 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++ 版本工作,因為它涉及 Python2 與 Python3,以及pip和缺少的庫。

所以只需關注protobuf.js 網站,我已經包含了使其工作的基本最低要求。

我們可以從 Google 的網站上閱讀有關如何制作.proto文件的所有信息。

腳步:

創建一個空文件夾並安裝 protobufjs。 如果需要,請使用 Google for nvm (可選)和 Node.js 和 npm。

mkdir TryProtobufJS
cd TryProtobufJS
npm i protobufjs

現在,創建這 3 個文件:

// 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

# screen output

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

在 Mac 上,您可以使用十六進制轉儲它來查看它:

hexdump -C awesome.dat

現在,要“取回數據”,請使用

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 工作。

另外需要注意的是,我們在Node.js中寫入文件和讀取文件時,由於以下原因我們沒有指定編碼:

  1. writeFileSync :如果數據是緩沖區,則忽略編碼選項。
  2. readFileSync :如果指定了編碼選項,則此 function 返回一個字符串。 否則它返回一個緩沖區。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM