cocos-creator Node.js protobuf.js
-
Server:
-
Download and install
-
Check installation by cmd(I used windows):
npm -v
to check npm installationnode -v
to check nodejs installation -
Build the server
-
Go to your project directory first
-
mkDir nodeServer_HelloWorld
Make a new directory, then go to that directory bycd nodeServer_HelloWorld
. -
npm init
Create a new project, leave all the default settings. Then npm will create a new file: package.json automatically. (How does package.json work) -
package.json
Open package.json to see what's in there currently. -
npm install --save express
Install Express(a common web application framework),--save
means you want the npm command to write in your package.json file with Express's versions, which will be added to the dependencies property. -
npm install --save socket.io
Install SocketIO library -
Confirm that the version value have been added to dependencies property in package.json
-
echo >index.js
Will create a new empty file called index.js, which is the entry point for the application. -
Coding in index.js, see server/index.js in this repo
-
-
-
Client
- No tricky config needed. Just see the Cocos-creator project in the repo.
- Scripts related(to socket connection): NetConfig.js, NetControl.js, SocketTest.js
- Create your own .proto files. See example:
server/nodeServer_HelloWorld/protos/Test.userInfo.proto
in this repo - What is protobuf
- Download protobuf.js
- Import protobuf.js to creator, NOT as plugin
- Deal with .proto files from scripts, see ProtoTest.js
- How to use protobuf.js, go to official document
-
Go to the directory where your server is in
-
npm install protobufjs --save
Install protobufjs module -
Import the prepared .proto files
-
pbjs -t static-module -w commonjs -o [target file(s)] [source file name]
Compile .proto files and generate static code to .js files -
Reference .js files which generated from .proto, see index.js
- Run your server:
node index.js
- Run the app from client
- Start communicating