protobuf: init

Signed-off-by: Jianhui Zhao <jianhuizhao329@gmail.com>
This commit is contained in:
Jianhui Zhao
2018-06-07 00:05:13 +08:00
parent ed6c155eca
commit 40326d830f
3 changed files with 85 additions and 0 deletions
+1
View File
@@ -12,6 +12,7 @@
"dependencies": {
"axios": "^0.18.0",
"iview": "^2.14.1",
"protobufjs": "^6.8.6",
"simple-websocket": "^7.0.2",
"string-format-easy": "^1.0.1",
"vue": "^2.5.13",
+26
View File
@@ -36,6 +36,7 @@ import 'xterm/lib/xterm.css'
import * as fit from 'xterm/lib/addons/fit/fit';
import axios from 'axios'
import * as rtty from './rtty'
import * as protobuf from 'protobufjs/light'
Terminal.applyAddon(fit);
@@ -401,6 +402,31 @@ export default {
this.terminal.term.fit();
}
});
let root = protobuf.Root.fromJSON(require("./rtty.pb.json"));
let rttyMessage = root.lookupType("rtty.rtty_message");
// Exemplary payload
let payload = { version: 1, type: 12, sid: 'rwetr5345464'};
// Create a new message
let message = rttyMessage.create(payload); // or use .fromObject if conversion is necessary
// Encode a message to an Uint8Array (browser) or Buffer (node)
let buffer = rttyMessage.encode(message).finish();
// Decode an Uint8Array (browser) or Buffer (node) to a message
message = rttyMessage.decode(buffer);
// Maybe convert the message back to a plain object
var object = rttyMessage.toObject(message, {
longs: String,
enums: String,
bytes: String,
// see ConversionOptions
});
console.log(object)
}
}
</script>
+58
View File
@@ -0,0 +1,58 @@
{
"nested": {
"rtty": {
"nested": {
"rtty_message": {
"fields": {
"version": {
"type": "uint32",
"id": 1
},
"type": {
"type": "uint32",
"id": 2
},
"sid": {
"type": "string",
"id": 3
}
},
"nested": {
"Type": {
"values": {
"UNKNOWN": 0,
"LOGIN": 1,
"LOGINACK": 2,
"LOGOUT": 3,
"TTY": 4,
"ANNOUNCE": 5,
"UPFILE": 6,
"DOWNFILE": 7
}
},
"data": {
"fields": {
"code": {
"type": "int32",
"id": 1
},
"data": {
"type": "bytes",
"id": 2
},
"name": {
"type": "string",
"id": 3
},
"size": {
"type": "uint32",
"id": 4
}
}
}
}
}
}
}
}
}