gamerpc 8.0.7 → 8.0.8

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.
Files changed (3) hide show
  1. package/README.md +12 -0
  2. package/package.json +1 -1
  3. package/src/index.js +37 -63
package/README.md CHANGED
@@ -57,3 +57,15 @@ import toolkit from require('gamerpc')
57
57
  ```
58
58
 
59
59
  范例参见 ./test/test.html (请在必要时手工修改页面包含文件的链接地址)
60
+
61
+ 注意:如下写法无法在页面嵌套中引用到内部对象,但在NPM依赖包引入模式下没有影响
62
+ ```bash
63
+ module.exports = Remote;
64
+ ```
65
+
66
+ 页面嵌入建议改成如下模式
67
+ ```bash
68
+ const toolkit = exports;
69
+ toolkit.conn = Remote;
70
+ global.toolkit = toolkit;
71
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gamerpc",
3
- "version": "8.0.7",
3
+ "version": "8.0.8",
4
4
  "author": "bookmansoft <ceo@920.cc>",
5
5
  "contributors": [
6
6
  "bookmansoft <ceo@920.cc>"
package/src/index.js CHANGED
@@ -48,17 +48,18 @@ class Remote {
48
48
  * @param {*} ip
49
49
  * @param {*} port
50
50
  */
51
- async createSocket(ip, port) {
52
- if(!ip) {
53
- ip = this.config.webserver.host;
54
- }
55
- if(!port) {
56
- port = this.config.webserver.port;
57
- }
51
+ async createSocket() {
52
+ let port = this.config.webserver.port;
53
+ let host = this.config.webserver.host;
54
+ let url = this.config.webserver.url;
58
55
 
59
56
  this.close();
60
57
 
61
- this.socket = io(`${this.config.UrlHead}://${ip}:${port}`, {'force new connection': true});
58
+ if(!url) {
59
+ this.socket = io(`${this.config.UrlHead}://${host}:${port}`, {'force new connection': true});
60
+ } else {
61
+ this.socket = io(`${this.config.UrlHead}://${url}`, {'force new connection': true});
62
+ }
62
63
  this.socket.on('notify', ret => {//监听推送消息
63
64
  if(this.notifyHandles[ret.type]) {
64
65
  this.notifyHandles[ret.type](ret.info);
@@ -95,33 +96,6 @@ class Remote {
95
96
  return prom;
96
97
  }
97
98
 
98
- /**
99
- * 获取OpenId
100
- */
101
- async getOpenId() {
102
- //此处根据实际需要,发起了基于HTTP请求的认证访问,和本身创建时指定的通讯模式无关。
103
- console.log('gameconn: getOpenId');
104
- let msg = await this.getRequest({
105
- port: this.configOri.webserver.authPort,
106
- openkey: this.userInfo.openkey,
107
- }, this.userInfo.domain);
108
-
109
- //客户端从模拟网关取得了签名集
110
- if(!msg) {
111
- return false;
112
- }
113
-
114
- if(!msg.unionid) {
115
- msg.unionid = msg.openid;
116
- }
117
-
118
- this.userInfo.openid = msg.unionid;
119
- this.userInfo.openkey = msg.access_token;
120
-
121
- console.log('gameconn: getOpenId', msg.unionid);
122
- return true;
123
- }
124
-
125
99
  /**
126
100
  * 获取签名数据集
127
101
  */
@@ -226,13 +200,12 @@ class Remote {
226
200
  this.status.init();
227
201
 
228
202
  console.log('gameconn: lb');
229
- let msg = await this.locate(this.configOri.webserver.host, this.configOri.webserver.port)
230
- .getRequest({"func": "lb.getServerInfo", "oemInfo":{"domain": this.userInfo.domain, "openid": this.userInfo.openid}});
203
+ let msg = await this.locate(this.configOri.webserver.host, this.configOri.webserver.port, this.configOri.webserver.url).getRequest({"func": "lb.getServerInfo", "oemInfo":{"domain": this.userInfo.domain, "openid": this.userInfo.openid}});
231
204
 
232
205
  if(!!msg && msg.code == ReturnCode.Success) {
233
206
  console.log('gameconn: lb', msg.data);
234
207
  this.status.set(CommStatus.lb);
235
- this.locate(msg.data.ip, msg.data.port);
208
+ this.locate(msg.data.ip, msg.data.port, msg.data.url);
236
209
  return true;
237
210
  }
238
211
 
@@ -316,17 +289,6 @@ class Remote {
316
289
  return false;
317
290
  }
318
291
 
319
- //检测执行微信所需要的KeyId转换
320
- if(this.loginMode.check(CommStatus.reqOpenId)) {
321
- if(!this.status.check(CommStatus.OpenId)) {
322
- if(!(await this.getOpenId())) {
323
- throw(new Error('keyId error'));
324
- } else {
325
- this.status.set(CommStatus.OpenId);
326
- }
327
- }
328
- }
329
-
330
292
  //检测执行负载均衡
331
293
  if(this.loginMode.check(CommStatus.reqLb)) {
332
294
  if(!this.status.check(CommStatus.lb)) {
@@ -504,7 +466,7 @@ class Remote {
504
466
  switch(this.rpcMode) {
505
467
  case CommMode.ws:
506
468
  if(!this.socket) {
507
- await this.createSocket(this.config.webserver.host, this.config.webserver.port);
469
+ await this.createSocket();
508
470
  }
509
471
  return new Promise((resolve, reject) => {
510
472
  this.socket.emit('req', params, msg => {
@@ -524,13 +486,14 @@ class Remote {
524
486
 
525
487
  /**
526
488
  * 设定远程服务器地址
527
- * @param ip
489
+ * @param host
528
490
  * @param port
529
491
  * @returns {Remote}
530
492
  */
531
- locate(ip, port){
532
- this.config.webserver.host = ip;
493
+ locate(host, port, url) {
494
+ this.config.webserver.host = host;
533
495
  this.config.webserver.port = port;
496
+ this.config.webserver.url = url;
534
497
 
535
498
  return this;
536
499
  }
@@ -656,15 +619,21 @@ class Remote {
656
619
  async getRequest(params, authControl) {
657
620
  this.parseParams(params);
658
621
 
659
- let port = !!params.port ? params.port : this.config.webserver.port;
660
- let url = !!authControl ? `${this.config.UrlHead}://${this.config.webserver.host}:${port}/${authControl}` : `${this.config.UrlHead}://${this.config.webserver.host}:${port}/index.html`;
622
+ let url = null;
623
+ if(!this.config.webserver.url) {
624
+ let port = !!params.port ? params.port : this.config.webserver.port;
625
+ url = !!authControl ? `${this.config.UrlHead}://${this.config.webserver.host}:${port}/${authControl}` : `${this.config.UrlHead}://${this.config.webserver.host}:${port}/index.html`;
626
+ } else {
627
+ url = !!authControl ? `${this.config.UrlHead}://${this.config.webserver.url}/${authControl}` : `${this.config.UrlHead}://${this.config.webserver.url}/index.html`;
628
+ }
629
+
661
630
  url += "?" + Object.keys(params).reduce((ret, next)=>{
662
- if(ret != '') {
663
- ret += '&';
664
- }
665
- //增加了字段编译,避免特殊字符如汉字对URL拼接造成干扰
666
- return ret + next + "=" + ((typeof params[next]) == "object" ? encodeURIComponent(JSON.stringify(params[next])) : encodeURIComponent(params[next]));
667
- }, '');
631
+ if(ret != '') {
632
+ ret += '&';
633
+ }
634
+ //增加了字段编译,避免特殊字符如汉字对URL拼接造成干扰
635
+ return ret + next + "=" + ((typeof params[next]) == "object" ? encodeURIComponent(JSON.stringify(params[next])) : encodeURIComponent(params[next]));
636
+ }, '');
668
637
 
669
638
  return this.get(url);
670
639
  }
@@ -676,8 +645,13 @@ class Remote {
676
645
  async postRequest(params, authControl) {
677
646
  this.parseParams(params);
678
647
 
679
- let port = !!params.port ? params.port : this.config.webserver.port;
680
- let url = !!authControl ? `${this.config.UrlHead}://${this.config.webserver.host}:${port}/${authControl}` : `${this.config.UrlHead}://${this.config.webserver.host}:${port}/index.html`;
648
+ let url = null;
649
+ if(!this.config.webserver.url) {
650
+ let port = !!params.port ? params.port : this.config.webserver.port;
651
+ url = !!authControl ? `${this.config.UrlHead}://${this.config.webserver.host}:${port}/${authControl}` : `${this.config.UrlHead}://${this.config.webserver.host}:${port}/index.html`;
652
+ } else {
653
+ url = !!authControl ? `${this.config.UrlHead}://${this.config.webserver.url}/${authControl}` : `${this.config.UrlHead}://${this.config.webserver.url}/index.html`;
654
+ }
681
655
 
682
656
  return this.post(url, params);
683
657
  }