Use Protobuf in Laya [Salted Fish Tutorial] Use of protobuf in websocket communication

reference:

The Laya-ProtoBuf   generator environment is built automatically by Docker.

Network and format--ProtocolBuffer

[Salted Fish Tutorial] The use of protobuf in websocket communication

 

Codeing source code:

https://gamedaybyday.coding.net/p/LayaProtoBuf

 

There is no convenient way to find in the forum community and official tutorials of Laya. There is a water friend github issued a Docker automatically generated, but not easy to use Egret.

 

One Run npm installation

Under the Laya project folder, right-click and select "Open Powershell window here"

 

 Enter the following command

npm install [email protected] -g
npm install @egret/protobuf -g

 

 

 

Two run pb-egret add

Run pb-egret add to prohibit running

 

 

Run the following command once, select Y

Set-ExecutionPolicy Unrestricted

 

 

Then run pb-egret add

pb-egret add

 

 

 

A protobuf folder is generated under the project directory

 

 

 

Three Generate library files based on .proto files

Write a test. Proto file, placed in the protobuf / protofile folder

 

 

 

 Run pb-egret generate

pb-egret generate

After running, the following files are generated under protobuf / bundles. This is the library file generated by the .proto file, which can be used as a third-party library in Laya.

 

 Modify the pbconfig.json in the protobuf folder of the project as shown below

 

 

 Run pb-egret generate again, the resulting file becomes smaller, and some unused methods are not generated.

 

 

 Four using library files in Laya

Copy protobuf-library.min.js under protobuf / library to the project bin / libs

Copy protobuf-library.d.ts under protobuf / library to the project libs

 

Copy protobuf-bundles.min.js under protobuf / bundles to the project bin / libs

Copy protobuf-bundles.d.ts under protobuf / bundles to the project libs

 

Under Laya's UI editor, F9, check the following

 

The order of the libraries must not be wrong! ! Check in bin / index.js, you need to load protobuf-library.js first, and then load protobuf-bundles.js, otherwise you will get an error, you can not find protobuf.roots initialization object.

 

 

 

Used in the code, make sure that template.login can be used. Socket sending can be debugged by myself. I have no server and have not debugged.

 

 

Five How to update after modifying the .proto file

1. Follow step 4 above to copy the new files from protobuf / bundles to the bin / libs and libs directories

2. Modify protobuf / pbconfig.json to have an outputFile release path

 

Guess you like

Origin www.cnblogs.com/gamedaybyday/p/12716984.html