当前位置:网站首页>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
边栏推荐
- MongoDB的原理、基本使用、集群和分片集群
- Set the location permission in the shutter to "always allow"
- Redis(一)原理与基本使用
- ShardingSphere-Proxy 4.1 分庫分錶
- Learning notes of rxjs takeuntil operator
- 独步武林,架构选型手册(包含 PDF)
- CYCA 2022少儿形体礼仪初级师资班 深圳总部站圆满结束
- Flutter dialog: cupertinoalertdialog
- CyCa children's physical etiquette Yueqing City training results assessment successfully concluded
- CYCA少儿形体礼仪 乐清市培训成果考核圆满落幕
猜你喜欢

Notes on writing questions in C language -- monkeys eat peaches

8. Intelligent transportation project (1)

Creo makes a mobius belt in the simplest way

Pytorch_Geometric(PyG)使用DataLoader报错RuntimeError: Sizes of tensors must match except in dimension 0.

链表 删除链表中的节点

CyCa children's physical etiquette Yueqing City training results assessment successfully concluded

How to build a wechat applet? How to open an applet?

The gradle configuration supports the upgrade of 64 bit architecture of Xiaomi, oppo, vivo and other app stores

Etcd教程 — 第四章 Etcd集群安全配置

汇付国际为跨境电商赋能:做合规的跨境支付平台!
随机推荐
Processing picture class library
Guiding principle - read source code
Encoding format for x86
Pytorch_ Geometric (pyg) uses dataloader to report an error runtimeerror: sizes of tenants must match except in dimension 0
Flask博客实战 - 实现个人中心及权限管理
Remittance international empowers cross-border e-commerce: to be a compliant cross-border payment platform!
Flask博客实战 - 实现侧边栏最新文章及搜索
Redis (I) principle and basic use
CYCA 2022少儿形体礼仪初级师资班 深圳总部站圆满结束
Use evo
Flutter Gaode map privacy compliance error
puzzle(019.2)六边锁
Arduino bootloader burning summary
tokenizers>=0.11.1,!=0.11.3,<0.13 is required for a normal functioning of this module,
The problem of automatic page refresh after the flyer WebView pops up the soft keyboard
Android database security: after the user exits, the transaction rollback log still stores relevant data information
Download the arm64 package of Debian on X86 computer
Flask博客实战 - 实现侧边栏文章归档及标签
What are the PMP scores?
使用EVO