Tutorials Directory
A brief introduction to Protobuf
Two use Protobuf
Three demo download
a brief introduction to Protobuf
Baidu Encyclopedia: Protocolbuffer (hereinafter referred to as PB) is Google's Data Interchange format, which is independent of the language, independent of the platform. Google offers implementations in multiple languages: Java, C #, C + +, go and Python, each of which contains compilers and library files for the appropriate language. Because it is a binary format, it is much faster than using XML for data exchange. It can be used in data communication between distributed applications or in heterogeneous environments. As a good efficiency and compatibility of the binary data transmission format, can be used for such as network transport, configuration files, data storage and many other areas.
Reference: Protocol buffer_ Baidu Encyclopediamedium and heavy game development framework: EGER Pro Development Tutorials
GOOGLE_PROTOBUF Data types
two use protobuf
1 Importing third-party libraries
I directly put the protobuf third-party library in the project.
Modify the Egretproperties.json file to add the Protobuf library. After the addition is complete, the engine needs to be compiled.
2 Writing Protobuf files
Create a new file Template.proto
I'm writing a test data here, User_login
3 Loading Protobuf files
Note Type if text
4 using Protobuf
Read Template_proto file
[C + +]Plain Text view copy code ?
1 |
var message = dcodeIO.ProtoBuf.loadProto(RES.getRes( "template_proto" )); |
Create a new User_login class
[AppleScript]Plain Text view copy code ?
1234 |
var user_login_class
= message
.build
(
"user_login"
)
;
var user_login
= new user_login_class
(
)
;
user_login.userId
= 123
;
user_login.userName
= "peter"
;
|
Turn User_login into bytes
[C #]Plain Text view copy code ?
1 |
var bytes = user_login.toArrayBuffer(); |
Socket send User_login
[C #]Plain Text view copy code ?
123 |
var socket:egret.WebSocket = new egret.WebSocket(); socket.writeBytes(bytes); socket.flush(); |
The processing of receiving data is roughly the same, see demo in detail.
Three demo download
The use of "salted fish Tutorial" protobuf in WebSocket communication