gs2 1.0.6 → 1.3.2

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.
@@ -0,0 +1,5 @@
1
+ import { Gs2WebSocketSession } from './model';
2
+ export default class {
3
+ session: Gs2WebSocketSession;
4
+ constructor(session: Gs2WebSocketSession);
5
+ }
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ /*
3
+ Copyright 2016 Game Server Services, Inc. or its affiliates. All Rights
4
+ Reserved.
5
+
6
+ Licensed under the Apache License, Version 2.0 (the "License").
7
+ You may not use this file except in compliance with the License.
8
+ A copy of the License is located at
9
+
10
+ http://www.apache.org/licenses/LICENSE-2.0
11
+
12
+ or in the "license" file accompanying this file. This file is distributed
13
+ on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
14
+ express or implied. See the License for the specific language governing
15
+ permissions and limitations under the License.
16
+ */
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ var default_1 = /** @class */ (function () {
19
+ function default_1(session) {
20
+ this.session = session;
21
+ }
22
+ return default_1;
23
+ }());
24
+ exports.default = default_1;
25
+ //# sourceMappingURL=AbstractGs2WebSocketClient.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AbstractGs2WebSocketClient.js","sourceRoot":"","sources":["../src/core/AbstractGs2WebSocketClient.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;AAIH;IAIE,mBAAY,OAA4B;QACtC,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IACzB,CAAC;IACH,gBAAC;AAAD,CAAC,AAPD,IAOC"}
package/core/index.d.ts CHANGED
@@ -1,8 +1,9 @@
1
1
  import AbstractGs2Client from './AbstractGs2RestClient';
