@lightsoft/js-sdk 1.0.1 → 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 +108 -2
- package/dist/index.js +2403 -263
- package/dist/index.umd.js +608 -266
- package/dist/rebuild.d.ts +4 -0
- package/dist/rebuild.js +236 -0
- package/dist/rebuild.umd.js +245 -0
- package/dist/types/enums/api.d.ts +61 -0
- package/dist/types/index.d.ts +4 -3
- package/dist/types/reg-exp/index.d.ts +12 -0
- package/dist/types/types/api.d.ts +16 -0
- package/dist/types/{uilts → utils}/common.d.ts +2 -2
- package/dist/types/utils/index.d.ts +6 -0
- package/dist/types/utils/mask.d.ts +11 -0
- package/dist/types/utils/rebuild.d.ts +3 -0
- package/dist/types/utils/request.d.ts +36 -0
- package/dist/types/utils/tour.d.ts +22 -0
- package/dist/types/{uilts → utils}/vue.d.ts +1 -1
- package/dist/types/utils/ws.d.ts +14 -0
- package/dist/ws-0exRUlFu.js +144 -0
- package/dist/ws-BPCzgHy0.js +168 -0
- package/dist/ws-CIMFnzix.js +169 -0
- package/dist/ws-DIZYbi31.js +144 -0
- package/dist/ws-D_5GJokP.js +167 -0
- package/dist/ws-DjIqS-Gi.js +169 -0
- package/dist/ws-EehpMWgu.js +168 -0
- package/dist/ws-mqcHwlvx.js +166 -0
- package/dist/ws.d.ts +15 -0
- package/dist/ws.js +1 -0
- package/dist/ws.umd.js +142 -0
- package/package.json +5 -2
- package/dist/types/uilts/index.d.ts +0 -3
- package/dist/types/uilts/rebuild.d.ts +0 -1
- /package/dist/types/enums/{index.enum.d.ts → index.d.ts} +0 -0
- /package/dist/types/types/{index.interface.d.ts → index.d.ts} +0 -0
|
@@ -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
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
type WebSocketState = 'CONNECTING' | 'OPEN' | 'CLOSING' | 'CLOSED';
|
|
2
|
+
interface WebSocketConfig {
|
|
3
|
+
heartbeat?: boolean;
|
|
4
|
+
heartbeatTime?: number;
|
|
5
|
+
reconnect: boolean;
|
|
6
|
+
reconnectTime?: number;
|
|
7
|
+
maxReconnectNumber?: number;
|
|
8
|
+
}
|
|
9
|
+
declare function createWebSocket(url: string, config: WebSocketConfig | null, onOpen?: Function | null): WebSocket;
|
|
10
|
+
declare function getState(ws: WebSocket, type: WebSocketState): boolean;
|
|
11
|
+
declare function useSendMessage(ws: WebSocket, data: any): void;
|
|
12
|
+
declare function useWebSocketMessage(ws: WebSocket, callback: Function): void;
|
|
13
|
+
declare function useWebSocketClose(ws: WebSocket, code?: number, message?: string): void;
|
|
14
|
+
|
|
15
|
+
export { createWebSocket, getState, useSendMessage, useWebSocketClose, useWebSocketMessage };
|
package/dist/ws.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { c as createWebSocket, g as getState, u as useSendMessage, e as useWebSocketClose, d as useWebSocketMessage } from './ws-EehpMWgu.js';
|