1 | # 节点类的开发
|
2 |
|
3 | ## 节点类的存储
|
4 |
|
5 | 所有的用户自定义节点存储于 /app/core/ 目录下
|
6 |
|
7 | ## 开发新的节点类
|
8 |
|
9 | 所有的节点类都是 CoreOfBase 的子类。以 CoreOfImage 为例:
|
10 |
|
11 | ```js
|
12 | /**
|
13 | * 实现图像服务端中转的节点类
|
14 | */
|
15 | class CoreOfImage extends CoreOfBase {
|
16 | async Start(app) {
|
17 | //... 代码略
|
18 | }
|
19 |
|
20 | /**
|
21 | * 映射节点类的服务器类型数组,提供给类工厂使用
|
22 | */
|
23 | static get mapping() {
|
24 | if(!this.$mapping) {
|
25 | this.$mapping = ['Image']; //gameconfig.js 中类型为 Image 的节点,都将用 CoreOfImage 完成实例化
|
26 | }
|
27 | return this.$mapping;
|
28 | }
|
29 | /**
|
30 | * 设置节点类的服务器类型数组
|
31 | */
|
32 | static set mapping(val) {
|
33 | this.$mapping = val;
|
34 | }
|
35 | }
|
36 | ```
|
37 |
|
38 | ## 节点的运行
|
39 |
|
40 | 可以在 gameconfig.js 中进行节点的统一配置:
|
41 |
|
42 | ```js
|
43 | let config = {
|
44 | //配置集群中所有的节点,指明其类型、编号、配置信息
|
45 | "servers": {
|
46 | "Index":{ //相同类型的节点分组
|
47 | "1":{ //分组内各节点的编号
|
48 | "UrlHead": "http", //协议选择: http/https
|
49 | "mysql": { //数据库连接信息
|
50 | "logging" : false, //记录日志
|
51 | "db": "gamecloud", //数据库名称
|
52 | "sa": "dev", //数据库用户名
|
53 | "pwd": "helloworld", //数据库用户密码
|
54 | "host": "127.0.0.1", //数据库服务器IP地址
|
55 | "port": 3306 //数据库服务器端口号
|
56 | },
|
57 | "webserver": { //WEB服务器配置信息
|
58 | "mapping": "127.0.0.1", //映射绑定地址
|
59 | "host": "127.0.0.1", //内部IP地址
|
60 | "port": 9901 //监听端口
|
61 | },
|
62 | }
|
63 | }
|
64 | },
|
65 |
|
66 | //配置当前服务器上需要运行的节点
|
67 | "apps":[
|
68 | {
|
69 | "name" : "gamecloud_IOS_1",
|
70 | "script" : "app/start.js",
|
71 | "env": {
|
72 | "NODE_ENV": "production",
|
73 | "sys":{
|
74 | "serverType": "IOS",
|
75 | "serverId": 1,
|
76 | "portal": true //指示该服务器兼任门户
|
77 | }
|
78 | }
|
79 | }
|
80 | ]
|
81 | ```
|
82 |
|
83 | 通过如下语句即可启动一个特定类型的节点:
|
84 |
|
85 | ```js
|
86 | facade.boot({ env: { serverType: "Index", serverId: 1 } });
|
87 | ```
|
88 |
|
89 | 通过如下语句,可以重置内置节点类的分组类型
|
90 |
|
91 | ```js
|
92 | facade.CoreOfLogic.mapping = ['IOS', 'Android', 'Windows', 'Test'];
|
93 | ```
|