@lightsoft/js-sdk 1.0.2 → 1.1.0

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.
package/dist/index.d.ts CHANGED
@@ -252,16 +252,17 @@ declare class Tour {
252
252
 
253
253
  type WebSocketState = 'CONNECTING' | 'OPEN' | 'CLOSING' | 'CLOSED';
254
254
  interface WebSocketConfig {
255
- timeout?: number;
255
+ heartbeat?: boolean;
256
+ heartbeatTime?: number;
256
257
  reconnect: boolean;
257
258
  reconnectTime?: number;
258
259
  maxReconnectNumber?: number;
259
260
  }
260
261
  declare function createWebSocket(url: string, config: WebSocketConfig | null, onOpen?: Function | null): WebSocket;
262
+ declare function getState(ws: WebSocket, type: WebSocketState): boolean;
261
263
  declare function useSendMessage(ws: WebSocket, data: any): void;
262
264
  declare function useWebSocketMessage(ws: WebSocket, callback: Function): void;
263
265
  declare function useWebSocketClose(ws: WebSocket, code?: number, message?: string): void;
264
- declare function getState(ws: WebSocket, type: WebSocketState): boolean;
265
266
 
266
267
  /**
267
268
  * 匹配中文字母数字
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
- import { _ as __assign, a as __awaiter, b as __generator } from './ws-DIZYbi31.js';
2
- export { c as createWebSocket, g as getState, u as useSendMessage, e as useWebSocketClose, d as useWebSocketMessage } from './ws-DIZYbi31.js';
1
+ import { _ as __assign, a as __awaiter, b as __generator } from './ws-EehpMWgu.js';
2
+ export { c as createWebSocket, g as getState, u as useSendMessage, e as useWebSocketClose, d as useWebSocketMessage } from './ws-EehpMWgu.js';
3
3
 
4
4
  var READY_STATE;
5
5
  (function (READY_STATE) {
package/dist/index.umd.js CHANGED
@@ -834,7 +834,8 @@
834
834
  }());
835
835
 
836
836
  var defaultWebSocketConfig = {
837
- timeout: 6000,
837
+ heartbeat: false,
838
+ heartbeatTime: 15000,
838
839
  reconnect: false,
839
840
  reconnectTime: 1000,
840
841
  maxReconnectNumber: 3,
@@ -845,6 +846,8 @@
845
846
  var init = function () {
846
847
  var ws = new WebSocket(url);
847
848
  ws.onopen = function () {
849
+ if (config === null || config === void 0 ? void 0 : config.heartbeat)
850
+ heartbeat(ws, config);
848
851
  onOpen === null || onOpen === void 0 ? void 0 : onOpen();
849
852
  };
850
853
  ws.onerror = function () {
@@ -860,6 +863,9 @@
860
863
  };
861
864
  return init();
862
865
  }
866
+ function getState(ws, type) {
867
+ return ws.readyState === WebSocket[type];
868
+ }
863
869
  function reconnectWebSocket() {
864
870
  var reconnectTimeout;
865
871
  var reconnectNumber = 0;
@@ -880,6 +886,27 @@
880
886
  };
881
887
  return open;
882
888
  }
889
+ function heartbeat(ws, config) {
890
+ var heartbeatTime = config.heartbeatTime;
891
+ var timer = null;
892
+ timer = setInterval(function () {
893
+ var isOpen = getState(ws, 'OPEN');
894
+ var isConnected = getState(ws, 'CONNECTING');
895
+ if (isOpen) {
896
+ useSendMessage(ws, {
897
+ type: 'heartbeat',
898
+ timestamp: Date.now()
899
+ });
900
+ }
901
+ else if (!isConnected) {
902
+ var open_1 = reconnectWebSocket();
903
+ open_1(ws, config, function () {
904
+ timer && clearInterval(timer);
905
+ heartbeat(ws, config);
906
+ });
907
+ }
908
+ }, heartbeatTime);
909
+ }
883
910
  function useSendMessage(ws, data) {
884
911
  var isOpen = getState(ws, 'OPEN');
885
912
  if (isOpen)
@@ -901,9 +928,6 @@
901
928
  message = message || 'connection closed by client';
902
929
  ws.close(code, message);
903
930
  }
904
- function getState(ws, type) {
905
- return ws.readyState === WebSocket[type];
906
- }
907
931
 
908
932
  /**
909
933
  * 匹配中文字母数字
@@ -1,13 +1,14 @@
1
1
  type WebSocketState = 'CONNECTING' | 'OPEN' | 'CLOSING' | 'CLOSED';
2
2
  interface WebSocketConfig {
3
- timeout?: number;
3
+ heartbeat?: boolean;
4
+ heartbeatTime?: number;
4
5
  reconnect: boolean;
5
6
  reconnectTime?: number;
6
7
  maxReconnectNumber?: number;
7
8
  }
8
9
  export declare function createWebSocket(url: string, config: WebSocketConfig | null, onOpen?: Function | null): WebSocket;
10
+ export declare function getState(ws: WebSocket, type: WebSocketState): boolean;
9
11
  export declare function useSendMessage(ws: WebSocket, data: any): void;
10
12
  export declare function useWebSocketMessage(ws: WebSocket, callback: Function): void;
11
13
  export declare function useWebSocketClose(ws: WebSocket, code?: number, message?: string): void;
12
- export declare function getState(ws: WebSocket, type: WebSocketState): boolean;
13
14
  export {};
@@ -0,0 +1,168 @@
1
+ /******************************************************************************
2
+ Copyright (c) Microsoft Corporation.
3
+
4
+ Permission to use, copy, modify, and/or distribute this software for any
5
+ purpose with or without fee is hereby granted.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
8
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
9
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
10
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
11
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
12
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
13
+ PERFORMANCE OF THIS SOFTWARE.
14
+ ***************************************************************************** */
15
+ /* global Reflect, Promise, SuppressedError, Symbol, Iterator */
16
+
17
+
18
+ var __assign = function() {
19
+ __assign = Object.assign || function __assign(t) {
20
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
21
+ s = arguments[i];
22
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
23
+ }
24
+ return t;
25
+ };
26
+ return __assign.apply(this, arguments);
27
+ };
28
+
29
+ function __awaiter(thisArg, _arguments, P, generator) {
30
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
31
+ return new (P || (P = Promise))(function (resolve, reject) {
32
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
33
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
34
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
35
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
36
+ });
37
+ }
38
+
39
+ function __generator(thisArg, body) {
40
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
41
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
42
+ function verb(n) { return function (v) { return step([n, v]); }; }
43
+ function step(op) {
44
+ if (f) throw new TypeError("Generator is already executing.");
45
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
46
+ 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;
47
+ if (y = 0, t) op = [op[0] & 2, t.value];
48
+ switch (op[0]) {
49
+ case 0: case 1: t = op; break;
50
+ case 4: _.label++; return { value: op[1], done: false };
51
+ case 5: _.label++; y = op[1]; op = [0]; continue;
52
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
53
+ default:
54
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
55
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
56
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
57
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
58
+ if (t[2]) _.ops.pop();
59
+ _.trys.pop(); continue;
60
+ }
61
+ op = body.call(thisArg, _);
62
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
63
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
64
+ }
65
+ }
66
+
67
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
68
+ var e = new Error(message);
69
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
70
+ };
71
+
72
+ var defaultWebSocketConfig = {
73
+ timeout: 6000,
74
+ heartbeat: false,
75
+ reconnect: false,
76
+ reconnectTime: 1000,
77
+ maxReconnectNumber: 3,
78
+ };
79
+ function createWebSocket(url, config, onOpen) {
80
+ config = __assign(__assign({}, defaultWebSocketConfig), config);
81
+ var open = reconnectWebSocket();
82
+ var init = function () {
83
+ var ws = new WebSocket(url);
84
+ ws.onopen = function () {
85
+ if (config === null || config === void 0 ? void 0 : config.heartbeat)
86
+ heartbeat(ws, config);
87
+ onOpen === null || onOpen === void 0 ? void 0 : onOpen();
88
+ };
89
+ ws.onerror = function () {
90
+ if ((config === null || config === void 0 ? void 0 : config.reconnect) && open) {
91
+ open(ws, config, init);
92
+ }
93
+ else {
94
+ open = null;
95
+ config = null;
96
+ }
97
+ };
98
+ return ws;
99
+ };
100
+ return init();
101
+ }
102
+ function getState(ws, type) {
103
+ return ws.readyState === WebSocket[type];
104
+ }
105
+ function reconnectWebSocket() {
106
+ var reconnectTimeout;
107
+ var reconnectNumber = 0;
108
+ var open = function (ws, config, callback) {
109
+ var reconnectTime = config.reconnectTime, _a = config.maxReconnectNumber, maxReconnectNumber = _a === void 0 ? 1 : _a;
110
+ var isOpen = getState(ws, 'OPEN');
111
+ if (isOpen || (reconnectTimeout && reconnectNumber >= maxReconnectNumber)) {
112
+ clearTimeout(reconnectTimeout);
113
+ reconnectTimeout = null;
114
+ reconnectNumber = null;
115
+ config.reconnect = false;
116
+ return;
117
+ }
118
+ reconnectTimeout = setTimeout(function () {
119
+ reconnectNumber += 1;
120
+ callback();
121
+ }, reconnectTime);
122
+ };
123
+ return open;
124
+ }
125
+ function heartbeat(ws, config) {
126
+ var _a = config.timeout, timeout = _a === void 0 ? 6000 : _a;
127
+ var timeoutTimer = null;
128
+ setInterval(function () {
129
+ var isOpen = getState(ws, 'OPEN');
130
+ if (isOpen) {
131
+ useSendMessage(ws, {
132
+ type: 'heartbeat',
133
+ timestamp: Date.now()
134
+ });
135
+ clearTimeout(timeoutTimer);
136
+ timeoutTimer = setTimeout(function () {
137
+ console.log('心跳超时,连接可能已断开');
138
+ var open = reconnectWebSocket();
139
+ open(ws, config, function () {
140
+ heartbeat(ws, config);
141
+ });
142
+ }, timeout);
143
+ }
144
+ }, timeout);
145
+ }
146
+ function useSendMessage(ws, data) {
147
+ var isOpen = getState(ws, 'OPEN');
148
+ if (isOpen)
149
+ ws.send(JSON.stringify(data));
150
+ }
151
+ function useWebSocketMessage(ws, callback) {
152
+ try {
153
+ ws.onmessage = function (e) {
154
+ var data = JSON.parse(e.data);
155
+ callback(data, e);
156
+ };
157
+ }
158
+ catch (error) {
159
+ console.error(error);
160
+ }
161
+ }
162
+ function useWebSocketClose(ws, code, message) {
163
+ code = code || 1000;
164
+ message = message || 'connection closed by client';
165
+ ws.close(code, message);
166
+ }
167
+
168
+ export { __assign as _, __awaiter as a, __generator as b, createWebSocket as c, useWebSocketMessage as d, useWebSocketClose as e, getState as g, useSendMessage as u };
@@ -0,0 +1,169 @@
1
+ /******************************************************************************
2
+ Copyright (c) Microsoft Corporation.
3
+
4
+ Permission to use, copy, modify, and/or distribute this software for any
5
+ purpose with or without fee is hereby granted.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
8
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
9
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
10
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
11
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
12
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
13
+ PERFORMANCE OF THIS SOFTWARE.
14
+ ***************************************************************************** */
15
+ /* global Reflect, Promise, SuppressedError, Symbol, Iterator */
16
+
17
+
18
+ var __assign = function() {
19
+ __assign = Object.assign || function __assign(t) {
20
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
21
+ s = arguments[i];
22
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
23
+ }
24
+ return t;
25
+ };
26
+ return __assign.apply(this, arguments);
27
+ };
28
+
29
+ function __awaiter(thisArg, _arguments, P, generator) {
30
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
31
+ return new (P || (P = Promise))(function (resolve, reject) {
32
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
33
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
34
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
35
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
36
+ });
37
+ }
38
+
39
+ function __generator(thisArg, body) {
40
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
41
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
42
+ function verb(n) { return function (v) { return step([n, v]); }; }
43
+ function step(op) {
44
+ if (f) throw new TypeError("Generator is already executing.");
45
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
46
+ 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;
47
+ if (y = 0, t) op = [op[0] & 2, t.value];
48
+ switch (op[0]) {
49
+ case 0: case 1: t = op; break;
50
+ case 4: _.label++; return { value: op[1], done: false };
51
+ case 5: _.label++; y = op[1]; op = [0]; continue;
52
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
53
+ default:
54
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
55
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
56
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
57
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
58
+ if (t[2]) _.ops.pop();
59
+ _.trys.pop(); continue;
60
+ }
61
+ op = body.call(thisArg, _);
62
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
63
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
64
+ }
65
+ }
66
+
67
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
68
+ var e = new Error(message);
69
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
70
+ };
71
+
72
+ var defaultWebSocketConfig = {
73
+ timeout: 6000,
74
+ heartbeat: false,
75
+ reconnect: false,
76
+ reconnectTime: 1000,
77
+ maxReconnectNumber: 3,
78
+ };
79
+ function createWebSocket(url, config, onOpen) {
80
+ config = __assign(__assign({}, defaultWebSocketConfig), config);
81
+ var open = reconnectWebSocket();
82
+ var init = function () {
83
+ var ws = new WebSocket(url);
84
+ ws.onopen = function () {
85
+ if (config === null || config === void 0 ? void 0 : config.heartbeat)
86
+ heartbeat(ws, config);
87
+ onOpen === null || onOpen === void 0 ? void 0 : onOpen();
88
+ };
89
+ ws.onerror = function () {
90
+ if ((config === null || config === void 0 ? void 0 : config.reconnect) && open) {
91
+ open(ws, config, init);
92
+ }
93
+ else {
94
+ open = null;
95
+ config = null;
96
+ }
97
+ };
98
+ return ws;
99
+ };
100
+ return init();
101
+ }
102
+ function getState(ws, type) {
103
+ return ws.readyState === WebSocket[type];
104
+ }
105
+ function reconnectWebSocket() {
106
+ var reconnectTimeout;
107
+ var reconnectNumber = 0;
108
+ var open = function (ws, config, callback) {
109
+ var reconnectTime = config.reconnectTime, _a = config.maxReconnectNumber, maxReconnectNumber = _a === void 0 ? 1 : _a;
110
+ var isOpen = getState(ws, 'OPEN');
111
+ if (isOpen || (reconnectTimeout && reconnectNumber >= maxReconnectNumber)) {
112
+ clearTimeout(reconnectTimeout);
113
+ reconnectTimeout = null;
114
+ reconnectNumber = null;
115
+ config.reconnect = false;
116
+ return;
117
+ }
118
+ reconnectTimeout = setTimeout(function () {
119
+ reconnectNumber += 1;
120
+ callback();
121
+ }, reconnectTime);
122
+ };
123
+ return open;
124
+ }
125
+ function heartbeat(ws, config) {
126
+ var _a = config.timeout, timeout = _a === void 0 ? 6000 : _a;
127
+ var timeoutTimer = null;
128
+ setInterval(function () {
129
+ var isOpen = getState(ws, 'OPEN');
130
+ if (isOpen) {
131
+ useSendMessage(ws, {
132
+ type: 'heartbeat',
133
+ timestamp: Date.now()
134
+ });
135
+ clearTimeout(timeoutTimer);
136
+ console.log('发送心跳包');
137
+ timeoutTimer = setTimeout(function () {
138
+ console.log('心跳超时,连接可能已断开');
139
+ var open = reconnectWebSocket();
140
+ open(ws, config, function () {
141
+ heartbeat(ws, config);
142
+ });
143
+ }, timeout);
144
+ }
145
+ }, timeout);
146
+ }
147
+ function useSendMessage(ws, data) {
148
+ var isOpen = getState(ws, 'OPEN');
149
+ if (isOpen)
150
+ ws.send(JSON.stringify(data));
151
+ }
152
+ function useWebSocketMessage(ws, callback) {
153
+ try {
154
+ ws.onmessage = function (e) {
155
+ var data = JSON.parse(e.data);
156
+ callback(data, e);
157
+ };
158
+ }
159
+ catch (error) {
160
+ console.error(error);
161
+ }
162
+ }
163
+ function useWebSocketClose(ws, code, message) {
164
+ code = code || 1000;
165
+ message = message || 'connection closed by client';
166
+ ws.close(code, message);
167
+ }
168
+
169
+ export { __assign as _, __awaiter as a, __generator as b, createWebSocket as c, useWebSocketMessage as d, useWebSocketClose as e, getState as g, useSendMessage as u };
@@ -0,0 +1,167 @@
1
+ /******************************************************************************
2
+ Copyright (c) Microsoft Corporation.
3
+
4
+ Permission to use, copy, modify, and/or distribute this software for any
5
+ purpose with or without fee is hereby granted.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
8
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
9
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
10
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
11
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
12
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
13
+ PERFORMANCE OF THIS SOFTWARE.
14
+ ***************************************************************************** */
15
+ /* global Reflect, Promise, SuppressedError, Symbol, Iterator */
16
+
17
+
18
+ var __assign = function() {
19
+ __assign = Object.assign || function __assign(t) {
20
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
21
+ s = arguments[i];
22
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
23
+ }
24
+ return t;
25
+ };
26
+ return __assign.apply(this, arguments);
27
+ };
28
+
29
+ function __awaiter(thisArg, _arguments, P, generator) {
30
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
31
+ return new (P || (P = Promise))(function (resolve, reject) {
32
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
33
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
34
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
35
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
36
+ });
37
+ }
38
+
39
+ function __generator(thisArg, body) {
40
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
41
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
42
+ function verb(n) { return function (v) { return step([n, v]); }; }
43
+ function step(op) {
44
+ if (f) throw new TypeError("Generator is already executing.");
45
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
46
+ 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;
47
+ if (y = 0, t) op = [op[0] & 2, t.value];
48
+ switch (op[0]) {
49
+ case 0: case 1: t = op; break;
50
+ case 4: _.label++; return { value: op[1], done: false };
51
+ case 5: _.label++; y = op[1]; op = [0]; continue;
52
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
53
+ default:
54
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
55
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
56
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
57
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
58
+ if (t[2]) _.ops.pop();
59
+ _.trys.pop(); continue;
60
+ }
61
+ op = body.call(thisArg, _);
62
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
63
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
64
+ }
65
+ }
66
+
67
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
68
+ var e = new Error(message);
69
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
70
+ };
71
+
72
+ var defaultWebSocketConfig = {
73
+ timeout: 6000,
74
+ heartbeat: false,
75
+ reconnect: false,
76
+ reconnectTime: 1000,
77
+ maxReconnectNumber: 3,
78
+ };
79
+ function createWebSocket(url, config, onOpen) {
80
+ config = __assign(__assign({}, defaultWebSocketConfig), config);
81
+ var open = reconnectWebSocket();
82
+ var init = function () {
83
+ var ws = new WebSocket(url);
84
+ ws.onopen = function () {
85
+ if (config === null || config === void 0 ? void 0 : config.heartbeat)
86
+ heartbeat(ws, config);
87
+ onOpen === null || onOpen === void 0 ? void 0 : onOpen();
88
+ };
89
+ ws.onerror = function () {
90
+ if ((config === null || config === void 0 ? void 0 : config.reconnect) && open) {
91
+ open(ws, config, init);
92
+ }
93
+ else {
94
+ open = null;
95
+ config = null;
96
+ }
97
+ };
98
+ return ws;
99
+ };
100
+ return init();
101
+ }
102
+ function getState(ws, type) {
103
+ return ws.readyState === WebSocket[type];
104
+ }
105
+ function reconnectWebSocket() {
106
+ var reconnectTimeout;
107
+ var reconnectNumber = 0;
108
+ var open = function (ws, config, callback) {
109
+ var reconnectTime = config.reconnectTime, _a = config.maxReconnectNumber, maxReconnectNumber = _a === void 0 ? 1 : _a;
110
+ var isOpen = getState(ws, 'OPEN');
111
+ if (isOpen || (reconnectTimeout && reconnectNumber >= maxReconnectNumber)) {
112
+ clearTimeout(reconnectTimeout);
113
+ reconnectTimeout = null;
114
+ reconnectNumber = null;
115
+ config.reconnect = false;
116
+ return;
117
+ }
118
+ reconnectTimeout = setTimeout(function () {
119
+ reconnectNumber += 1;
120
+ callback();
121
+ }, reconnectTime);
122
+ };
123
+ return open;
124
+ }
125
+ function heartbeat(ws, config) {
126
+ var _a = config.timeout, timeout = _a === void 0 ? 6000 : _a;
127
+ var timeoutTimer = null;
128
+ setInterval(function () {
129
+ var isOpen = getState(ws, 'OPEN');
130
+ if (isOpen) {
131
+ useSendMessage(ws, {
132
+ type: 'heartbeat',
133
+ timestamp: Date.now()
134
+ });
135
+ clearTimeout(timeoutTimer);
136
+ timeoutTimer = setTimeout(function () {
137
+ var open = reconnectWebSocket();
138
+ open(ws, config, function () {
139
+ heartbeat(ws, config);
140
+ });
141
+ }, timeout);
142
+ }
143
+ }, timeout);
144
+ }
145
+ function useSendMessage(ws, data) {
146
+ var isOpen = getState(ws, 'OPEN');
147
+ if (isOpen)
148
+ ws.send(JSON.stringify(data));
149
+ }
150
+ function useWebSocketMessage(ws, callback) {
151
+ try {
152
+ ws.onmessage = function (e) {
153
+ var data = JSON.parse(e.data);
154
+ callback(data, e);
155
+ };
156
+ }
157
+ catch (error) {
158
+ console.error(error);
159
+ }
160
+ }
161
+ function useWebSocketClose(ws, code, message) {
162
+ code = code || 1000;
163
+ message = message || 'connection closed by client';
164
+ ws.close(code, message);
165
+ }
166
+
167
+ export { __assign as _, __awaiter as a, __generator as b, createWebSocket as c, useWebSocketMessage as d, useWebSocketClose as e, getState as g, useSendMessage as u };
@@ -0,0 +1,169 @@
1
+ /******************************************************************************
2
+ Copyright (c) Microsoft Corporation.
3
+
4
+ Permission to use, copy, modify, and/or distribute this software for any
5
+ purpose with or without fee is hereby granted.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
8
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
9
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
10
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
11
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
12
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
13
+ PERFORMANCE OF THIS SOFTWARE.
14
+ ***************************************************************************** */
15
+ /* global Reflect, Promise, SuppressedError, Symbol, Iterator */
16
+
17
+
18
+ var __assign = function() {
19
+ __assign = Object.assign || function __assign(t) {
20
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
21
+ s = arguments[i];
22
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
23
+ }
24
+ return t;
25
+ };
26
+ return __assign.apply(this, arguments);
27
+ };
28
+
29
+ function __awaiter(thisArg, _arguments, P, generator) {
30
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
31
+ return new (P || (P = Promise))(function (resolve, reject) {
32
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
33
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
34
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
35
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
36
+ });
37
+ }
38
+
39
+ function __generator(thisArg, body) {
40
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
41
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
42
+ function verb(n) { return function (v) { return step([n, v]); }; }
43
+ function step(op) {
44
+ if (f) throw new TypeError("Generator is already executing.");
45
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
46
+ 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;
47
+ if (y = 0, t) op = [op[0] & 2, t.value];
48
+ switch (op[0]) {
49
+ case 0: case 1: t = op; break;
50
+ case 4: _.label++; return { value: op[1], done: false };
51
+ case 5: _.label++; y = op[1]; op = [0]; continue;
52
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
53
+ default:
54
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
55
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
56
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
57
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
58
+ if (t[2]) _.ops.pop();
59
+ _.trys.pop(); continue;
60
+ }
61
+ op = body.call(thisArg, _);
62
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
63
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
64
+ }
65
+ }
66
+
67
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
68
+ var e = new Error(message);
69
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
70
+ };
71
+
72
+ var defaultWebSocketConfig = {
73
+ timeout: 6000,
74
+ heartbeat: false,
75
+ reconnect: false,
76
+ reconnectTime: 1000,
77
+ maxReconnectNumber: 3,
78
+ };
79
+ function createWebSocket(url, config, onOpen) {
80
+ config = __assign(__assign({}, defaultWebSocketConfig), config);
81
+ var open = reconnectWebSocket();
82
+ var init = function () {
83
+ var ws = new WebSocket(url);
84
+ ws.onopen = function () {
85
+ if (config === null || config === void 0 ? void 0 : config.heartbeat)
86
+ heartbeat(ws, config);
87
+ onOpen === null || onOpen === void 0 ? void 0 : onOpen();
88
+ };
89
+ ws.onerror = function () {
90
+ if ((config === null || config === void 0 ? void 0 : config.reconnect) && open) {
91
+ open(ws, config, init);
92
+ }
93
+ else {
94
+ open = null;
95
+ config = null;
96
+ }
97
+ };
98
+ return ws;
99
+ };
100
+ return init();
101
+ }
102
+ function getState(ws, type) {
103
+ return ws.readyState === WebSocket[type];
104
+ }
105
+ function reconnectWebSocket() {
106
+ var reconnectTimeout;
107
+ var reconnectNumber = 0;
108
+ var open = function (ws, config, callback) {
109
+ var reconnectTime = config.reconnectTime, _a = config.maxReconnectNumber, maxReconnectNumber = _a === void 0 ? 1 : _a;
110
+ var isOpen = getState(ws, 'OPEN');
111
+ if (isOpen || (reconnectTimeout && reconnectNumber >= maxReconnectNumber)) {
112
+ clearTimeout(reconnectTimeout);
113
+ reconnectTimeout = null;
114
+ reconnectNumber = null;
115
+ config.reconnect = false;
116
+ return;
117
+ }
118
+ reconnectTimeout = setTimeout(function () {
119
+ reconnectNumber += 1;
120
+ callback();
121
+ }, reconnectTime);
122
+ };
123
+ return open;
124
+ }
125
+ function heartbeat(ws, config) {
126
+ var _a = config.timeout, timeout = _a === void 0 ? 6000 : _a;
127
+ var timeoutTimer = null;
128
+ setInterval(function () {
129
+ var isOpen = getState(ws, 'OPEN');
130
+ if (isOpen) {
131
+ useSendMessage(ws, {
132
+ type: 'heartbeat',
133
+ timestamp: Date.now()
134
+ });
135
+ clearTimeout(timeoutTimer);
136
+ console.log('发送心跳包');
137
+ timeoutTimer = setTimeout(function () {
138
+ console.log('心跳超时,连接可能已断开');
139
+ var open = reconnectWebSocket();
140
+ open(ws, config, function () {
141
+ heartbeat(ws, config);
142
+ });
143
+ }, 3000);
144
+ }
145
+ }, timeout);
146
+ }
147
+ function useSendMessage(ws, data) {
148
+ var isOpen = getState(ws, 'OPEN');
149
+ if (isOpen)
150
+ ws.send(JSON.stringify(data));
151
+ }
152
+ function useWebSocketMessage(ws, callback) {
153
+ try {
154
+ ws.onmessage = function (e) {
155
+ var data = JSON.parse(e.data);
156
+ callback(data, e);
157
+ };
158
+ }
159
+ catch (error) {
160
+ console.error(error);
161
+ }
162
+ }
163
+ function useWebSocketClose(ws, code, message) {
164
+ code = code || 1000;
165
+ message = message || 'connection closed by client';
166
+ ws.close(code, message);
167
+ }
168
+
169
+ export { __assign as _, __awaiter as a, __generator as b, createWebSocket as c, useWebSocketMessage as d, useWebSocketClose as e, getState as g, useSendMessage as u };
@@ -0,0 +1,168 @@
1
+ /******************************************************************************
2
+ Copyright (c) Microsoft Corporation.
3
+
4
+ Permission to use, copy, modify, and/or distribute this software for any
5
+ purpose with or without fee is hereby granted.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
8
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
9
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
10
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
11
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
12
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
13
+ PERFORMANCE OF THIS SOFTWARE.
14
+ ***************************************************************************** */
15
+ /* global Reflect, Promise, SuppressedError, Symbol, Iterator */
16
+
17
+
18
+ var __assign = function() {
19
+ __assign = Object.assign || function __assign(t) {
20
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
21
+ s = arguments[i];
22
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
23
+ }
24
+ return t;
25
+ };
26
+ return __assign.apply(this, arguments);
27
+ };
28
+
29
+ function __awaiter(thisArg, _arguments, P, generator) {
30
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
31
+ return new (P || (P = Promise))(function (resolve, reject) {
32
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
33
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
34
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
35
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
36
+ });
37
+ }
38
+
39
+ function __generator(thisArg, body) {
40
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
41
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
42
+ function verb(n) { return function (v) { return step([n, v]); }; }
43
+ function step(op) {
44
+ if (f) throw new TypeError("Generator is already executing.");
45
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
46
+ 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;
47
+ if (y = 0, t) op = [op[0] & 2, t.value];
48
+ switch (op[0]) {
49
+ case 0: case 1: t = op; break;
50
+ case 4: _.label++; return { value: op[1], done: false };
51
+ case 5: _.label++; y = op[1]; op = [0]; continue;
52
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
53
+ default:
54
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
55
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
56
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
57
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
58
+ if (t[2]) _.ops.pop();
59
+ _.trys.pop(); continue;
60
+ }
61
+ op = body.call(thisArg, _);
62
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
63
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
64
+ }
65
+ }
66
+
67
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
68
+ var e = new Error(message);
69
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
70
+ };
71
+
72
+ var defaultWebSocketConfig = {
73
+ heartbeat: false,
74
+ heartbeatTime: 15000,
75
+ reconnect: false,
76
+ reconnectTime: 1000,
77
+ maxReconnectNumber: 3,
78
+ };
79
+ function createWebSocket(url, config, onOpen) {
80
+ config = __assign(__assign({}, defaultWebSocketConfig), config);
81
+ var open = reconnectWebSocket();
82
+ var init = function () {
83
+ var ws = new WebSocket(url);
84
+ ws.onopen = function () {
85
+ if (config === null || config === void 0 ? void 0 : config.heartbeat)
86
+ heartbeat(ws, config);
87
+ onOpen === null || onOpen === void 0 ? void 0 : onOpen();
88
+ };
89
+ ws.onerror = function () {
90
+ if ((config === null || config === void 0 ? void 0 : config.reconnect) && open) {
91
+ open(ws, config, init);
92
+ }
93
+ else {
94
+ open = null;
95
+ config = null;
96
+ }
97
+ };
98
+ return ws;
99
+ };
100
+ return init();
101
+ }
102
+ function getState(ws, type) {
103
+ return ws.readyState === WebSocket[type];
104
+ }
105
+ function reconnectWebSocket() {
106
+ var reconnectTimeout;
107
+ var reconnectNumber = 0;
108
+ var open = function (ws, config, callback) {
109
+ var reconnectTime = config.reconnectTime, _a = config.maxReconnectNumber, maxReconnectNumber = _a === void 0 ? 1 : _a;
110
+ var isOpen = getState(ws, 'OPEN');
111
+ if (isOpen || (reconnectTimeout && reconnectNumber >= maxReconnectNumber)) {
112
+ clearTimeout(reconnectTimeout);
113
+ reconnectTimeout = null;
114
+ reconnectNumber = null;
115
+ config.reconnect = false;
116
+ return;
117
+ }
118
+ reconnectTimeout = setTimeout(function () {
119
+ reconnectNumber += 1;
120
+ callback();
121
+ }, reconnectTime);
122
+ };
123
+ return open;
124
+ }
125
+ function heartbeat(ws, config) {
126
+ var heartbeatTime = config.heartbeatTime;
127
+ var timer = null;
128
+ timer = setInterval(function () {
129
+ var isOpen = getState(ws, 'OPEN');
130
+ var isConnected = getState(ws, 'CONNECTING');
131
+ if (isOpen) {
132
+ useSendMessage(ws, {
133
+ type: 'heartbeat',
134
+ timestamp: Date.now()
135
+ });
136
+ }
137
+ else if (!isConnected) {
138
+ var open_1 = reconnectWebSocket();
139
+ open_1(ws, config, function () {
140
+ timer && clearInterval(timer);
141
+ heartbeat(ws, config);
142
+ });
143
+ }
144
+ }, heartbeatTime);
145
+ }
146
+ function useSendMessage(ws, data) {
147
+ var isOpen = getState(ws, 'OPEN');
148
+ if (isOpen)
149
+ ws.send(JSON.stringify(data));
150
+ }
151
+ function useWebSocketMessage(ws, callback) {
152
+ try {
153
+ ws.onmessage = function (e) {
154
+ var data = JSON.parse(e.data);
155
+ callback(data, e);
156
+ };
157
+ }
158
+ catch (error) {
159
+ console.error(error);
160
+ }
161
+ }
162
+ function useWebSocketClose(ws, code, message) {
163
+ code = code || 1000;
164
+ message = message || 'connection closed by client';
165
+ ws.close(code, message);
166
+ }
167
+
168
+ export { __assign as _, __awaiter as a, __generator as b, createWebSocket as c, useWebSocketMessage as d, useWebSocketClose as e, getState as g, useSendMessage as u };
@@ -0,0 +1,166 @@
1
+ /******************************************************************************
2
+ Copyright (c) Microsoft Corporation.
3
+
4
+ Permission to use, copy, modify, and/or distribute this software for any
5
+ purpose with or without fee is hereby granted.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
8
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
9
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
10
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
11
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
12
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
13
+ PERFORMANCE OF THIS SOFTWARE.
14
+ ***************************************************************************** */
15
+ /* global Reflect, Promise, SuppressedError, Symbol, Iterator */
16
+
17
+
18
+ var __assign = function() {
19
+ __assign = Object.assign || function __assign(t) {
20
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
21
+ s = arguments[i];
22
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
23
+ }
24
+ return t;
25
+ };
26
+ return __assign.apply(this, arguments);
27
+ };
28
+
29
+ function __awaiter(thisArg, _arguments, P, generator) {
30
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
31
+ return new (P || (P = Promise))(function (resolve, reject) {
32
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
33
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
34
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
35
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
36
+ });
37
+ }
38
+
39
+ function __generator(thisArg, body) {
40
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
41
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
42
+ function verb(n) { return function (v) { return step([n, v]); }; }
43
+ function step(op) {
44
+ if (f) throw new TypeError("Generator is already executing.");
45
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
46
+ 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;
47
+ if (y = 0, t) op = [op[0] & 2, t.value];
48
+ switch (op[0]) {
49
+ case 0: case 1: t = op; break;
50
+ case 4: _.label++; return { value: op[1], done: false };
51
+ case 5: _.label++; y = op[1]; op = [0]; continue;
52
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
53
+ default:
54
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
55
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
56
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
57
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
58
+ if (t[2]) _.ops.pop();
59
+ _.trys.pop(); continue;
60
+ }
61
+ op = body.call(thisArg, _);
62
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
63
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
64
+ }
65
+ }
66
+
67
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
68
+ var e = new Error(message);
69
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
70
+ };
71
+
72
+ var defaultWebSocketConfig = {
73
+ timeout: 6000,
74
+ heartbeat: false,
75
+ reconnect: false,
76
+ reconnectTime: 1000,
77
+ maxReconnectNumber: 3,
78
+ };
79
+ function createWebSocket(url, config, onOpen) {
80
+ config = __assign(__assign({}, defaultWebSocketConfig), config);
81
+ var open = reconnectWebSocket();
82
+ var init = function () {
83
+ var ws = new WebSocket(url);
84
+ ws.onopen = function () {
85
+ if (config === null || config === void 0 ? void 0 : config.heartbeat)
86
+ heartbeat(ws, config);
87
+ onOpen === null || onOpen === void 0 ? void 0 : onOpen();
88
+ };
89
+ ws.onerror = function () {
90
+ if ((config === null || config === void 0 ? void 0 : config.reconnect) && open) {
91
+ open(ws, config, init);
92
+ }
93
+ else {
94
+ open = null;
95
+ config = null;
96
+ }
97
+ };
98
+ return ws;
99
+ };
100
+ return init();
101
+ }
102
+ function getState(ws, type) {
103
+ return ws.readyState === WebSocket[type];
104
+ }
105
+ function reconnectWebSocket() {
106
+ var reconnectTimeout;
107
+ var reconnectNumber = 0;
108
+ var open = function (ws, config, callback) {
109
+ var reconnectTime = config.reconnectTime, _a = config.maxReconnectNumber, maxReconnectNumber = _a === void 0 ? 1 : _a;
110
+ var isOpen = getState(ws, 'OPEN');
111
+ if (isOpen || (reconnectTimeout && reconnectNumber >= maxReconnectNumber)) {
112
+ clearTimeout(reconnectTimeout);
113
+ reconnectTimeout = null;
114
+ reconnectNumber = null;
115
+ config.reconnect = false;
116
+ return;
117
+ }
118
+ reconnectTimeout = setTimeout(function () {
119
+ reconnectNumber += 1;
120
+ callback();
121
+ }, reconnectTime);
122
+ };
123
+ return open;
124
+ }
125
+ function heartbeat(ws, config) {
126
+ var _a = config.timeout, timeout = _a === void 0 ? 6000 : _a;
127
+ setInterval(function () {
128
+ var isOpen = getState(ws, 'OPEN');
129
+ var isConnected = getState(ws, 'CONNECTING');
130
+ if (isOpen) {
131
+ useSendMessage(ws, {
132
+ type: 'heartbeat',
133
+ timestamp: Date.now()
134
+ });
135
+ }
136
+ else if (!isConnected) {
137
+ var open_1 = reconnectWebSocket();
138
+ open_1(ws, config, function () {
139
+ heartbeat(ws, config);
140
+ });
141
+ }
142
+ }, timeout);
143
+ }
144
+ function useSendMessage(ws, data) {
145
+ var isOpen = getState(ws, 'OPEN');
146
+ if (isOpen)
147
+ ws.send(JSON.stringify(data));
148
+ }
149
+ function useWebSocketMessage(ws, callback) {
150
+ try {
151
+ ws.onmessage = function (e) {
152
+ var data = JSON.parse(e.data);
153
+ callback(data, e);
154
+ };
155
+ }
156
+ catch (error) {
157
+ console.error(error);
158
+ }
159
+ }
160
+ function useWebSocketClose(ws, code, message) {
161
+ code = code || 1000;
162
+ message = message || 'connection closed by client';
163
+ ws.close(code, message);
164
+ }
165
+
166
+ export { __assign as _, __awaiter as a, __generator as b, createWebSocket as c, useWebSocketMessage as d, useWebSocketClose as e, getState as g, useSendMessage as u };
package/dist/ws.d.ts CHANGED
@@ -1,14 +1,15 @@
1
1
  type WebSocketState = 'CONNECTING' | 'OPEN' | 'CLOSING' | 'CLOSED';
