The use of "salted fish Tutorial" protobuf in WebSocket communication

Source: Internet
Author: User

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 = newegret.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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.