当前位置:网站首页>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's principle, basic use, clustering and partitioned clustering
- 广发证券靠谱吗?是否合法?开股票账户安全吗?
- [buuctf.reverse] 121-125
- Pytorch_Geometric(PyG)使用DataLoader报错RuntimeError: Sizes of tensors must match except in dimension 0.
- Force buckle -104 Maximum depth of binary tree
- 依赖属性、依赖附加属性以及类型转换
- 纳米数据世界杯数据接口,中超数据,体育数据比分,世界杯赛程api,足球比赛实时数据接口
- Basic use and principle of Minio
- Simple waterfall effect
- How do wechat sell small commodity programs do? How to open wechat apps to sell things?
猜你喜欢

Etcd tutorial - Chapter 4 etcd cluster security configuration

Methodchannel of flutter

Flask博客实战 - 实现侧边栏最新文章及搜索

Redis(一)原理与基本使用

Wallys/MULTI-FUNCTION IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL

纳米数据世界杯数据接口,中超数据,体育数据比分,世界杯赛程api,足球比赛实时数据接口

Flutter dialog: cupertinoalertdialog

Puzzle (019.2) hexagonal lock

Vscode attempted to write the procedure to a pipeline that does not exist
![[MySQL learning notes 21] storage engine](/img/3a/a3cd573281efc689cafdb7d7562ce0.png)
[MySQL learning notes 21] storage engine
随机推荐
Can two Mitsubishi PLC adopt bcnettcp protocol to realize wireless communication of network interface?
How do wechat sell small commodity programs do? How to open wechat apps to sell things?
Flutter replaces the default icon of Gaud positioning
tokenizers>=0.11.1,!=0.11.3,<0.13 is required for a normal functioning of this module,
MySQL source code reading (II) login connection debugging
Linked list delete nodes in the linked list
Kotlin advanced set
Jetpack compose layout (IV) - constraintlayout
Kotlin common standard functions
Remittance international empowers cross-border e-commerce: to be a compliant cross-border payment platform!
PHP obtains the IP address, and the apache2 server runs without error
Nano data World Cup data interface, CSL data, sports data score, world cup schedule API, real-time data interface of football match
瑞吉外卖项目(二)
[smart agriculture program] smart agriculture small program project is currently popular.
测试开发工程师
Pytorch_ Geometric (pyg) uses dataloader to report an error runtimeerror: sizes of tenants must match except in dimension 0
WPF 绑定表达式和绑定数据源(一)
Redis (II) distributed locks and redis cluster construction
Rxjs TakeUntil 操作符的学习笔记
What are the PMP scores?