当前位置:网站首页>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
边栏推荐
- i++ 和 ++i的真正区别
- What functions should smart agriculture applet system design have
- 匯付國際為跨境電商賦能:做合規的跨境支付平臺!
- What should be paid attention to in PMP examination?
- Exception: gradle task assemblydebug failed with exit code 1
- [MySQL learning notes 21] storage engine
- Basic usage and principle of schedulemaster distributed task scheduling center
- Wallys/MULTI-FUNCTION IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL
- An auxiliary MVP architecture project quick development library -mvpfastdagger
- JS tool function, self encapsulating a throttling function
猜你喜欢

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

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

Mongodb's principle, basic use, clustering and partitioned clustering

manhattan_ Slam environment configuration

Mengyou Technology: tiktok live broadcast with goods elements hot topics retention skills shaping image highlight selling points

如何在Microsoft Exchange 2010中安装SSL证书

Learning notes of rxjs takeuntil operator

Rxjs TakeUntil 操作符的学习笔记

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

How much does a wechat applet cost? Wechat applet development and production costs? Come and have a look
随机推荐
How do wechat sell small commodity programs do? How to open wechat apps to sell things?
Experience in writing C
[shared farm] smart agriculture applet, customized development and secondary development of Kaiyuan source code, which is more appropriate?
Basic use and cluster construction of consult
How to make a self-service order wechat applet? How to do the wechat order applet? visual editing
How to make small programs on wechat? How to make small programs on wechat
Wallys/MULTI-FUNCTION IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL
Kotlin keyword and operator
ScheduleMaster分布式任务调度中心基本使用和原理
puzzle(019.2)六边锁
Fluent: target support file /pods runner / pods runner frameworks Sh: permission denied - stack overflow
Flutter replaces the default icon of Gaud positioning
x86电脑上下载debian的arm64的包
Mysql 源码阅读(二)登录连接调试
Jetpack compose layout (II) - material components and layout
Redis (II) distributed locks and redis cluster construction
独步武林,架构选型手册(包含 PDF)
CYCA 2022少儿形体礼仪初级师资班 深圳总部站圆满结束
字符串 实现 strStr()
Can two Mitsubishi PLC adopt bcnettcp protocol to realize wireless communication of network interface?