2
- import { Gs2RestSession, BasicGs2Credential, ProjectTokenGs2Credential } from './model';
2
+ import { Gs2RestSession, Gs2WebSocketSession, BasicGs2Credential, ProjectTokenGs2Credential } from './model';
3
3
  declare const _default: {
4
4
  AbstractGs2Client: typeof AbstractGs2Client;
5
5
  Gs2RestSession: typeof Gs2RestSession;
6
+ Gs2WebSocketSession: typeof Gs2WebSocketSession;
6
7
  BasicGs2Credential: typeof BasicGs2Credential;
7
8
  ProjectTokenGs2Credential: typeof ProjectTokenGs2Credential;
8
9
  Region: {
package/core/index.js CHANGED
@@ -21,6 +21,7 @@ var model_1 = require("./model");
21
21
  exports.default = {
22
22
  AbstractGs2Client: AbstractGs2RestClient_1.default,
23
23
  Gs2RestSession: model_1.Gs2RestSession,
24
+ Gs2WebSocketSession: model_1.Gs2WebSocketSession,
24
25
  BasicGs2Credential: model_1.BasicGs2Credential,
25
26
  ProjectTokenGs2Credential: model_1.ProjectTokenGs2Credential,
26
27
  Region: model_1.Region,
package/core/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/core/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;;AAEH,+FAAwD;AACxD,iCAAgG;AAEhG,kBAAe;IACb,iBAAiB,iCAAA;IACjB,cAAc,wBAAA;IACd,kBAAkB,4BAAA;IAClB,yBAAyB,mCAAA;IACzB,MAAM,gBAAA;CACP,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/core/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;;AAEH,+FAAwD;AACxD,iCAAqH;AAErH,kBAAe;IACb,iBAAiB,iCAAA;IACjB,cAAc,wBAAA;IACd,mBAAmB,6BAAA;IACnB,kBAAkB,4BAAA;IAClB,yBAAyB,mCAAA;IACzB,MAAM,gBAAA;CACP,CAAC"}
package/core/model.d.ts CHANGED
@@ -14,6 +14,30 @@ export declare class Gs2RestSession {
14
14
  connect(): Promise<LoginResult> | undefined;
15
15
  disconnect(): void;
16
16
  }
17
+ export declare class Gs2WebSocketSession {
18
+ credential: IGs2Credential;
19
+ private client;
20
+ region: string;
21
+ projectToken: string | null;
22
+ expiresAt: number | null;
23
+ private inflightRequest;
24
+ private onOpenHandlers;
25
+ private onErrorHandlers;
26
+ private onCloseHandlers;
27
+ private onNotificationHandlers;
28
+ constructor(credential: IGs2Credential, region: string);
29
+ connect(): Promise<any>;
30
+ send(service: string, component: string, func: string, payload: any): Promise<any>;
31
+ onOpen(func: (ev: Event) => any): void;
32
+ onError(func: (ev: Event) => any): void;
33
+ onClose(func: (ev: CloseEvent) => any): void;
34
+ onNotification(func: (message: {
35
+ subject: string;
36
+ issuer: string;
37
+ payload: any;
38
+ }) => any): void;
39
+ disconnect(): Promise<any>;
40
+ }
17
41
  declare class LoginResult {
18
42
  accessToken: string;
19
43
  tokenType: string;
@@ -36,6 +60,7 @@ export declare class ProjectTokenGs2Credential implements IGs2Credential {
36
60
  }
37
61
  export declare const Gs2Constant: {
38
62
  ENDPOINT_HOST: string;
63
+ WS_ENDPOINT_HOST: string;
39
64
  };
40
65
  export declare const Region: {
41
66
  AP_NORTHEAST_1: string;
package/core/model.js CHANGED
@@ -15,9 +15,12 @@ express or implied. See the License for the specific language governing
15
15
  permissions and limitations under the License.
16
16
  */
17
17
  Object.defineProperty(exports, "__esModule", { value: true });
18
- exports.Region = exports.Gs2Constant = exports.ProjectTokenGs2Credential = exports.ProjectToken = exports.Gs2RestSession = exports.BasicGs2Credential = void 0;
18
+ exports.Region = exports.Gs2Constant = exports.ProjectTokenGs2Credential = exports.ProjectToken = exports.Gs2WebSocketSession = exports.Gs2RestSession = exports.BasicGs2Credential = void 0;
19
19
  var tslib_1 = require("tslib");
20
20
  var axios_1 = (0, tslib_1.__importDefault)(require("axios"));
21
+ var async_wait_until_1 = (0, tslib_1.__importDefault)(require("async-wait-until"));
22
+ var uuid_1 = require("uuid");
23
+ var NodeWebSocket = require('ws');
21
24
  var BasicGs2Credential = /** @class */ (function () {
22
25
  function BasicGs2Credential(clientId, clientSecret) {
23
26
  this.clientId = clientId;
@@ -65,6 +68,171 @@ var Gs2RestSession = /** @class */ (function () {
65
68
  return Gs2RestSession;
66
69
  }());
67
70
  exports.Gs2RestSession = Gs2RestSession;
71
+ var Gs2WebSocketSession = /** @class */ (function () {
72
+ function Gs2WebSocketSession(credential, region) {
73
+ this.client = null;
74
+ this.inflightRequest = {};
75
+ this.onOpenHandlers = [];
76
+ this.onErrorHandlers = [];
77
+ this.onCloseHandlers = [];
78
+ this.onNotificationHandlers = [];
79
+ this.credential = credential;
80
+ this.region = region;
81
+ this.projectToken = null;
82
+ this.expiresAt = null;
83
+ }
84
+ Gs2WebSocketSession.prototype.connect = function () {
85
+ return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
86
+ var service, url, data, response, result;
87
+ var _this = this;
88
+ return (0, tslib_1.__generator)(this, function (_a) {
89
+ switch (_a.label) {
90
+ case 0:
91
+ service = 'identifier';
92
+ url = exports.Gs2Constant.ENDPOINT_HOST
93
+ .replace('{service}', service)
94
+ .replace('{region}', this.region)
95
+ + '/projectToken/login';
96
+ if (!(this.credential instanceof BasicGs2Credential)) return [3 /*break*/, 2];
97
+ data = {
98
+ client_id: this.credential.clientId,
99
+ client_secret: this.credential.clientSecret,
100
+ };
101
+ return [4 /*yield*/, axios_1.default.post(url, data)];
102
+ case 1:
103
+ response = _a.sent();
104
+ result = new LoginResult(response.data);
105
+ this.projectToken = result.accessToken;
106
+ this.expiresAt = new Date().getTime() + result.expiresIn * 1000;
107
+ return [3 /*break*/, 3];
108
+ case 2:
109
+ if (this.credential instanceof ProjectTokenGs2Credential) {
110
+ this.projectToken = this.credential.projectToken;
111
+ }
112
+ _a.label = 3;
113
+ case 3:
114
+ if (typeof window === 'undefined') {
115
+ this.client = new NodeWebSocket(exports.Gs2Constant.WS_ENDPOINT_HOST.replace('{region}', this.region));
116
+ }
117
+ else {
118
+ this.client = new WebSocket(exports.Gs2Constant.WS_ENDPOINT_HOST.replace('{region}', this.region));
119
+ }
120
+ this.client.onopen = function (event) {
121
+ for (var i = 0; i < _this.onOpenHandlers.length; i++) {
122
+ _this.onOpenHandlers[i]();
123
+ }
124
+ };
125
+ this.client.onmessage = function (message) {
126
+ var payload = JSON.parse(message.data);
127
+ if (payload.type == 'notification') {
128
+ for (var i = 0; i < _this.onNotificationHandlers.length; i++) {
129
+ _this.onNotificationHandlers[i](payload.body);
130
+ }
131
+ }
132
+ else {
133
+ _this.inflightRequest[payload.requestId] = payload;
134
+ }
135
+ };
136
+ this.client.onerror = function (error) {
137
+ for (var i = 0; i < _this.onErrorHandlers.length; i++) {
138
+ _this.onErrorHandlers[i](error);
139
+ }
140
+ };
141
+ this.client.onclose = function () {
142
+ for (var i = 0; i < _this.onCloseHandlers.length; i++) {
143
+ _this.onCloseHandlers[i]();
144
+ }
145
+ };
146
+ if (!(typeof window === 'undefined')) return [3 /*break*/, 5];
147
+ return [4 /*yield*/, (0, async_wait_until_1.default)(function () { return _this.client == null || _this.client.readyState == NodeWebSocket.CLOSED || _this.client.readyState == NodeWebSocket.OPEN; })];
148
+ case 4:
149
+ _a.sent();
150
+ return [3 /*break*/, 7];
151
+ case 5: return [4 /*yield*/, (0, async_wait_until_1.default)(function () { return _this.client == null || _this.client.readyState == WebSocket.CLOSED || _this.client.readyState == WebSocket.OPEN; })];
152
+ case 6:
153
+ _a.sent();
154
+ _a.label = 7;
155
+ case 7: return [2 /*return*/];
156
+ }
157
+ });
158
+ });
159
+ };
160
+ Gs2WebSocketSession.prototype.send = function (service, component, func, payload) {
161
+ var _a;
162
+ return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
163
+ var requestId, result;
164
+ var _this = this;
165
+ return (0, tslib_1.__generator)(this, function (_b) {
166
+ switch (_b.label) {
167
+ case 0:
168
+ requestId = (0, uuid_1.v4)();
169
+ this.inflightRequest[requestId] = null;
170
+ (_a = this.client) === null || _a === void 0 ? void 0 : _a.send(JSON.stringify(Object.assign({}, payload, {
171
+ xGs2ClientId: this.credential.clientId,
172
+ xGs2ProjectToken: this.projectToken,
173
+ x_gs2: {
174
+ service: service,
175
+ component: component,
176
+ function: func,
177
+ contentType: "application/json",
178
+ requestId: requestId,
179
+ },
180
+ })));
181
+ return [4 /*yield*/, (0, async_wait_until_1.default)(function () { return _this.inflightRequest[requestId] != null; })];
182
+ case 1:
183
+ _b.sent();
184
+ result = this.inflightRequest[requestId];
185
+ delete this.inflightRequest[requestId];
186
+ if (result.status != 200) {
187
+ throw result.body;
188
+ }
189
+ return [2 /*return*/, result.body];
190
+ }
191
+ });
192
+ });
193
+ };
194
+ Gs2WebSocketSession.prototype.onOpen = function (func) {
195
+ this.onOpenHandlers.push(func);
196
+ };
197
+ Gs2WebSocketSession.prototype.onError = function (func) {
198
+ this.onErrorHandlers.push(func);
199
+ };
200
+ Gs2WebSocketSession.prototype.onClose = function (func) {
201
+ this.onCloseHandlers.push(func);
202
+ };
203
+ Gs2WebSocketSession.prototype.onNotification = function (func) {
204
+ this.onNotificationHandlers.push(func);
205
+ };
206
+ Gs2WebSocketSession.prototype.disconnect = function () {
207
+ return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
208
+ var _this = this;
209
+ return (0, tslib_1.__generator)(this, function (_a) {
210
+ switch (_a.label) {
211
+ case 0:
212
+ if (!(this.client != null)) return [3 /*break*/, 5];
213
+ this.client.close();
214
+ if (!(typeof window === 'undefined')) return [3 /*break*/, 2];
215
+ return [4 /*yield*/, (0, async_wait_until_1.default)(function () { return _this.client == null || _this.client.readyState == NodeWebSocket.CLOSED; })];
216
+ case 1:
217
+ _a.sent();
218
+ return [3 /*break*/, 4];
219
+ case 2: return [4 /*yield*/, (0, async_wait_until_1.default)(function () { return _this.client == null || _this.client.readyState == WebSocket.CLOSED; })];
220
+ case 3:
221
+ _a.sent();
222
+ _a.label = 4;
223
+ case 4:
224
+ this.client = null;
225
+ _a.label = 5;
226
+ case 5:
227
+ this.projectToken = null;
228
+ return [2 /*return*/];
229
+ }
230
+ });
231
+ });
232
+ };
233
+ return Gs2WebSocketSession;
234
+ }());
235
+ exports.Gs2WebSocketSession = Gs2WebSocketSession;
68
236
  var LoginResult = /** @class */ (function () {
69
237
  function LoginResult(data) {
70
238
  if (data) {
@@ -95,6 +263,7 @@ var ProjectTokenGs2Credential = /** @class */ (function () {
95
263
  exports.ProjectTokenGs2Credential = ProjectTokenGs2Credential;
96
264
  exports.Gs2Constant = {
97
265
  ENDPOINT_HOST: 'https://{service}.{region}.gen2.gs2io.com',
266
+ WS_ENDPOINT_HOST: 'wss://gateway-ws.{region}.gen2.gs2io.com',
98
267
  };
99
268
  exports.Region = {
100
269
  AP_NORTHEAST_1: 'ap-northeast-1',
package/core/model.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"model.js","sourceRoot":"","sources":["../src/core/model.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;;;AAIH,6DAA0B;AAE1B;IAKE,4BACI,QAAgB,EAChB,YAAoB;QAEtB,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;IACnC,CAAC;IACH,yBAAC;AAAD,CAAC,AAZD,IAYC;AAZY,gDAAkB;AAe/B;IAOE,wBAAY,UAA0B,EAAE,MAAc;QACpD,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;QAC7B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC;QACzB,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC;IACxB,CAAC;IAEM,gCAAO,GAAd;QAAA,iBA6BC;QA3BC,IAAM,OAAO,GAAG,YAAY,CAAC;QAC7B,IAAM,GAAG,GAAG,mBAAW,CAAC,aAAa;aAC5B,OAAO,CAAC,WAAW,EAAE,OAAO,CAAC;aAC7B,OAAO,CAAC,UAAU,EAAE,IAAI,CAAC,MAAM,CAAC;cACnC,qBAAqB,CAAC;QAE5B,IAAI,IAAI,CAAC,UAAU,YAAY,kBAAkB,EAAE;YACjD,IAAM,IAAI,GAAG;gBACX,SAAS,EAAG,IAAI,CAAC,UAAiC,CAAC,QAAQ;gBAC3D,aAAa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B;AAczB,QAAA,WAAW,GAAG;IACzB,aAAa,EAAE,2CAA2C;IAC1D,gBAAgB,EAAE,0CAA0C;CAC7D,CAAC;AAEW,QAAA,MAAM,GAAG;IACpB,cAAc,EAAE,gBAAgB;IAChC,SAAS,EAAE,WAAW;IACtB,SAAS,EAAE,WAAW;IACtB,cAAc,EAAE,gBAAgB;CACjC,CAAC"}
@@ -2,4 +2,5 @@ import * as model from './model';
2
2
  import * as request from './request';
3
3
  import * as result from './result';
4
4
  import Gs2GatewayRestClient from './rest';
5
- export { model, request, result, Gs2GatewayRestClient, };
5
+ import Gs2GatewayWebSocketClient from './websocket';
6
+ export { model, request, result, Gs2GatewayRestClient, Gs2GatewayWebSocketClient, };
package/gateway/index.js CHANGED
@@ -15,7 +15,7 @@ express or implied. See the License for the specific language governing
15
15
  permissions and limitations under the License.
16
16
  */
17
17
  Object.defineProperty(exports, "__esModule", { value: true });
18
- exports.Gs2GatewayRestClient = exports.result = exports.request = exports.model = void 0;
18
+ exports.Gs2GatewayWebSocketClient = exports.Gs2GatewayRestClient = exports.result = exports.request = exports.model = void 0;
19
19
  var tslib_1 = require("tslib");
20
20
  var model = (0, tslib_1.__importStar)(require("./model"));
21
21
  exports.model = model;
@@ -25,4 +25,6 @@ var result = (0, tslib_1.__importStar)(require("./result"));
25
25
  exports.result = result;
26
26
  var rest_1 = (0, tslib_1.__importDefault)(require("./rest"));
27
27
  exports.Gs2GatewayRestClient = rest_1.default;
28
+ var websocket_1 = (0, tslib_1.__importDefault)(require("./websocket"));
29
+ exports.Gs2GatewayWebSocketClient = websocket_1.default;
28
30
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/gateway/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;;;AAEH,0DAAiC;AAM7B,sBAAK;AALT,8DAAqC;AAMjC,0BAAO;AALX,4DAAmC;AAM/B,wBAAM;AALV,6DAA0C;AAMtC,+BANG,cAAoB,CAMH"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/gateway/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;;;AAEH,0DAAiC;AAO7B,sBAAK;AANT,8DAAqC;AAOjC,0BAAO;AANX,4DAAmC;AAO/B,wBAAM;AANV,6DAA0C;AAOtC,+BAPG,cAAoB,CAOH;AANxB,uEAAoD;AAOhD,oCAPG,mBAAyB,CAOH"}
@@ -0,0 +1,8 @@
1
+ import AbstractGs2WebSocketClient from '../core/AbstractGs2WebSocketClient';
2
+ import { Gs2WebSocketSession } from '../core/model';
3
+ import * as Request from './request';
4
+ import * as Result from './result';
5
+ export default class Gs2GatewayWebSocketClient extends AbstractGs2WebSocketClient {
6
+ constructor(session: Gs2WebSocketSession);
7
+ setUserId(request: Request.SetUserIdRequest): Promise<Result.SetUserIdResult>;
8
+ }
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ /*
3
+ Copyright 2016 Game Server Services, Inc. or its affiliates. All Rights
4
+ Reserved.
5
+
6
+ Licensed under the Apache License, Version 2.0 (the 'License').
7
+ You may not use this file except in compliance with the License.
8
+ A copy of the License is located at
9
+
10
+ http://www.apache.org/licenses/LICENSE-2.0
11
+
12
+ or in the 'license' file accompanying this file. This file is distributed
13
+ on an 'AS IS' BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
14
+ express or implied. See the License for the specific language governing
15
+ permissions and limitations under the License.
16
+ */
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ var tslib_1 = require("tslib");
19
+ var AbstractGs2WebSocketClient_1 = (0, tslib_1.__importDefault)(require("../core/AbstractGs2WebSocketClient"));
20
+ var Result = (0, tslib_1.__importStar)(require("./result"));
21
+ var Gs2GatewayWebSocketClient = /** @class */ (function (_super) {
22
+ (0, tslib_1.__extends)(Gs2GatewayWebSocketClient, _super);
23
+ function Gs2GatewayWebSocketClient(session) {
24
+ return _super.call(this, session) || this;
25
+ }
26
+ Gs2GatewayWebSocketClient.prototype.setUserId = function (request) {
27
+ var _a, _b, _c, _d;
28
+ return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
29
+ var result;
30
+ return (0, tslib_1.__generator)(this, function (_e) {
31
+ switch (_e.label) {
32
+ case 0: return [4 /*yield*/, this.session.send("gateway", "webSocketSession", "setUserId", {
33
+ 'contextStack': (_a = request.getContextStack()) !== null && _a !== void 0 ? _a : null,
34
+ 'namespaceName': (_b = request.getNamespaceName()) !== null && _b !== void 0 ? _b : null,
35
+ 'xGs2AccessToken': (_c = request.getAccessToken()) !== null && _c !== void 0 ? _c : null,
36
+ 'allowConcurrentAccess': (_d = request.getAllowConcurrentAccess()) !== null && _d !== void 0 ? _d : null,
37
+ })];
38
+ case 1:
39
+ result = _e.sent();
40
+ return [2 /*return*/, Result.SetUserIdResult.fromDict(result)];
41
+ }
42
+ });
43
+ });
44
+ };
45
+ return Gs2GatewayWebSocketClient;
46
+ }(AbstractGs2WebSocketClient_1.default));
47
+ exports.default = Gs2GatewayWebSocketClient;
48
+ //# sourceMappingURL=websocket.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"websocket.js","sourceRoot":"","sources":["../src/gateway/websocket.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;;AAEH,+GAA4E;AAG5E,4DAAmC;AAInC;IAAuD,0DAA0B;IAE7E,mCAAY,OAA4B;eACpC,kBAAM,OAAO,CAAC;IAClB,CAAC;IAEY,6CAAS,GAAtB,UAAuB,OAAiC;;;;;;4BACrC,qBAAM,IAAI,CAAC,OAAO,CAAC,IAAI,CAClC,SAAS,EACT,kBAAkB,EAClB,WAAW,EACX;4BACI,cAAc,EAAE,MAAA,OAAO,CAAC,eAAe,EAAE,mCAAI,IAAI;4BACjD,eAAe,EAAE,MAAA,OAAO,CAAC,gBAAgB,EAAE,mCAAI,IAAI;4BACnD,iBAAiB,EAAE,MAAA,OAAO,CAAC,cAAc,EAAE,mCAAI,IAAI;4BACnD,uBAAuB,EAAE,MAAA,OAAO,CAAC,wBAAwB,EAAE,mCAAI,IAAI;yBACtE,CACJ,EAAA;;wBAVK,MAAM,GAAG,SAUd;wBACD,sBAAO,MAAM,CAAC,eAAe,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAC;;;;KAClD;IACL,gCAAC;AAAD,CAAC,AApBD,CAAuD,oCAA0B,GAoBhF"}
package/package.json CHANGED
@@ -1,20 +1,25 @@
1
1
  {
2
2
  "name": "gs2",
3
- "version": "1.0.6",
3
+ "version": "1.3.2",
4
4
  "scripts": {
5
5
  "build": "tsc",
6
6
  "prepublishOnly": "npm run build"
7
7
  },
8
8
  "dependencies": {
9
- "axios": "^0.26.0"
9
+ "@types/websocket": "^1.0.5",
10
+ "async-wait-until": "^2.0.12",
11
+ "axios": "^0.26.0",
12
+ "uuid": "^8.3.2",
13
+ "websocket": "^1.0.34"
10
14
  },
11
15
  "devDependencies": {
16
+ "@types/jest": "^27.4.0",
12
17
  "@types/node": "^17.0.18",
13
- "typescript": "^4.5.5",
14
- "tslib": "^2.3.1",
18
+ "@types/uuid": "^8.3.4",
15
19
  "jest": "^27.5.1",
16
20
  "ts-jest": "^27.1.3",
17
- "@types/jest": "^27.4.0"
21
+ "tslib": "^2.3.1",
22
+ "typescript": "^4.5.5"
18
23
  },
19
24
  "types": "index.d.ts",
20
25
  "postcss": {
@@ -29,8 +34,7 @@
29
34
  ],
30
35
  "test": "jest",
31
36
  "jest": {
32
- "moduleNameMapper": {
33
- },
37
+ "moduleNameMapper": {},
34
38
  "moduleFileExtensions": [
35
39
  "js",
36
40
  "jsx",
@@ -0,0 +1,26 @@
1
+ /*
2
+ Copyright 2016 Game Server Services, Inc. or its affiliates. All Rights
3
+ Reserved.
4
+
5
+ Licensed under the Apache License, Version 2.0 (the "License").
6
+ You may not use this file except in compliance with the License.
7
+ A copy of the License is located at
8
+
9
+ http://www.apache.org/licenses/LICENSE-2.0
10
+
11
+ or in the "license" file accompanying this file. This file is distributed
12
+ on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13
+ express or implied. See the License for the specific language governing
14
+ permissions and limitations under the License.
15
+ */
16
+
17
+ import { Gs2WebSocketSession } from './model';
18
+
19
+ export default class {
20
+
21
+ public session: Gs2WebSocketSession;
22
+
23
+ constructor(session: Gs2WebSocketSession) {
24
+ this.session = session;
25
+ }
26
+ }
package/src/core/index.ts CHANGED
@@ -15,11 +15,12 @@ permissions and limitations under the License.
15
15
  */
16
16
 
17
17
  import AbstractGs2Client from './AbstractGs2RestClient';
18
- import { Gs2RestSession, BasicGs2Credential, ProjectTokenGs2Credential, Region } from './model';
18
+ import { Gs2RestSession, Gs2WebSocketSession, BasicGs2Credential, ProjectTokenGs2Credential, Region } from './model';
19
19
 
20
20
  export default {
21
21
  AbstractGs2Client,
22
22
  Gs2RestSession,
23
+ Gs2WebSocketSession,
23
24
  BasicGs2Credential,
24
25
  ProjectTokenGs2Credential,
25
26
  Region,
package/src/core/model.ts CHANGED
@@ -17,6 +17,9 @@ permissions and limitations under the License.
17
17
  import IGs2Credential from './interface/IGs2Credential';
18
18
  import IModel from './interface/IModel';
19
19
  import axios from 'axios';
20
+ import waitUntil from "async-wait-until";
21
+ import { v4 as uuid } from 'uuid';
22
+ const NodeWebSocket = require('ws');
20
23
 
21
24
  export class BasicGs2Credential implements IGs2Credential {
22
25
 
@@ -83,6 +86,158 @@ export class Gs2RestSession {
83
86
  }
84
87
  }
85
88
 
89
+
90
+ export class Gs2WebSocketSession {
91
+
92
+ public credential: IGs2Credential;
93
+ private client: WebSocket | null = null;
94
+ public region: string;
95
+ public projectToken: string | null;
96
+ public expiresAt: number | null;
97
+
98
+ private inflightRequest: any = {};
99
+
100
+ private onOpenHandlers: Function[] = [];
101
+ private onErrorHandlers: Function[] = [];
102
+ private onCloseHandlers: Function[] = [];
103
+
104
+ private onNotificationHandlers: Function[] = [];
105
+
106
+ constructor(credential: IGs2Credential, region: string) {
107
+ this.credential = credential;
108
+ this.region = region;
109
+ this.projectToken = null;
110
+ this.expiresAt = null;
111
+ }
112
+
113
+ public async connect(): Promise<any> {
114
+
115
+ const service = 'identifier';
116
+ const url = Gs2Constant.ENDPOINT_HOST
117
+ .replace('{service}', service)
118
+ .replace('{region}', this.region)
119
+ + '/projectToken/login';
120
+
121
+ if (this.credential instanceof BasicGs2Credential) {
122
+ const data = {
123
+ client_id: (this.credential as BasicGs2Credential).clientId,
124
+ client_secret: (this.credential as BasicGs2Credential).clientSecret,
125
+ };
126
+
127
+ const response = await axios.post(
128
+ url,
129
+ data,
130
+ )
131
+ const result = new LoginResult(response.data);
132
+ this.projectToken = result.accessToken;
133
+ this.expiresAt = new Date().getTime() + result.expiresIn * 1000;
134
+ } else if (this.credential instanceof ProjectTokenGs2Credential) {
135
+ this.projectToken = (this.credential as ProjectTokenGs2Credential).projectToken;
136
+ }
137
+
138
+ if (typeof window === 'undefined'){
139
+ this.client = new NodeWebSocket(Gs2Constant.WS_ENDPOINT_HOST.replace('{region}', this.region));
140
+ } else {
141
+ this.client = new WebSocket(Gs2Constant.WS_ENDPOINT_HOST.replace('{region}', this.region));
142
+ }
143
+ this.client!.onopen = (event: Event): any => {
144
+ for (let i=0; i<this.onOpenHandlers.length; i++) {
145
+ this.onOpenHandlers[i]();
146
+ }
147
+ };
148
+ this.client!.onmessage = (message: MessageEvent) => {
149
+ const payload = JSON.parse(message.data);
150
+ if (payload.type == 'notification') {
151
+ for (let i=0; i<this.onNotificationHandlers.length; i++) {
152
+ this.onNotificationHandlers[i](payload.body);
153
+ }
154
+ } else {
155
+ this.inflightRequest[payload.requestId] = payload;
156
+ }
157
+ };
158
+ this.client!.onerror = (error: Event) => {
159
+ for (let i = 0; i < this.onErrorHandlers.length; i++) {
160
+ this.onErrorHandlers[i](error);
161
+ }
162
+ };
163
+ this.client!.onclose = () => {
164
+ for (let i=0; i<this.onCloseHandlers.length; i++) {
165
+ this.onCloseHandlers[i]();
166
+ }
167
+ };
168
+ if (typeof window === 'undefined'){
169
+ await waitUntil(() => this.client == null || this.client.readyState == NodeWebSocket.CLOSED || this.client.readyState == NodeWebSocket.OPEN);
170
+ } else {
171
+ await waitUntil(() => this.client == null || this.client.readyState == WebSocket.CLOSED || this.client.readyState == WebSocket.OPEN);
172
+ }
173
+ }
174
+
175
+ public async send(
176
+ service: string,
177
+ component: string,
178
+ func: string,
179
+ payload: any,
180
+ ): Promise<any> {
181
+
182
+ const requestId = uuid();
183
+
184
+ this.inflightRequest[requestId] = null;
185
+ this.client?.send(JSON.stringify(Object.assign({}, payload, {
186
+ xGs2ClientId: this.credential.clientId,
187
+ xGs2ProjectToken: this.projectToken,
188
+ x_gs2: {
189
+ service: service,
190
+ component: component,
191
+ function: func,
192
+ contentType: "application/json",
193
+ requestId : requestId,
194
+ },
195
+ })));
196
+
197
+ await waitUntil(() => this.inflightRequest[requestId] != null);
198
+
199
+ const result = this.inflightRequest[requestId];
200
+ delete this.inflightRequest[requestId];
201
+ if (result.status != 200) {
202
+ throw result.body;
203
+ }
204
+ return result.body;
205
+ }
206
+
207
+ public onOpen(func: (ev: Event) => any) {
208
+ this.onOpenHandlers.push(func);
209
+ }
210
+
211
+ public onError(func: (ev: Event) => any) {
212
+ this.onErrorHandlers.push(func);
213
+ }
214
+
215
+ public onClose(func: (ev: CloseEvent) => any) {
216
+ this.onCloseHandlers.push(func);
217
+ }
218
+
219
+ public onNotification(func: (message: {
220
+ subject: string,
221
+ issuer: string,
222
+ payload: any,
223
+ }) => any) {
224
+ this.onNotificationHandlers.push(func);
225
+ }
226
+
227
+ public async disconnect(): Promise<any> {
228
+ if (this.client != null) {
229
+ this.client.close();
230
+ if (typeof window === 'undefined') {
231
+ await waitUntil(() => this.client == null || this.client.readyState == NodeWebSocket.CLOSED);
232
+ } else {
233
+ await waitUntil(() => this.client == null || this.client.readyState == WebSocket.CLOSED);
234
+ }
235
+ this.client = null;
236
+ }
237
+ this.projectToken = null;
238
+ }
239
+ }
240
+
86
241
  class LoginResult {
87
242
 
88
243
  public accessToken!: string;
@@ -129,6 +284,7 @@ export class ProjectTokenGs2Credential implements IGs2Credential {
129
284
 
130
285
  export const Gs2Constant = {
131
286
  ENDPOINT_HOST: 'https://{service}.{region}.gen2.gs2io.com',
287
+ WS_ENDPOINT_HOST: 'wss://gateway-ws.{region}.gen2.gs2io.com',
132
288
  };
133
289
 
134
290
  export const Region = {
@@ -18,10 +18,12 @@ import * as model from './model';
18
18
  import * as request from './request';
19
19
  import * as result from './result';
20
20
  import Gs2GatewayRestClient from './rest';
21
+ import Gs2GatewayWebSocketClient from './websocket';
21
22
 
22
23
  export {
23
24
  model,
24
25
  request,
25
26
  result,
26
27
  Gs2GatewayRestClient,
28
+ Gs2GatewayWebSocketClient,
27
29
  }
@@ -0,0 +1,44 @@
1
+ /*
2
+ Copyright 2016 Game Server Services, Inc. or its affiliates. All Rights
3
+ Reserved.
4
+
5
+ Licensed under the Apache License, Version 2.0 (the 'License').
6
+ You may not use this file except in compliance with the License.
7
+ A copy of the License is located at
8
+
9
+ http://www.apache.org/licenses/LICENSE-2.0
10
+
11
+ or in the 'license' file accompanying this file. This file is distributed
12
+ on an 'AS IS' BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13
+ express or implied. See the License for the specific language governing
14
+ permissions and limitations under the License.
15
+ */
16
+
17
+ import AbstractGs2WebSocketClient from '../core/AbstractGs2WebSocketClient';
18
+ import { Gs2Constant, Gs2WebSocketSession } from '../core/model';
19
+ import * as Request from './request';
20
+ import * as Result from './result';
21
+
22
+ import axios from 'axios';
23
+
24
+ export default class Gs2GatewayWebSocketClient extends AbstractGs2WebSocketClient {
25
+
26
+ constructor(session: Gs2WebSocketSession) {
27
+ super(session);
28
+ }
29
+
30
+ public async setUserId(request: Request.SetUserIdRequest): Promise<Result.SetUserIdResult> {
31
+ const result = await this.session.send(
32
+ "gateway",
33
+ "webSocketSession",
34
+ "setUserId",
35
+ {
36
+ 'contextStack': request.getContextStack() ?? null,
37
+ 'namespaceName': request.getNamespaceName() ?? null,
38
+ 'xGs2AccessToken': request.getAccessToken() ?? null,
39
+ 'allowConcurrentAccess': request.getAllowConcurrentAccess() ?? null,
40
+ },
41
+ );
42
+ return Result.SetUserIdResult.fromDict(result);
43
+ }
44
+ }