cgserver 6.6.7 → 6.8.447

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -566,7 +566,7 @@ class core {
566
566
  });
567
567
  }
568
568
  static async safeCall(func, thisArg = null, ...params) {
569
- if (!func) {
569
+ if (!func || !core.isFunction(func)) {
570
570
  return;
571
571
  }
572
572
  try {
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.GAsyncQueueTool = void 0;
3
+ exports.GSyncQueueTool = void 0;
4
4
  const Core_1 = require("../Core/Core");
5
5
  class AsyncQueueItem {
6
6
  key = "";
@@ -40,4 +40,4 @@ class AsyncQueueTool {
40
40
  return error;
41
41
  }
42
42
  }
43
- exports.GAsyncQueueTool = new AsyncQueueTool();
43
+ exports.GSyncQueueTool = new AsyncQueueTool();
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.GSyncQueueTool = void 0;
4
+ const Core_1 = require("../Core/Core");
5
+ class SyncQueueItem {
6
+ key = "";
7
+ running = false;
8
+ funcs = [];
9
+ constructor(key) {
10
+ this.key = key;
11
+ }
12
+ async add(func, thisArg = null, ...params) {
13
+ return new Promise((resolve, reject) => {
14
+ this.funcs.push({ func: func, thisArg: thisArg, params: params, resolve: resolve });
15
+ this._run();
16
+ });
17
+ }
18
+ async _run() {
19
+ if (this.running) {
20
+ return;
21
+ }
22
+ this.running = true;
23
+ while (this.funcs.length > 0) {
24
+ let funcitem = this.funcs.shift();
25
+ if (!funcitem.func) {
26
+ continue;
27
+ }
28
+ let rs = await Core_1.core.safeCall(funcitem.func, funcitem.thisArg, ...funcitem.params);
29
+ await Core_1.core.safeCall(funcitem.resolve, null, rs);
30
+ }
31
+ this.running = false;
32
+ }
33
+ }
34
+ class SyncQueueTool {
35
+ //队列数据,一个key队列必须有先后
36
+ _queues = {};
37
+ async add(key, func, thisArg = null, ...params) {
38
+ this._queues[key] = this._queues[key] || new SyncQueueItem(key);
39
+ let error = await this._queues[key].add(func, thisArg, ...params);
40
+ return error;
41
+ }
42
+ }
43
+ exports.GSyncQueueTool = new SyncQueueTool();
@@ -5,6 +5,11 @@ const IWebSocket_1 = require("./IWebSocket");
5
5
  const ws = require("websocket");
6
6
  const Log_1 = require("../Logic/Log");
7
7
  const IProtoFilter_1 = require("./ProtoFilter/IProtoFilter");
8
+ /**
9
+ * 客户端使用的websocket
10
+ * 用它去主动连接服务器
11
+ * 默认自动重连
12
+ */
8
13
  class IClientWebSocket extends IWebSocket_1.IWebSocket {
9
14
  _host = "";
10
15
  /**
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.IRpcClientWebSocket = exports.RpcBaseMsg = void 0;
4
+ const _ = require("underscore");
5
+ const EventTool_1 = require("../Logic/EventTool");
6
+ const Log_1 = require("../Logic/Log");
7
+ const IClientWebSocket_1 = require("./IClientWebSocket");
8
+ const IWebSocket_1 = require("./IWebSocket");
9
+ class RpcBaseMsg extends IWebSocket_1.BaseMsg {
10
+ __rpcid = "";
11
+ }
12
+ exports.RpcBaseMsg = RpcBaseMsg;
13
+ class IRpcClientWebSocket extends IClientWebSocket_1.IClientWebSocket {
14
+ _genId(pre = "") {
15
+ return pre + "_" + Date.now() % 10000000000 + "_" + _.uniqueId() + _.random(9999999);
16
+ }
17
+ getNewMsg(cmd, errcode) {
18
+ let msg = super.getNewMsg(cmd, errcode);
19
+ msg.__rpcid = this._genId(cmd);
20
+ return msg;
21
+ }
22
+ async callRemote(msg) {
23
+ if (!msg) {
24
+ Log_1.GLog.error("send null msg!");
25
+ return;
26
+ }
27
+ if (!msg.__rpcid) {
28
+ msg.__rpcid = this._genId(msg.cmd);
29
+ }
30
+ return new Promise((resolve, reject) => {
31
+ let func = (jsonData) => {
32
+ resolve(jsonData);
33
+ };
34
+ let handler = setTimeout(() => {
35
+ EventTool_1.GEventTool.off(msg.__rpcid, func);
36
+ resolve({ errcode: { id: 10086, des: "timeout" } });
37
+ }, 3000);
38
+ EventTool_1.GEventTool.once(msg.__rpcid, func);
39
+ super.send(msg);
40
+ });
41
+ }
42
+ receive_other_all(jsonData) {
43
+ if (jsonData.__rpcid) {
44
+ EventTool_1.GEventTool.emit(jsonData.__rpcid, jsonData);
45
+ return;
46
+ }
47
+ Log_1.GLog.error({ des: "no handle", jsonData });
48
+ }
49
+ }
50
+ exports.IRpcClientWebSocket = IRpcClientWebSocket;
@@ -4,6 +4,10 @@ exports.IServerWebSocket = void 0;
4
4
  const IWebSocket_1 = require("./IWebSocket");
5
5
  const Log_1 = require("../Logic/Log");
6
6
  const IProtoFilter_1 = require("./ProtoFilter/IProtoFilter");
7
+ /**
8
+ * 服务器接收到的客户端的连接
9
+ * 客户端的session对象
10
+ */
7
11
  class IServerWebSocket extends IWebSocket_1.IWebSocket {
8
12
  _server = null;
9
13
  get server() {
@@ -1,22 +1,31 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.IWebSocket = void 0;
3
+ exports.IWebSocket = exports.BaseMsg = void 0;
4
4
  const Log_1 = require("../Logic/Log");
5
5
  const ProtoFactory_1 = require("./ProtoFilter/ProtoFactory");
6
6
  const IProtoFilter_1 = require("./ProtoFilter/IProtoFilter");
7
7
  const Core_1 = require("../Core/Core");
8
8
  const _ = require("underscore");
9
9
  const ServerConfig_1 = require("../Config/ServerConfig");
10
- let WebSocketIdMgr = {
11
- id: _.random(0, 99999999),
12
- getNewID() {
13
- return this.id++;
14
- },
15
- };
10
+ const SyncQueueTool_1 = require("../Logic/SyncQueueTool");
11
+ class BaseMsg {
12
+ cmd;
13
+ errcode;
14
+ servertime = Date.now();
15
+ constructor(cmd, errcode) {
16
+ this.cmd = cmd;
17
+ this.errcode = errcode;
18
+ }
19
+ }
20
+ exports.BaseMsg = BaseMsg;
16
21
  class IWebSocket {
17
- _await = false;
18
- get await() {
19
- return this._await;
22
+ /**
23
+ * 是否同步消息
24
+ * 默认为true
25
+ */
26
+ _is_sync_msg = true;
27
+ get isSynMsg() {
28
+ return this._is_sync_msg;
20
29
  }
21
30
  _debug_msg = false;
22
31
  get debug_msg() {
@@ -54,16 +63,12 @@ class IWebSocket {
54
63
  }
55
64
  constructor(protoType = IProtoFilter_1.EProtoType.Json, protoPath = "") {
56
65
  this._debug_msg = ServerConfig_1.GServerCfg.debug_msg;
57
- this._socket_id = WebSocketIdMgr.getNewID();
66
+ this._socket_id = parseInt(_.uniqueId());
58
67
  this._protoType = protoType;
59
68
  this._protoPath = protoPath;
60
69
  }
61
70
  getNewMsg(cmd, errcode) {
62
- let msg = {
63
- cmd: cmd,
64
- errcode: errcode,
65
- servertime: new Date().getTime()
66
- };
71
+ let msg = new BaseMsg(cmd, errcode);
67
72
  return msg;
68
73
  }
69
74
  receive_heartbeat(jsonData) {
@@ -84,15 +89,11 @@ class IWebSocket {
84
89
  this._ws.on("close", this.onClose.bind(this));
85
90
  this.onOpen();
86
91
  }
87
- _msgs = [];
88
92
  onMessage(message) {
89
93
  try {
90
94
  let msg = this._onDecode(message);
91
- if (this._await) {
92
- this._msgs.push(msg);
93
- if (!this._run_await) {
94
- this._awaitMessages();
95
- }
95
+ if (this._is_sync_msg) {
96
+ SyncQueueTool_1.GSyncQueueTool.add(this._socket_id + "", this._onMessage, this, msg);
96
97
  }
97
98
  else {
98
99
  this._onMessage(msg);
@@ -116,30 +117,21 @@ class IWebSocket {
116
117
  let msg = this._protoFilter.encode(data, ...params);
117
118
  return msg;
118
119
  }
119
- _run_await = false;
120
- async _awaitMessages() {
121
- this._run_await = true;
122
- while (this._msgs.length > 0) {
123
- let msg = this._msgs.shift();
124
- await this._onMessage(msg);
125
- }
126
- this._run_await = false;
127
- }
128
120
  /**
129
121
  * 过滤消息,每个消息处理之前调用
130
- * @param jsonData
122
+ * @param msg
131
123
  * @returns
132
124
  */
133
- filterMsg(jsonData) {
134
- if (this._debug_msg
135
- && jsonData
136
- && jsonData.cmd != "heartbeat") {
137
- Log_1.GLog.info(this.tipKey + " receive:--------");
138
- Log_1.GLog.info({ tipKey: this.tipKey, jsonData });
139
- }
140
- if (!jsonData.cmd || jsonData.cmd == "") {
141
- Log_1.GLog.error(this.tipKey + ' Received Message warning: no cmd param,data=');
142
- Log_1.GLog.error({ tipKey: this.tipKey, jsonData });
125
+ filterMsg(msg) {
126
+ if (!msg) {
127
+ Log_1.GLog.error({ tipKey: this.tipKey, action: "receive", error: "no cmd", msg });
128
+ return false;
129
+ }
130
+ if (this._debug_msg && msg.cmd != "heartbeat") {
131
+ Log_1.GLog.info({ tipKey: this.tipKey, action: "receive", msg });
132
+ }
133
+ if (!msg.cmd) {
134
+ Log_1.GLog.error({ tipKey: this.tipKey, action: "receive", error: "no cmd", msg });
143
135
  return false;
144
136
  }
145
137
  return true;
@@ -154,40 +146,14 @@ class IWebSocket {
154
146
  else if (!func) {
155
147
  let otherfunc = this["receive_other_all"];
156
148
  if (otherfunc) {
157
- try {
158
- if (Core_1.core.isAsyncFunc(otherfunc)) {
159
- //默认支持其他所有处理消息
160
- await otherfunc.call(this, jsonData).catch((reason) => {
161
- Log_1.GLog.error(reason);
162
- });
163
- }
164
- else {
165
- otherfunc.call(this, jsonData);
166
- }
167
- }
168
- catch (e) {
169
- Log_1.GLog.error(this.tipKey + e.stack);
170
- }
149
+ await Core_1.core.safeCall(otherfunc, this, jsonData);
171
150
  }
172
151
  else {
173
152
  Log_1.GLog.error(this.tipKey + ' Received Message warning: no cmd handle,cmd=' + jsonData.cmd);
174
153
  }
175
154
  }
176
155
  else {
177
- try {
178
- if (Core_1.core.isAsyncFunc(func)) {
179
- //默认支持其他所有处理消息
180
- await func.call(this, jsonData).catch((reason) => {
181
- Log_1.GLog.error(reason);
182
- });
183
- }
184
- else {
185
- func.call(this, jsonData);
186
- }
187
- }
188
- catch (e) {
189
- Log_1.GLog.error(this.tipKey + " error msg:data=" + JSON.stringify(jsonData) + "\n" + e.stack);
190
- }
156
+ await Core_1.core.safeCall(func, this, jsonData);
191
157
  }
192
158
  }
193
159
  onOpen(e) {
@@ -197,21 +163,20 @@ class IWebSocket {
197
163
  onClose(reasonCode, description) {
198
164
  Log_1.GLog.info(this.tipKey + " onClose resonCode=" + reasonCode + " des=" + description);
199
165
  }
200
- send(data) {
166
+ send(msg) {
201
167
  if (!this.connected) {
202
168
  return;
203
169
  }
204
- if (!data) {
170
+ if (!msg) {
205
171
  Log_1.GLog.error(this.tipKey + " Send Message warning:null data!");
206
172
  return;
207
173
  }
208
174
  if (this._debug_msg
209
- && data.cmd != "heartbeat") {
210
- Log_1.GLog.info(this.tipKey + " send:-----------------------------------");
211
- Log_1.GLog.info({ tipKey: this.tipKey, data });
175
+ && msg.cmd != "heartbeat") {
176
+ Log_1.GLog.info({ tipKey: this.tipKey, action: "send", msg });
212
177
  }
213
- let msg = this._onEncode(data);
214
- this._ws.send(msg);
178
+ let data = this._onEncode(msg);
179
+ this._ws.send(data);
215
180
  }
216
181
  close() {
217
182
  this._ws.close();
@@ -0,0 +1,127 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CgMq = exports.CgMqConfig = exports.CgMqRetMsg = void 0;
4
+ const Core_1 = require("../Core/Core");
5
+ const Log_1 = require("../Logic/Log");
6
+ const IRpcClientWebSocket_1 = require("../SocketServer/IRpcClientWebSocket");
7
+ //接受到的消息无需basemsg部分
8
+ class CgMqMsg extends IRpcClientWebSocket_1.RpcBaseMsg {
9
+ /**
10
+ * 必填,目的身份
11
+ */
12
+ to_identity = "";
13
+ /**
14
+ * 消息携带的数据
15
+ */
16
+ data = null;
17
+ }
18
+ class CgMqRetMsg extends CgMqMsg {
19
+ /**
20
+ * 发送者身份
21
+ */
22
+ from_identity = "";
23
+ /**
24
+ * audience 数量
25
+ */
26
+ count = 0;
27
+ }
28
+ exports.CgMqRetMsg = CgMqRetMsg;
29
+ class CgMqServerWebsocket extends IRpcClientWebSocket_1.IRpcClientWebSocket {
30
+ _cgmq = null;
31
+ /**
32
+ * 自己的身份
33
+ */
34
+ _identity = "";
35
+ constructor(cgmq) {
36
+ super();
37
+ this._cgmq = cgmq;
38
+ this._identity = this._cgmq.cfg.identity;
39
+ }
40
+ onOpen(e) {
41
+ this.init(this._cgmq.cfg.identity);
42
+ }
43
+ async init(identity) {
44
+ let msg = this.getNewMsg("init");
45
+ msg.identity = identity;
46
+ let jsonData = await this.callRemote(msg);
47
+ return jsonData;
48
+ }
49
+ async push(to_identity, data) {
50
+ let msg = this.getNewMsg("msg");
51
+ msg.to_identity = to_identity;
52
+ msg.data = data;
53
+ let jsonData = this.callRemote(msg);
54
+ return jsonData;
55
+ }
56
+ async receive_msg(msg) {
57
+ let data = await this._cgmq.onMsg(msg);
58
+ msg.data = data;
59
+ this.send(msg);
60
+ }
61
+ }
62
+ class CgMqConfig {
63
+ identity = "";
64
+ host = "";
65
+ port = -1;
66
+ }
67
+ exports.CgMqConfig = CgMqConfig;
68
+ class CgMq {
69
+ _ws = null;
70
+ _inited = false;
71
+ _cfg = null;
72
+ _onmsg = null;
73
+ get cfg() {
74
+ return this._cfg;
75
+ }
76
+ get identity() {
77
+ return this._cfg?.identity;
78
+ }
79
+ async init(cfg, onmsg) {
80
+ if (!cfg) {
81
+ return false;
82
+ }
83
+ this._cfg = cfg;
84
+ this._onmsg = onmsg;
85
+ if (this._inited) {
86
+ Log_1.GLog.error("dulplicate init for CgMq");
87
+ return true;
88
+ }
89
+ this._inited = true;
90
+ if (!this._ws) {
91
+ this._ws = new CgMqServerWebsocket(this);
92
+ }
93
+ return new Promise(async (resolve, reject) => {
94
+ this._ws.connect(cfg.host, cfg.port);
95
+ let pretime = Date.now();
96
+ while (true) {
97
+ if (this._ws.connected) {
98
+ resolve(true);
99
+ break;
100
+ }
101
+ let now = Date.now();
102
+ if (now - pretime >= 3 * 1000) {
103
+ this._ws.close();
104
+ resolve(false);
105
+ break;
106
+ }
107
+ await Core_1.core.sleep(100);
108
+ }
109
+ });
110
+ }
111
+ async callRemote(to_identity, func_name, ...args) {
112
+ let data = {
113
+ cmd: func_name,
114
+ args: args
115
+ };
116
+ let jsonData = await this._ws.push(to_identity, data);
117
+ return jsonData;
118
+ }
119
+ async onMsg(msg) {
120
+ if (this._onmsg) {
121
+ let data = await Core_1.core.safeCall(this._onmsg, msg);
122
+ return data;
123
+ }
124
+ return;
125
+ }
126
+ }
127
+ exports.CgMq = CgMq;
@@ -0,0 +1,140 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CgMq = exports.CgMqConfig = void 0;
4
+ const Core_1 = require("../Core/Core");
5
+ const Log_1 = require("../Logic/Log");
6
+ const IRpcClientWebSocket_1 = require("../SocketServer/IRpcClientWebSocket");
7
+ const _ = require("underscore");
8
+ //接受到的消息无需basemsg部分
9
+ class CgMqMsg extends IRpcClientWebSocket_1.RpcBaseMsg {
10
+ /**
11
+ * 必填,目的身份
12
+ */
13
+ to_identity = "";
14
+ /**
15
+ * 消息携带的数据
16
+ */
17
+ data = null;
18
+ }
19
+ class CgMqRetMsg extends CgMqMsg {
20
+ /**
21
+ * 发送者身份
22
+ */
23
+ from_identity = "";
24
+ /**
25
+ * audience 数量
26
+ */
27
+ count = 0;
28
+ }
29
+ class CgMqServerWebsocket extends IRpcClientWebSocket_1.IRpcClientWebSocket {
30
+ _cgmq = null;
31
+ /**
32
+ * 自己的身份
33
+ */
34
+ _identity = "";
35
+ constructor(cgmq) {
36
+ super();
37
+ this._cgmq = cgmq;
38
+ this._identity = this._cgmq.cfg.identity;
39
+ }
40
+ onOpen(e) {
41
+ this.listen(_.toArray(this._cgmq.listenIdentities));
42
+ }
43
+ //
44
+ async listen(identities) {
45
+ let msg = this.getNewMsg("listen");
46
+ msg.identities = identities;
47
+ let jsonData = await this.callRemote(msg);
48
+ return jsonData;
49
+ }
50
+ async cancelListen(identity) {
51
+ let msg = this.getNewMsg("cancelListen");
52
+ msg.identity = identity;
53
+ let jsonData = this.callRemote(msg);
54
+ return jsonData;
55
+ }
56
+ push(to_identity, data) {
57
+ let msg = this.getNewMsg("msg");
58
+ msg.to_identity = to_identity;
59
+ msg.data = data;
60
+ this.send(msg);
61
+ }
62
+ }
63
+ class CgMqConfig {
64
+ identity = "";
65
+ host = "";
66
+ port = -1;
67
+ }
68
+ exports.CgMqConfig = CgMqConfig;
69
+ class CgMq {
70
+ _ws = null;
71
+ _inited = false;
72
+ _cfg = null;
73
+ get cfg() {
74
+ return this._cfg;
75
+ }
76
+ get identity() {
77
+ return this._cfg?.identity;
78
+ }
79
+ //用kv方便查一些
80
+ _listen_identities = {};
81
+ get listenIdentities() {
82
+ return this._listen_identities;
83
+ }
84
+ async init(cfg, identities) {
85
+ if (!cfg) {
86
+ return false;
87
+ }
88
+ for (let key in identities) {
89
+ let id = identities[key];
90
+ this._listen_identities[id] = id;
91
+ }
92
+ this._cfg = cfg;
93
+ if (this._inited) {
94
+ Log_1.GLog.error("dulplicate init for CgMq");
95
+ return true;
96
+ }
97
+ this._inited = true;
98
+ if (!this._ws) {
99
+ this._ws = new CgMqServerWebsocket(this);
100
+ }
101
+ return new Promise(async (resolve, reject) => {
102
+ this._ws.connect(cfg.host, cfg.port);
103
+ let pretime = Date.now();
104
+ while (true) {
105
+ if (this._ws.connected) {
106
+ resolve(true);
107
+ break;
108
+ }
109
+ let now = Date.now();
110
+ if (now - pretime >= 3 * 1000) {
111
+ this._ws.close();
112
+ resolve(false);
113
+ break;
114
+ }
115
+ await Core_1.core.sleep(100);
116
+ }
117
+ });
118
+ }
119
+ async listen(identity) {
120
+ if (this._listen_identities[identity]) {
121
+ return true;
122
+ }
123
+ if (!this._ws || !this._ws.connected) {
124
+ return false;
125
+ }
126
+ let ret = this._ws.listen([identity]);
127
+ return ret;
128
+ }
129
+ async cancelListen(identity) {
130
+ if (this._listen_identities[identity]) {
131
+ return true;
132
+ }
133
+ if (!this._ws || !this._ws.connected) {
134
+ return false;
135
+ }
136
+ let ret = await this._ws.cancelListen(identity);
137
+ return ret;
138
+ }
139
+ }
140
+ exports.CgMq = CgMq;
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Rpc = exports.RpcConfig = void 0;
4
+ const Core_1 = require("../Core/Core");
5
+ const Log_1 = require("../Logic/Log");
6
+ const CgMq_1 = require("./CgMq");
7
+ class Remote {
8
+ _cgmq = null;
9
+ get cgmq() {
10
+ return this._cgmq;
11
+ }
12
+ _to_identity = "";
13
+ constructor(identity, cgmq) {
14
+ this._to_identity = identity;
15
+ this._cgmq = cgmq;
16
+ }
17
+ async call(func_name, ...args) {
18
+ let jsonData = await this._cgmq.callRemote(this._to_identity, func_name, ...args);
19
+ return jsonData;
20
+ }
21
+ }
22
+ class RpcConfig extends CgMq_1.CgMqConfig {
23
+ }
24
+ exports.RpcConfig = RpcConfig;
25
+ class Rpc {
26
+ _cgmq = null;
27
+ get cgmq() {
28
+ return this._cgmq;
29
+ }
30
+ async init(cfg) {
31
+ this._cgmq = new CgMq_1.CgMq();
32
+ let ret = await this._cgmq.init(cfg, this.onMsg.bind(this));
33
+ return ret;
34
+ }
35
+ getRemote(identity) {
36
+ return new Remote(identity, this._cgmq);
37
+ }
38
+ async onMsg(msg) {
39
+ if (!msg || !msg.data || !msg.data.cmd) {
40
+ return;
41
+ }
42
+ let cmd = msg.data.cmd;
43
+ let func = this[cmd];
44
+ if (!func) {
45
+ Log_1.GLog.error({ des: "rpc no cmd", msg });
46
+ return;
47
+ }
48
+ let data = await Core_1.core.safeCall(func);
49
+ return data;
50
+ }
51
+ }
52
+ exports.Rpc = Rpc;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.GRpcTool = void 0;
4
+ class Remote {
5
+ call(func_name, ...args) {
6
+ }
7
+ }
8
+ class RpcTool {
9
+ _cgmq;
10
+ async init() {
11
+ }
12
+ }
13
+ exports.GRpcTool = new RpcTool();
package/dist/lib/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.GoogleProtoFilter = exports.MongoCacheModel = exports.GMongoCacheSer = exports.MongoUserModel = exports.MysqlUserModel = exports.MongoUserService = exports.MysqlUserService = exports.GMongoAccountSer = exports.MongoAccountService = exports.GMysqlAccountSer = exports.MysqlAccountService = exports.EAccountFrom = exports.EUserState = exports.ERoleGroup = exports.GLog = exports.GHttpTool = exports.GCacheTool = exports.RedisManager = exports.GRedisMgr = exports.GMysqlMgr = exports.MysqlBaseService = exports.GMSSqlMgr = exports.GMongoMgr = exports.MongoBaseModel = exports.MysqlBaseModel = exports.MongoBaseService = exports.EPropertyType = exports.Type = exports.Table = exports.Property = exports.PrimaryKey = exports.NotNull = exports.AutoIncrement = exports.Timer = exports.core = exports.GServerCfg = exports.ServerConfig = exports.FrameworkConfig = exports.Config = exports.FrameworkErrorCode = exports.GTriggerMgr = exports.Trigger = exports.Point = exports.Entity = exports.BehaviorAI = exports.AStar = exports.AiObject = exports.GDBCache = exports.GProtoFactory = exports.GCgServer = void 0;
4
- exports.EAccountState = exports.GEventTool = exports.GAsyncQueueTool = exports.WebServerConfig = exports.Response = exports.Request = exports.RazorJs = exports.Engine = exports.GCtrMgr = exports.JsonCreatorValidate = exports.JsonAuthorityValidate = exports.JsonAdminValidate = exports.CreatorValidate = exports.AuthorityValidate = exports.AdminValidate = exports.MongoAccountModel = exports.MysqlAccountModel = exports.MongoBaseUserController = exports.BaseUserController = exports.BaseController = exports.IWebServer = exports.GWechatTool = exports.GWechatOATool = exports.GQQTool = exports.GQiniuTool = exports.GOpenSocial = exports.GEmailTool = exports.GAppleTool = exports.GSmsTool = exports.GAlipayTool = exports.IWebSocket = exports.ISocketServer = exports.IServerWebSocket = exports.IClientWebSocket = exports.JsonProtoFilter = void 0;
4
+ exports.CgMqConfig = exports.CgMq = exports.RpcConfig = exports.Rpc = exports.EAccountState = exports.GEventTool = exports.GSyncQueueTool = exports.WebServerConfig = exports.Response = exports.Request = exports.RazorJs = exports.Engine = exports.GCtrMgr = exports.JsonCreatorValidate = exports.JsonAuthorityValidate = exports.JsonAdminValidate = exports.CreatorValidate = exports.AuthorityValidate = exports.AdminValidate = exports.MongoAccountModel = exports.MysqlAccountModel = exports.MongoBaseUserController = exports.BaseUserController = exports.BaseController = exports.IWebServer = exports.GWechatTool = exports.GWechatOATool = exports.GQQTool = exports.GQiniuTool = exports.GOpenSocial = exports.GEmailTool = exports.GAppleTool = exports.GSmsTool = exports.GAlipayTool = exports.IWebSocket = exports.ISocketServer = exports.IServerWebSocket = exports.IClientWebSocket = exports.JsonProtoFilter = void 0;
5
5
  var cgserver_1 = require("./cgserver");
6
6
  Object.defineProperty(exports, "GCgServer", { enumerable: true, get: function () { return cgserver_1.GCgServer; } });
7
7
  var ProtoFactory_1 = require("./SocketServer/ProtoFilter/ProtoFactory");
@@ -158,9 +158,15 @@ var Response_1 = require("./WebServer/Engine/Response");
158
158
  Object.defineProperty(exports, "Response", { enumerable: true, get: function () { return Response_1.Response; } });
159
159
  var FrameworkConfig_2 = require("./Config/FrameworkConfig");
160
160
  Object.defineProperty(exports, "WebServerConfig", { enumerable: true, get: function () { return FrameworkConfig_2.WebServerConfig; } });
161
- var AsyncQueueTool_1 = require("./Logic/AsyncQueueTool");
162
- Object.defineProperty(exports, "GAsyncQueueTool", { enumerable: true, get: function () { return AsyncQueueTool_1.GAsyncQueueTool; } });
161
+ var SyncQueueTool_1 = require("./Logic/SyncQueueTool");
162
+ Object.defineProperty(exports, "GSyncQueueTool", { enumerable: true, get: function () { return SyncQueueTool_1.GSyncQueueTool; } });
163
163
  var EventTool_1 = require("./Logic/EventTool");
164
164
  Object.defineProperty(exports, "GEventTool", { enumerable: true, get: function () { return EventTool_1.GEventTool; } });
165
165
  var ini_2 = require("./Service/ini");
166
166
  Object.defineProperty(exports, "EAccountState", { enumerable: true, get: function () { return ini_2.EAccountState; } });
167
+ var Rpc_1 = require("./ThirdParty/Rpc");
168
+ Object.defineProperty(exports, "Rpc", { enumerable: true, get: function () { return Rpc_1.Rpc; } });
169
+ Object.defineProperty(exports, "RpcConfig", { enumerable: true, get: function () { return Rpc_1.RpcConfig; } });
170
+ var CgMq_1 = require("./ThirdParty/CgMq");
171
+ Object.defineProperty(exports, "CgMq", { enumerable: true, get: function () { return CgMq_1.CgMq; } });
172
+ Object.defineProperty(exports, "CgMqConfig", { enumerable: true, get: function () { return CgMq_1.CgMqConfig; } });
@@ -99,6 +99,6 @@ export declare class core {
99
99
  * @param num
100
100
  */
101
101
  static convertToGlobalStr(num: number): string;
102
- static sleep(milliseconds: any): Promise<unknown>;
102
+ static sleep(milliseconds: number): Promise<unknown>;
103
103
  static safeCall(func: Function, thisArg?: any, ...params: any[]): Promise<any>;
104
104
  }
@@ -17,5 +17,5 @@ declare class AsyncQueueTool {
17
17
  };
18
18
  add(key: string, func: Function, thisArg?: any, ...params: any[]): Promise<unknown>;
19
19
  }
20
- export declare let GAsyncQueueTool: AsyncQueueTool;
20
+ export declare let GSyncQueueTool: AsyncQueueTool;
21
21
  export {};
@@ -0,0 +1,21 @@
1
+ declare class SyncQueueItem {
2
+ key: string;
3
+ running: boolean;
4
+ funcs: {
5
+ func: Function;
6
+ thisArg: any;
7
+ params: any[];
8
+ resolve?: Function;
9
+ }[];
10
+ constructor(key: string);
11
+ add(func: Function, thisArg?: any, ...params: any[]): Promise<unknown>;
12
+ protected _run(): Promise<void>;
13
+ }
14
+ declare class SyncQueueTool {
15
+ protected _queues: {
16
+ [key: string]: SyncQueueItem;
17
+ };
18
+ add(key: string, func: Function, thisArg?: any, ...params: any[]): Promise<unknown>;
19
+ }
20
+ export declare let GSyncQueueTool: SyncQueueTool;
21
+ export {};
@@ -1,6 +1,11 @@
1
1
  import { IWebSocket } from './IWebSocket';
2
2
  import * as ws from 'websocket';
3
3
  import { EProtoType } from './ProtoFilter/IProtoFilter';
4
+ /**
5
+ * 客户端使用的websocket
6
+ * 用它去主动连接服务器
7
+ * 默认自动重连
8
+ */
4
9
  export declare class IClientWebSocket extends IWebSocket {
5
10
  protected _host: string;
6
11
  /**
@@ -0,0 +1,14 @@
1
+ import { IClientWebSocket } from "./IClientWebSocket";
2
+ import { BaseMsg } from "./IWebSocket";
3
+ export declare class RpcBaseMsg extends BaseMsg {
4
+ __rpcid: string;
5
+ }
6
+ export declare class IRpcClientWebSocket extends IClientWebSocket {
7
+ protected _genId(pre?: string): string;
8
+ getNewMsg(cmd: string, errcode?: {
9
+ id: number;
10
+ des: string;
11
+ }): any;
12
+ callRemote(msg: RpcBaseMsg): Promise<unknown>;
13
+ receive_other_all(jsonData: any): void;
14
+ }
@@ -2,6 +2,10 @@ import { ISocketServer } from './ISocketServer';
2
2
  import { IWebSocket } from './IWebSocket';
3
3
  import { EProtoType } from './ProtoFilter/IProtoFilter';
4
4
  import * as ws from 'websocket';
5
+ /**
6
+ * 服务器接收到的客户端的连接
7
+ * 客户端的session对象
8
+ */
5
9
  export declare class IServerWebSocket extends IWebSocket {
6
10
  protected _server: ISocketServer;
7
11
  get server(): ISocketServer;
@@ -1,8 +1,24 @@
1
1
  import { IProtoFilter, EProtoType } from "./ProtoFilter/IProtoFilter";
2
2
  import * as ws from 'websocket';
3
+ export declare class BaseMsg {
4
+ cmd: string;
5
+ errcode?: {
6
+ id: number;
7
+ des: string;
8
+ };
9
+ servertime: number;
10
+ constructor(cmd: string, errcode?: {
11
+ id: number;
12
+ des: string;
13
+ });
14
+ }
3
15
  export declare class IWebSocket {
4
- protected _await: boolean;
5
- get await(): boolean;
16
+ /**
17
+ * 是否同步消息
18
+ * 默认为true
19
+ */
20
+ protected _is_sync_msg: boolean;
21
+ get isSynMsg(): boolean;
6
22
  protected _debug_msg: boolean;
7
23
  get debug_msg(): boolean;
8
24
  set debug_msg(value: boolean);
@@ -18,27 +34,27 @@ export declare class IWebSocket {
18
34
  get remoteHost(): string;
19
35
  get connected(): boolean;
20
36
  constructor(protoType?: EProtoType, protoPath?: string);
21
- getNewMsg(cmd: any, errcode?: any): any;
37
+ getNewMsg(cmd: string, errcode?: {
38
+ id: number;
39
+ des: string;
40
+ }): BaseMsg | any;
22
41
  receive_heartbeat(jsonData: any): void;
23
42
  send_heartbeat(): void;
24
43
  onConnect(_ws: ws.connection): void;
25
- protected _msgs: Array<any>;
26
44
  onMessage(message: ws.Message): void;
27
45
  protected _onDecode(message: ws.Message, ...params: any[]): any;
28
46
  protected _onEncode(data: any, ...params: any[]): any;
29
- protected _run_await: boolean;
30
- protected _awaitMessages(): Promise<void>;
31
47
  /**
32
48
  * 过滤消息,每个消息处理之前调用
33
- * @param jsonData
49
+ * @param msg
34
50
  * @returns
35
51
  */
36
- filterMsg(jsonData: any): boolean;
52
+ filterMsg(msg: BaseMsg): boolean;
37
53
  protected _onMessage(data: any): Promise<void>;
38
54
  onOpen(e?: any): void;
39
55
  onError(e: Error): void;
40
56
  onClose(reasonCode: number, description: string): void;
41
- send(data: any): void;
57
+ send(msg: BaseMsg): void;
42
58
  close(): void;
43
59
  getServerNameFromCmd(cmd: any): any;
44
60
  }
@@ -0,0 +1,50 @@
1
+ import { IRpcClientWebSocket, RpcBaseMsg } from "../SocketServer/IRpcClientWebSocket";
2
+ declare class CgMqMsg extends RpcBaseMsg {
3
+ /**
4
+ * 必填,目的身份
5
+ */
6
+ to_identity: string;
7
+ /**
8
+ * 消息携带的数据
9
+ */
10
+ data: any;
11
+ }
12
+ export declare class CgMqRetMsg extends CgMqMsg {
13
+ /**
14
+ * 发送者身份
15
+ */
16
+ from_identity: string;
17
+ /**
18
+ * audience 数量
19
+ */
20
+ count: number;
21
+ }
22
+ declare class CgMqServerWebsocket extends IRpcClientWebSocket {
23
+ protected _cgmq: CgMq;
24
+ /**
25
+ * 自己的身份
26
+ */
27
+ protected _identity: string;
28
+ constructor(cgmq: CgMq);
29
+ onOpen(e?: any): void;
30
+ init(identity: string): Promise<unknown>;
31
+ push(to_identity: string, data: any): Promise<unknown>;
32
+ receive_msg(msg: CgMqRetMsg): Promise<void>;
33
+ }
34
+ export declare class CgMqConfig {
35
+ identity: string;
36
+ host: string;
37
+ port: number;
38
+ }
39
+ export declare class CgMq {
40
+ protected _ws: CgMqServerWebsocket;
41
+ protected _inited: boolean;
42
+ protected _cfg: CgMqConfig;
43
+ protected _onmsg: (msg: CgMqRetMsg) => any;
44
+ get cfg(): CgMqConfig;
45
+ get identity(): string;
46
+ init(cfg: CgMqConfig, onmsg?: (msg: CgMqRetMsg) => any): Promise<unknown>;
47
+ callRemote(to_identity: string, func_name: string, ...args: any[]): Promise<unknown>;
48
+ onMsg(msg: CgMqRetMsg): Promise<any>;
49
+ }
50
+ export {};
@@ -0,0 +1,35 @@
1
+ import { IRpcClientWebSocket } from "../SocketServer/IRpcClientWebSocket";
2
+ declare class CgMqServerWebsocket extends IRpcClientWebSocket {
3
+ protected _cgmq: CgMq;
4
+ /**
5
+ * 自己的身份
6
+ */
7
+ protected _identity: string;
8
+ constructor(cgmq: CgMq);
9
+ onOpen(e?: any): void;
10
+ listen(identities: string[]): Promise<unknown>;
11
+ cancelListen(identity: string): Promise<unknown>;
12
+ push(to_identity: string, data: any): void;
13
+ }
14
+ export declare class CgMqConfig {
15
+ identity: string;
16
+ host: string;
17
+ port: number;
18
+ }
19
+ export declare class CgMq {
20
+ protected _ws: CgMqServerWebsocket;
21
+ protected _inited: boolean;
22
+ protected _cfg: CgMqConfig;
23
+ get cfg(): CgMqConfig;
24
+ get identity(): string;
25
+ protected _listen_identities: {
26
+ [identity: string]: string;
27
+ };
28
+ get listenIdentities(): {
29
+ [identity: string]: string;
30
+ };
31
+ init(cfg: CgMqConfig, identities: string[]): Promise<unknown>;
32
+ listen(identity: string): Promise<unknown>;
33
+ cancelListen(identity: string): Promise<unknown>;
34
+ }
35
+ export {};
@@ -0,0 +1,18 @@
1
+ import { CgMq, CgMqConfig, CgMqRetMsg } from "./CgMq";
2
+ declare class Remote {
3
+ protected _cgmq: CgMq;
4
+ get cgmq(): CgMq;
5
+ protected _to_identity: string;
6
+ constructor(identity: string, cgmq: CgMq);
7
+ call(func_name: string, ...args: any[]): Promise<unknown>;
8
+ }
9
+ export declare class RpcConfig extends CgMqConfig {
10
+ }
11
+ export declare class Rpc {
12
+ protected _cgmq: CgMq;
13
+ get cgmq(): CgMq;
14
+ init(cfg: RpcConfig): Promise<unknown>;
15
+ getRemote(identity: string): Remote;
16
+ onMsg(msg: CgMqRetMsg): Promise<any>;
17
+ }
18
+ export {};
@@ -0,0 +1,6 @@
1
+ declare class RpcTool {
2
+ protected _cgmq: any;
3
+ init(): Promise<void>;
4
+ }
5
+ export declare let GRpcTool: RpcTool;
6
+ export {};
@@ -73,6 +73,8 @@ export { RazorJs } from './WebServer/Engine/RazorJs';
73
73
  export { Request } from './WebServer/Engine/Request';
74
74
  export { Response } from './WebServer/Engine/Response';
75
75
  export { WebServerConfig } from './Config/FrameworkConfig';
76
- export { GAsyncQueueTool } from './Logic/AsyncQueueTool';
76
+ export { GSyncQueueTool } from './Logic/SyncQueueTool';
77
77
  export { GEventTool } from './Logic/EventTool';
78
78
  export { EAccountState } from './Service/ini';
79
+ export { Rpc, RpcConfig } from './ThirdParty/Rpc';
80
+ export { CgMq, CgMqConfig } from './ThirdParty/CgMq';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cgserver",
3
- "version": "6.6.7",
3
+ "version": "6.8.447",
4
4
  "author": "trojan",
5
5
  "type": "commonjs",
6
6
  "description": "free for all.Websocket or Http",