2
2
  interface WebSocketConfig {
3
- timeout?: number;
3
+ heartbeat?: boolean;
4
+ heartbeatTime?: number;
4
5
  reconnect: boolean;
5
6
  reconnectTime?: number;
6
7
  maxReconnectNumber?: number;
7
8
  }
8
9
  declare function createWebSocket(url: string, config: WebSocketConfig | null, onOpen?: Function | null): WebSocket;
10
+ declare function getState(ws: WebSocket, type: WebSocketState): boolean;
9
11
  declare function useSendMessage(ws: WebSocket, data: any): void;
10
12
  declare function useWebSocketMessage(ws: WebSocket, callback: Function): void;
11
13
  declare function useWebSocketClose(ws: WebSocket, code?: number, message?: string): void;
12
- declare function getState(ws: WebSocket, type: WebSocketState): boolean;
13
14
 
14
15
  export { createWebSocket, getState, useSendMessage, useWebSocketClose, useWebSocketMessage };
package/dist/ws.js CHANGED
@@ -1 +1 @@
1
- export { c as createWebSocket, g as getState, u as useSendMessage, e as useWebSocketClose, d as useWebSocketMessage } from './ws-DIZYbi31.js';
1
+ export { c as createWebSocket, g as getState, u as useSendMessage, e as useWebSocketClose, d as useWebSocketMessage } from './ws-EehpMWgu.js';
package/dist/ws.umd.js CHANGED
@@ -38,7 +38,8 @@
38
38
  };
