当前位置:网站首页>Solutions using protobuf in TS projects
Solutions using protobuf in TS projects
2022-06-25 10:09:00 【Southern kingdom_ Love】
H5 because Adobe Give up right Flash Player The maintenance of the is hot again .
There are laya and egret Two H5 The game engine , Support AS3、TS、JS Three languages development .
use H5 Game development , Cannot bypass the transmission with the server . The popular solution is to use protobuf. and JS Also have protobuf. My project uses Laya Engine TS edition , Unfortunately, the search for the entire network has not found TS Version of protobuf, I have to use JS Version of .
protobufjs Yes 3 Use in , Real time parsing 、json Analytical way 、 Static code mode ( Pre export proto Of js file ).
So three ways , As one can imagine , Static code is the fastest to execute , And you can directly access proto Fields defined in , There is no need to pass the field name in a string . This will also ensure that proto There will be no spelling errors when assigning values , You don't need to remember the name of the field .
There is a special tool link You can put .proto File export .js And the corresponding files .d.ts file .
First installation Node.js
then , install protobufjs:
npm install protobufjs
After loading , You can export with the command .js Document and .d.ts file
pbjs -t static-module -w commonjs -o joengProto.js joengProto.proto pbts -o joengProto.d.ts joengProto.js
The first line exports js file , The second line exports js Generate corresponding .d.ts file , So you can do it TS The code is called. .
-t -static-module Parameters , Indicates that the static code mode is selected .
-w commonjs Indicates what standard is used to output js Code , There are various, such as es2015、es5、umd、amd What? , But I'm right js Not very familiar with , In short, there is only commonjs Can be executed successfully , Others will report errors .
Okay , Export succeeded . But the code doesn't execute , Or nothing happens , Or report some strange mistakes :
unexpected token import
What's the problem ? I found a lot of information , I don't know what the problem is , But after reading a lot of articles without a clue , I almost thought about what the problem was .
because TS/JS Various third parties will be used in the project JS Class library , The standards used by these libraries are different , There are plenty of them ES5 Yes, there are ES6 Of 、commonJSd Of , Your own project may be ES5 It may also be ES6 Of, etc. . and ES5 It is not supported import and export Of , and es6 Well , Many browsers do not support this standard yet .
So here comes the question ,js The most troublesome thing about the three-party libraries is that the standards of various libraries are inconsistent , Want to coexist in one project , What shall I do? ? Follow this line of thinking , It should be generated js The code is converted to be compatible with various standards .
There are already many solutions . An article by Ruan Yifeng, the great God, introduced how to use it in detail babel, So I chose babel.Babel Introductory tutorial
Follow the link above , It will soon be possible to proto Exported code conversion standard . But still not , because babel No automatic conversion export and import, The main reason why my code can't run is the two keywords . It's useless to go around ...
Fortunately, you only need to install a plug-in to transfer export and import 了 .
install babel
npm install babel
Installing a plug-in
npm install --save-dev babel-plugin-add-module-exports
IDE in , Create a file in the project root directory .babelrc
Note the root directory of your project , No babel It's not node Of .
The contents are as follows :
{
"presets": [
"es2015"
],
"plugins": [
"add-module-exports"
]
}Execute command again
babel proto/joengProto.js -o proto/joengProto.js
View the generated joengProto.js file , No more import and export Keyword. .
Okay , Now it can be in TS Project use proto Of JS Code.
import awesome = require("../proto/joengProto");
class MyTest
{
constructor()
{
console.log("---start---");
var cls = awesome.awesomepackage.AwesomeMessage;
let msg:awesome.awesomepackage.AwesomeMessage = cls.create();
msg.awesomeField = "12345";
msg.num = 20;
console.log(msg.num);
}
}
new MyTest();Output :
---start--- 20
边栏推荐
- Flutter replaces the default icon of Gaud positioning
- Wallys/MULTI-FUNCTION IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL
- How to do the wechat selling applet? How to apply for applets
- x86电脑上下载debian的arm64的包
- Opencv中的GrabCut图像分割
- 2台三菱PLC走BCNetTCP协议,能否实现网口无线通讯?
- Processing picture class library
- 从海量手机号中匹配某一个手机号
- NetCore性能排查
- Why should the terminal retail industry choose the member management system
猜你喜欢

独步武林,架构选型手册(包含 PDF)

Learning notes of rxjs takeuntil operator

Jetpack compose layout (I) - basic knowledge of layout

puzzle(019.2)六边锁

MongoDB的原理、基本使用、集群和分片集群

Can two Mitsubishi PLC adopt bcnettcp protocol to realize wireless communication of network interface?

Basic use and principle of Minio

Kotlin advanced generic

Linked list delete nodes in the linked list

Mengyou Technology: tiktok live broadcast with goods elements hot topics retention skills shaping image highlight selling points
随机推荐
Pytorch_ Geometric (pyg) uses dataloader to report an error runtimeerror: sizes of tenants must match except in dimension 0
STM32 receives data by using idle interrupt of serial port
Shuttle JSON, list, map inter transfer
Yolov5更换上采样方式
Flask博客实战 - 实现侧边栏最新文章及搜索
Wallys/MULTI-FUNCTION IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL
oracle 函数 触发器
What should be paid attention to in PMP examination?
CyCa children's physical etiquette Yueqing City training results assessment successfully concluded
Linked list delete nodes in the linked list
Reasons for Meiye to choose membership system
‘Flutter/Flutter. h‘ file not found
manhattan_ Slam environment configuration
Exception: gradle task assemblydebug failed with exit code 1
Kotlin advanced generic
The way that flutter makes the keyboard disappear (forwarding from the dependent window)
如何在Microsoft Exchange 2010中安装SSL证书
Mysql 源码阅读(二)登录连接调试
Oracle function trigger
WebApi性能优化