Parcourir la source

chore: update

wxyyxc1992 il y a 5 ans
Parent
révision
c52d17ae4e

+ 193
- 1
packages/cts-api/dist/cjs/WebSocketClient.js Voir le fichier

@@ -1,8 +1,200 @@
1 1
 "use strict";
2
+var __assign = (this && this.__assign) || function () {
3
+    __assign = Object.assign || function(t) {
4
+        for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+            s = arguments[i];
6
+            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+                t[p] = s[p];
8
+        }
9
+        return t;
10
+    };
11
+    return __assign.apply(this, arguments);
12
+};
13
+var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
+    return new (P || (P = Promise))(function (resolve, reject) {
15
+        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
16
+        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
17
+        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
18
+        step((generator = generator.apply(thisArg, _arguments || [])).next());
19
+    });
20
+};
21
+var __generator = (this && this.__generator) || function (thisArg, body) {
22
+    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
23
+    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
24
+    function verb(n) { return function (v) { return step([n, v]); }; }
25
+    function step(op) {
26
+        if (f) throw new TypeError("Generator is already executing.");
27
+        while (_) try {
28
+            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
29
+            if (y = 0, t) op = [op[0] & 2, t.value];
30
+            switch (op[0]) {
31
+                case 0: case 1: t = op; break;
32
+                case 4: _.label++; return { value: op[1], done: false };
33
+                case 5: _.label++; y = op[1]; op = [0]; continue;
34
+                case 7: op = _.ops.pop(); _.trys.pop(); continue;
35
+                default:
36
+                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
37
+                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
38
+                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
39
+                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
40
+                    if (t[2]) _.ops.pop();
41
+                    _.trys.pop(); continue;
42
+            }
43
+            op = body.call(thisArg, _);
44
+        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
45
+        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
46
+    }
47
+};
2 48
 Object.defineProperty(exports, "__esModule", { value: true });
49
+var client_1 = require("./client");
50
+var meta_1 = require("./meta");
51
+var meta = meta_1.getMetaData();
3 52
 var WebSocketClient = (function () {
4
-    function WebSocketClient() {
53
+    function WebSocketClient(wsUrl, token, sid, chatToken) {
54
+        this.status = {
55
+            created: false,
56
+            connected: false,
57
+            login: false
58
+        };
59
+        this.wsUrl = wsUrl;
60
+        this.token = token;
61
+        this.chatToken = chatToken;
62
+        this.sid = sid;
5 63
     }
64
+    WebSocketClient.prototype.init = function () {
65
+        return __awaiter(this, void 0, void 0, function () {
66
+            return __generator(this, function (_a) {
67
+                return [2, this.create()];
68
+            });
69
+        });
70
+    };
71
+    WebSocketClient.prototype.getStatus = function () {
72
+        return this.status;
73
+    };
74
+    WebSocketClient.prototype.create = function () {
75
+        return __awaiter(this, void 0, void 0, function () {
76
+            var url;
77
+            var _this = this;
78
+            return __generator(this, function (_a) {
79
+                url = this.wsUrl;
80
+                return [2, new Promise(function (resolve, reject) {
81
+                        if (!url) {
82
+                            throw new Error('websocket url is required.');
83
+                        }
84
+                        _this.client = client_1.getClient(url, {
85
+                            onOpen: function () { return __awaiter(_this, void 0, void 0, function () {
86
+                                var data;
87
+                                var _this = this;
88
+                                return __generator(this, function (_a) {
89
+                                    switch (_a.label) {
90
+                                        case 0:
91
+                                            try {
92
+                                                this.ping();
93
+                                                this.interval = setInterval(function () {
94
+                                                    _this.ping();
95
+                                                }, PING_INTERVAL);
96
+                                            }
97
+                                            catch (err) {
98
+                                                this.client.reconnect();
99
+                                            }
100
+                                            return [4, this.connect()];
101
+                                        case 1:
102
+                                            data = _a.sent();
103
+                                            return [4, this.authentication()];
104
+                                        case 2:
105
+                                            _a.sent();
106
+                                            resolve(data.value);
107
+                                            return [2];
108
+                                    }
109
+                                });
110
+                            }); },
111
+                            onError: function (err) {
112
+                                resolve(false);
113
+                            },
114
+                            onClose: function (CloseEvent) {
115
+                                clearInterval(_this.interval);
116
+                            }
117
+                        });
118
+                    })];
119
+            });
120
+        });
121
+    };
122
+    WebSocketClient.prototype.on = function (url, callback) {
123
+        if (!url) {
124
+            throw new Error('url is required.');
125
+        }
126
+        this.client.addMessageListener(url, callback);
127
+    };
128
+    WebSocketClient.prototype.off = function (url) {
129
+        this.client.removeMessageListener(url);
130
+    };
131
+    WebSocketClient.prototype.request = function (url, data) {
132
+        return __awaiter(this, void 0, void 0, function () {
133
+            var error;
134
+            return __generator(this, function (_a) {
135
+                if (this.client.readyState !== 1) {
136
+                    error = 'websocket connection not ready!';
137
+                    throw new Error(error);
138
+                }
139
+                try {
140
+                    return [2, this.client.asyncSend(url, data)];
141
+                }
142
+                catch (e) {
143
+                    console.error(e);
144
+                    throw e;
145
+                }
146
+                return [2];
147
+            });
148
+        });
149
+    };
150
+    WebSocketClient.prototype.connect = function () {
151
+        return __awaiter(this, void 0, void 0, function () {
152
+            var sid, data, id;
153
+            return __generator(this, function (_a) {
154
+                switch (_a.label) {
155
+                    case 0:
156
+                        sid = this.sid;
157
+                        if (sid) {
158
+                            this.client.setRequestProperty('sid', sid);
159
+                        }
160
+                        return [4, this.request('/v1/session/start', __assign({}, this.chatToken, meta))];
161
+                    case 1:
162
+                        data = _a.sent();
163
+                        if (data && data.value) {
164
+                            id = data.value;
165
+                            this.sid = id;
166
+                            this.client.setRequestProperty('sid', id);
167
+                        }
168
+                        return [2, data || true];
169
+                }
170
+            });
171
+        });
172
+    };
173
+    WebSocketClient.prototype.ping = function () {
174
+        return this.client.ping({});
175
+    };
176
+    WebSocketClient.prototype.authentication = function () {
177
+        return __awaiter(this, void 0, void 0, function () {
178
+            var tk, data;
179
+            return __generator(this, function (_a) {
180
+                tk = this.token;
181
+                data = this.request('/v1/session/bind/uid/by/token', { token: tk });
182
+                return [2, data || true];
183
+            });
184
+        });
185
+    };
186
+    WebSocketClient.prototype.onMessage = function (callback) {
187
+        this.on('/v1/message/listener', callback);
188
+    };
189
+    WebSocketClient.prototype.sendMessage = function (data) {
190
+        return this.request('/v1/send/message', data);
191
+    };
192
+    WebSocketClient.prototype.markServiced = function (data) {
193
+        return this.request('/v1/mark/message/serviced', data);
194
+    };
195
+    WebSocketClient.prototype.getHistoryMessage = function (data) {
196
+        return this.request('/v1/history/message', data);
197
+    };
6 198
     return WebSocketClient;
7 199
 }());
8 200
 exports.WebSocketClient = WebSocketClient;

+ 8
- 0
packages/cts-api/dist/cjs/client.js Voir le fichier

@@ -281,3 +281,11 @@ var Client = (function () {
281 281
     return Client;
282 282
 }());
283 283
 exports.Client = Client;
284
+var client;
285
+function getClient(url, callback) {
286
+    if (!client) {
287
+        client = new Client(url, callback);
288
+    }
289
+    return client;
290
+}
291
+exports.getClient = getClient;

+ 4
- 4
packages/cts-api/dist/index.js
Fichier diff supprimé car celui-ci est trop grand
Voir le fichier


+ 28
- 0
packages/cts-api/dist/types/WebSocketClient.d.ts Voir le fichier

@@ -1,2 +1,30 @@
1
+import { Client, WebSocketResp } from './client';
2
+export interface WebSocketClientStatus {
3
+    created: boolean;
4
+    connected: boolean;
5
+    login: boolean;
6
+}
7
+export declare type ListenerCallback = (data: WebSocketResp) => void;
1 8
 export declare class WebSocketClient {
9
+    client: Client;
10
+    interval: number;
11
+    wsUrl: string;
12
+    token: string;
13
+    sid: string;
14
+    chatToken: object;
15
+    status: WebSocketClientStatus;
16
+    constructor(wsUrl: string, token: string, sid: string, chatToken: object);
17
+    init(): Promise<{}>;
18
+    getStatus(): WebSocketClientStatus;
19
+    create(): Promise<{}>;
20
+    on(url: string, callback: (data: WebSocketResp) => void): void;
21
+    off(url: string): void;
22
+    request(url: string, data: object): Promise<WebSocketResp>;
23
+    connect(): Promise<WebSocketResp>;
24
+    ping(): Promise<WebSocketResp>;
25
+    authentication(): Promise<WebSocketResp>;
26
+    onMessage(callback: ListenerCallback): void;
27
+    sendMessage(data: object): Promise<WebSocketResp>;
28
+    markServiced(data: object): Promise<WebSocketResp>;
29
+    getHistoryMessage(data: object): Promise<WebSocketResp>;
2 30
 }

+ 10
- 6
packages/cts-api/dist/types/client.d.ts Voir le fichier

@@ -1,4 +1,7 @@
1 1
 import { ReadyStateCallback } from './types/callback';
2
+export interface WebSocketResp {
3
+    value: object | string;
4
+}
2 5
 declare class Client {
3 6
     private _maxPayload;
4 7
     private _enableLogger;
@@ -13,9 +16,9 @@ declare class Client {
13 16
     constructor(url: string, readyStateCallback: ReadyStateCallback);
14 17
     maxPayload: number;
15 18
     enableLogger: boolean;
16
-    ping(param: object): Promise<string>;
17
-    request(operator: string, param: object): Promise<string>;
18
-    addMessageListener(operator: string, listener: (data: string) => void): void;
19
+    ping(param: object): Promise<WebSocketResp>;
20
+    request(operator: string, param: object): Promise<WebSocketResp>;
21
+    addMessageListener(operator: string, listener: (data: WebSocketResp) => void): void;
19 22
     removeMessageListener(operator: string): void;
20 23
     readonly readyState: number;
21 24
     setRequestProperty(key: string, value: string): void;
@@ -23,8 +26,9 @@ declare class Client {
23 26
     setResponseProperty(key: string, value: string): void;
24 27
     getResponseProperty(key: string): string;
25 28
     private connect;
26
-    private reconnect;
29
+    reconnect(): void;
27 30
     private send;
28
-    private asyncSend;
31
+    asyncSend(operator: string, param: object): Promise<WebSocketResp>;
29 32
 }
30
-export { Client };
33
+declare function getClient(url: string, callback: ReadyStateCallback): Client;
34
+export { Client, getClient };

+ 3
- 2
packages/cts-api/example/index.ts Voir le fichier

@@ -1,5 +1,6 @@
1 1
 import { Client } from '../src/client';
2 2
 import { WebsocketError } from '../src/error';
3
+import { WebSocketResp } from '../dist/types/client';
3 4
 
4 5
 const url = 'ws://127.0.0.1:8081';
5 6
 const client = new Client(url, {
@@ -7,7 +8,7 @@ const client = new Client(url, {
7 8
     client
8 9
       .ping({})
9 10
       .then(
10
-        (res: string): void => {
11
+        (res: WebSocketResp): void => {
11 12
           console.log('ping sucessful:', res);
12 13
         }
13 14
       )
@@ -20,7 +21,7 @@ const client = new Client(url, {
20 21
     client
21 22
       .request('/v1/healthy', {})
22 23
       .then(
23
-        (res: string): void => {
24
+        (res: WebSocketResp): void => {
24 25
           console.log('request successful:', res);
25 26
         }
26 27
       )

+ 2
- 2
packages/cts-api/src/WebSocketClient.ts Voir le fichier

@@ -13,7 +13,7 @@ const meta = getMetaData();
13 13
 
14 14
 export class WebSocketClient {
15 15
   client: Client;
16
-  interval: NodeJS.Timeout;
16
+  interval: number;
17 17
 
18 18
   wsUrl: string;
19 19
   token: string;
@@ -55,7 +55,7 @@ export class WebSocketClient {
55 55
             this.ping();
56 56
             this.interval = setInterval(() => {
57 57
               this.ping();
58
-            }, PING_INTERVAL);
58
+            }, PING_INTERVAL) as any;
59 59
           } catch (err) {
60 60
             this.client.reconnect();
61 61
           }