39
39
 
40
40
  var defaultWebSocketConfig = {
41
- timeout: 6000,
41
+ heartbeat: false,
42
+ heartbeatTime: 15000,
42
43
  reconnect: false,
43
44
  reconnectTime: 1000,
44
45
  maxReconnectNumber: 3,
@@ -49,6 +50,8 @@
49
50
  var init = function () {
50
51
  var ws = new WebSocket(url);
51
52
  ws.onopen = function () {
53
+ if (config === null || config === void 0 ? void 0 : config.heartbeat)
54
+ heartbeat(ws, config);
52
55
  onOpen === null || onOpen === void 0 ? void 0 : onOpen();
53
56
  };
54
57
  ws.onerror = function () {
@@ -64,6 +67,9 @@
64
67
  };
65
68
  return init();
66
69
  }
70
+ function getState(ws, type) {
71
+ return ws.readyState === WebSocket[type];
72
+ }
67
73
  function reconnectWebSocket() {
68
74
  var reconnectTimeout;
69
75
  var reconnectNumber = 0;
@@ -84,6 +90,27 @@
84
90
  };
85
91
  return open;
86
92
  }
93
+ function heartbeat(ws, config) {
94
+ var heartbeatTime = config.heartbeatTime;
95
+ var timer = null;
96
+ timer = setInterval(function () {
97
+ var isOpen = getState(ws, 'OPEN');
98
+ var isConnected = getState(ws, 'CONNECTING');
99
+ if (isOpen) {
100
+ useSendMessage(ws, {
101
+ type: 'heartbeat',
102
+ timestamp: Date.now()
103
+ });
104
+ }
105
+ else if (!isConnected) {
106
+ var open_1 = reconnectWebSocket();
107
+ open_1(ws, config, function () {
108
+ timer && clearInterval(timer);
109
+ heartbeat(ws, config);
110
+ });
111
+ }
112
+ }, heartbeatTime);
113
+ }
87
114
  function useSendMessage(ws, data) {
88
115
  var isOpen = getState(ws, 'OPEN');
89
116
  if (isOpen)
@@ -105,9 +132,6 @@
105
132
  message = message || 'connection closed by client';
106
133
  ws.close(code, message);
107
134
  }
108
- function getState(ws, type) {
109
- return ws.readyState === WebSocket[type];
110
- }
111
135
 
112
136
  exports.createWebSocket = createWebSocket;
113
137
  exports.getState = getState;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lightsoft/js-sdk",
3
- "version": "1.0.2",
3
+ "version": "1.1.0",
4
4
  "description": "",
5
5
  "main": "dist/index.umd.js",
6
6
  "module": "dist/index.js",