How to serialize Go structures using protocol buffers and use them in Dart over Ajax

huangapple go评论102阅读模式
英文:

How to serialize Go structures using protocol buffers and use them in Dart over Ajax

问题

如果您在我的服务器上有大量的键入数据存储在SQL数据库中,您想要如何使用Protocol Buffers将这些数据发送到Dart客户端?

英文:

If you have a large amount of typed data in an sql database on my server, how do you send this data to a dart client using protocol buffers?

答案1

得分: 13

首先,在计算机上安装 protoc,使用以下命令:

sudo apt-get install protobuf-compiler

然后从 https://code.google.com/p/goprotobuf/ 安装 Go Protocol Buffer 库。Dart 版本可以在这里找到:https://github.com/dart-lang/dart-protoc-plugin

下一步是编写一个包含要发送的消息定义的 .proto 文件。可以在这里找到示例:https://developers.google.com/protocol-buffers/docs/proto

例如:

message Car {
    required string make = 1;
    required int32 numdoors = 2;
}

然后使用 protoc 工具为该 proto 文件编译一个 Go 文件和一个 Dart 文件。

在 Go 中创建一个 Car 对象时,记得使用提供的类型:

c := new(Car)
c.Make = proto.String("Citroën")
c.Numdoors = proto.Int32(4)

然后,你可以将该对象通过 http.ResponseWriter 发送如下:

binaryData, err := proto.Marshal(c)
if err != nil {
  // 处理错误
}
w.Write(binaryData)

在 Dart 代码中,你可以按如下方式获取信息:

void getProtoBuffer() {
    HttpRequest.request("http://my.url.com", responseType: "arraybuffer").then( (request) {
        Uint8List buffer = new Uint8List.view(request.response, 0, (request.response as ByteBuffer).lengthInBytes); // 这是对 dart2js 的一个修复,因为存在一个 bug
        Car c = new Car.fromBuffer(buffer);
        print(c);
    });
}

如果一切正常,你现在应该在 Dart 应用程序中拥有一个 Car 对象 How to serialize Go structures using protocol buffers and use them in Dart over Ajax

英文:

First install protoc on your computer using

sudo apt-get install protobuf-compiler

Then install the go protocol buffer library from <https://code.google.com/p/goprotobuf/>. The dartlang version can be found here: <https://github.com/dart-lang/dart-protoc-plugin>.

The next step is to write a .proto file containing a definition of the message to be sent. examples can be found here: <https://developers.google.com/protocol-buffers/docs/proto>.

For example:

message Car {
    required string make = 1;
    required int32 numdoors = 2;
}

Then use the protoc tool to compile a go file and a dart file for this proto file.

To create a Car object in go, remember to use the types provided:

c := new(Car)
c.Make = proto.String(&quot;Citro&#235;n&quot;)
c.Numdoors = proto.Int32(4)

Then you can send the object over an http.ResponseWriter, w as follows:

binaryData, err := proto.Marshal(c)
if err != nil {
  // do something with error
}
w.Write(binaryData)

In the Dart code, you can fetch the information as follows:

void getProtoBuffer() {
    HttpRequest.request(&quot;http://my.url.com&quot;, responseType: &quot;arraybuffer&quot;).then( (request) {
        Uint8List buffer = new Uint8List.view(request.response, 0, (request.response as ByteBuffer).lengthInBytes); // this is a hack for dart2js because of a bug
        Car c = new Car.fromBuffer(buffer);
        print(c);
    });
}

If everything worked, you should now have a Car object in your Dart application How to serialize Go structures using protocol buffers and use them in Dart over Ajax

huangapple
  • 本文由 发表于 2013年9月27日 02:45:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/19036213.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定