mac 下 使用 protobuf 生成 java 及 js 及配合socket.io 使用

protubuf 地址 https://github.com/google/protobuf/releases

定义一个ProtoBuf源文件,gps_data.proto

syntax = "proto3";
message gps_data {
    int64 id = 1;
    string terminalId = 2;
    string dataTime = 3;
    double lon = 4;
    double lat = 5;
    float speed = 6;
    int32 altitude = 7;
    int32 locType = 8;
    int32 gpsStatus = 9;
    float direction = 10;
    int32 satellite = 11;
}

进入当前目录 生成java: protoc ./gps_data.proto --java_out=./
生成js : protoc ./gps_data.proto --js_out=./

基于socket.io protobuf 的使用

前台:
引入 protobuf.js cdn 节点

<script src="https://cdn.rawgit.com/dcodeIO/protobuf.js/6.8.6/dist/protobuf.js"></script>

protobuf.load("/static/protobuf/gps_data.proto", function (err, root) {
                    if (err) throw err;

                    gps_data = root.lookupType("gps_data");

                    let message = gps_data.create({dataTime: "2018-07-03",terminalId:"我们都有一个家名字叫中国"});
                    console.log(`message = ${JSON.stringify(message)}`);

                    let buffer = gps_data.encode(message).finish();
                    console.log(`buffer = ${Array.prototype.toString.call(buffer)}`);
                    console.log(buffer)

                    //参考文章: https://www.cnblogs.com/gradolabs/p/4762134.html
                    var bufArr = new ArrayBuffer(buffer.length);
                    var bufView = new Uint8Array(bufArr);
                    bufView.set(buffer)

                    console.log(bufArr)

                    socket.emit("protobufTest", bufArr, function (data) {
                        console.log("后台 传回来的 byte 数据 :==============》")

                        var d = new Uint8Array(data.byteLength);
                        var dataView = new DataView(data);
                        for (var i = 0; i < data.byteLength; i++) {
                            d[i] = dataView.getInt8(i);
                        }
                        console.log(d)
                        let decoded = gps_data.decode(d);
                        console.log(`decoded = ${JSON.stringify(decoded)}`);
                    })

                    // let decoded = AwesomeMessage.decode(buffer);
                    // console.log(`decoded = ${JSON.stringify(decoded)}`);
                });

后端:

maven 引入 :
 <dependency>
            <groupId>com.google.protobuf</groupId>
            <artifactId>protobuf-java</artifactId>
            <version>3.6.0</version>
       </dependency>
//      server.addEventListener("protobufTest", byte[].class, (client, data, ackRequest) -> {
//        GpsData.gps_data gps_data = GpsData.gps_data.parseFrom(data);
//        System.out.println("after :" + gps_data.toString());
//    });
//使用protobuf 测试传输的数据 使用了springboot后台框架 
    @OnEvent(value = "protobufTest")
    public void onProtobufTest(SocketIOClient client, AckRequest ackRequest, byte[] data) throws InvalidProtocolBufferException {
        if (ackRequest.isAckRequested()) {
            ackRequest.sendAckData(data);
        }
        GpsData.gps_data gps_data = GpsData.gps_data.parseFrom(data);
        System.out.println("after :" + gps_data.toString());
    }