@oeos-components/utils 0.0.19 → 0.0.22

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/ws.d.cts ADDED
@@ -0,0 +1,86 @@
1
+ /**
2
+ * setTimeout 类型
3
+ */
4
+ type Timeout = ReturnType<typeof setTimeout>;
5
+ /**
6
+ * setInterval 类型
7
+ */
8
+ type Interval = ReturnType<typeof setInterval>;
9
+ /**
10
+ * 允许null的泛型
11
+ */
12
+ type Nullable<T> = T | null;
13
+ type AutoReconnect = {
14
+ /**
15
+ *重连尝试次数 默认 3
16
+ */
17
+ reconnectMaxCount?: number;
18
+ };
19
+ type Heartbeat = {
20
+ /**
21
+ * 心跳信息 默认`ping`
22
+ */
23
+ message: string;
24
+ /**
25
+ * 心跳间隔时间 默认 `3000` 毫秒
26
+ */
27
+ interval: number;
28
+ };
29
+ interface WSOptions {
30
+ /**
31
+ * 是否自动重连 默认`true`
32
+ */
33
+ autoReconnect: boolean | AutoReconnect;
34
+ /**
35
+ * 心跳 默认`false`
36
+ */
37
+ heartbeat: boolean | Heartbeat;
38
+ /**
39
+ * url 携带的参数
40
+ */
41
+ query: Record<string, string>;
42
+ }
43
+ declare class WS {
44
+ url: string;
45
+ socket: WebSocket | null;
46
+ reconnectCount: number;
47
+ delay: Nullable<Timeout>;
48
+ timer: Nullable<Interval>;
49
+ autoReconnect: WSOptions["autoReconnect"];
50
+ heartbeat: WSOptions["heartbeat"];
51
+ query: WSOptions["query"];
52
+ constructor(url?: string, options?: WSOptions);
53
+ /**
54
+ * 连接
55
+ */
56
+ connect(): void;
57
+ /**
58
+ * 监听连接
59
+ */
60
+ onOpen(): void;
61
+ /**
62
+ * 开启心跳
63
+ */
64
+ startHeartbeat(): void;
65
+ /**
66
+ * 监听错误
67
+ */
68
+ onError(): void;
69
+ /**
70
+ * 关闭连接
71
+ */
72
+ close(): void;
73
+ /**
74
+ * 监听消息
75
+ * @param callback
76
+ */
77
+ onMessage(callback: (...data: any[]) => any): void;
78
+ /**
79
+ * 发送消息
80
+ * @param data
81
+ */
82
+ send(data: string | ArrayBufferLike | Blob | ArrayBufferView): void;
83
+ }
84
+
85
+ export { WS };
86
+ export type { WSOptions };
package/dist/ws.d.mts ADDED
@@ -0,0 +1,86 @@
1
+ /**
2
+ * setTimeout 类型
3
+ */
4
+ type Timeout = ReturnType<typeof setTimeout>;
5
+ /**
6
+ * setInterval 类型
7
+ */
8
+ type Interval = ReturnType<typeof setInterval>;
9
+ /**
10
+ * 允许null的泛型
11
+ */
12
+ type Nullable<T> = T | null;
13
+ type AutoReconnect = {
14
+ /**
15
+ *重连尝试次数 默认 3
16
+ */
17
+ reconnectMaxCount?: number;
18
+ };
19
+ type Heartbeat = {
20
+ /**
21
+ * 心跳信息 默认`ping`
22
+ */
23
+ message: string;
24
+ /**
25
+ * 心跳间隔时间 默认 `3000` 毫秒
26
+ */
27
+ interval: number;
28
+ };
29
+ interface WSOptions {
30
+ /**
31
+ * 是否自动重连 默认`true`
32
+ */
33
+ autoReconnect: boolean | AutoReconnect;
34
+ /**
35
+ * 心跳 默认`false`
36
+ */
37
+ heartbeat: boolean | Heartbeat;
38
+ /**
39
+ * url 携带的参数
40
+ */
41
+ query: Record<string, string>;
42
+ }
43
+ declare class WS {
44
+ url: string;
45
+ socket: WebSocket | null;
46
+ reconnectCount: number;
47
+ delay: Nullable<Timeout>;
48
+ timer: Nullable<Interval>;
49
+ autoReconnect: WSOptions["autoReconnect"];
50
+ heartbeat: WSOptions["heartbeat"];
51
+ query: WSOptions["query"];
52
+ constructor(url?: string, options?: WSOptions);
53
+ /**
54
+ * 连接
55
+ */
56
+ connect(): void;
57
+ /**
58
+ * 监听连接
59
+ */
60
+ onOpen(): void;
61
+ /**
62
+ * 开启心跳
63
+ */
64
+ startHeartbeat(): void;
65
+ /**
66
+ * 监听错误
67
+ */
68
+ onError(): void;
69
+ /**
70
+ * 关闭连接
71
+ */
72
+ close(): void;
73
+ /**
74
+ * 监听消息
75
+ * @param callback
76
+ */
77
+ onMessage(callback: (...data: any[]) => any): void;
78
+ /**
79
+ * 发送消息
80
+ * @param data
81
+ */
82
+ send(data: string | ArrayBufferLike | Blob | ArrayBufferView): void;
83
+ }
84
+
85
+ export { WS };
86
+ export type { WSOptions };
package/dist/ws.d.ts ADDED
@@ -0,0 +1,86 @@
1
+ /**
2
+ * setTimeout 类型
3
+ */
4
+ type Timeout = ReturnType<typeof setTimeout>;
5
+ /**
6
+ * setInterval 类型
7
+ */
8
+ type Interval = ReturnType<typeof setInterval>;
9
+ /**
10
+ * 允许null的泛型
11
+ */
12
+ type Nullable<T> = T | null;
13
+ type AutoReconnect = {
14
+ /**
15
+ *重连尝试次数 默认 3
16
+ */
17
+ reconnectMaxCount?: number;
18
+ };
19
+ type Heartbeat = {
20
+ /**
21
+ * 心跳信息 默认`ping`
22
+ */
23
+ message: string;
24
+ /**
25
+ * 心跳间隔时间 默认 `3000` 毫秒
26
+ */
27
+ interval: number;
28
+ };
29
+ interface WSOptions {
30
+ /**
31
+ * 是否自动重连 默认`true`
32
+ */
33
+ autoReconnect: boolean | AutoReconnect;
34
+ /**
35
+ * 心跳 默认`false`
36
+ */
37
+ heartbeat: boolean | Heartbeat;
38
+ /**
39
+ * url 携带的参数
40
+ */
41
+ query: Record<string, string>;
42
+ }
43
+ declare class WS {
44
+ url: string;
45
+ socket: WebSocket | null;
46
+ reconnectCount: number;
47
+ delay: Nullable<Timeout>;
48
+ timer: Nullable<Interval>;
49
+ autoReconnect: WSOptions["autoReconnect"];
50
+ heartbeat: WSOptions["heartbeat"];
51
+ query: WSOptions["query"];
52
+ constructor(url?: string, options?: WSOptions);
53
+ /**
54
+ * 连接
55
+ */
56
+ connect(): void;
57
+ /**
58
+ * 监听连接
59
+ */
60
+ onOpen(): void;
61
+ /**
62
+ * 开启心跳
63
+ */
64
+ startHeartbeat(): void;
65
+ /**
66
+ * 监听错误
67
+ */
68
+ onError(): void;
69
+ /**
70
+ * 关闭连接
71
+ */
72
+ close(): void;
73
+ /**
74
+ * 监听消息
75
+ * @param callback
76
+ */
77
+ onMessage(callback: (...data: any[]) => any): void;
78
+ /**
79
+ * 发送消息
80
+ * @param data
81
+ */
82
+ send(data: string | ArrayBufferLike | Blob | ArrayBufferView): void;
83
+ }
84
+
85
+ export { WS };
86
+ export type { WSOptions };
package/dist/ws.mjs ADDED
@@ -0,0 +1,114 @@
1
+ import qs from 'qs';
2
+
3
+ const reconnectMaxCount = 3;
4
+ const message = "ping";
5
+ const interval = 3e3;
6
+ const timeout = 1e3;
7
+ class WS {
8
+ url;
9
+ socket = null;
10
+ reconnectCount = 0;
11
+ delay = null;
12
+ timer = null;
13
+ autoReconnect;
14
+ heartbeat;
15
+ query;
16
+ constructor(url, options) {
17
+ const { autoReconnect = true, query = {}, heartbeat = false } = options || {};
18
+ this.autoReconnect = autoReconnect;
19
+ this.heartbeat = heartbeat;
20
+ this.query = query;
21
+ this.url = `${url}` + qs.stringify({ ...this.query }, { addQueryPrefix: true });
22
+ this.connect();
23
+ }
24
+ /**
25
+ * 连接
26
+ */
27
+ connect() {
28
+ this.close();
29
+ this.socket = new WebSocket(this.url);
30
+ this.onError();
31
+ this.onOpen();
32
+ }
33
+ /**
34
+ * 监听连接
35
+ */
36
+ onOpen() {
37
+ if (this.socket) {
38
+ this.socket.onopen = () => {
39
+ this.send("ping");
40
+ this.heartbeat && this.startHeartbeat();
41
+ };
42
+ }
43
+ }
44
+ /**
45
+ * 开启心跳
46
+ */
47
+ startHeartbeat() {
48
+ const msg = this.heartbeat?.message || message;
49
+ const int = this.heartbeat?.interval || interval;
50
+ this.timer = setInterval(() => {
51
+ this.send(msg);
52
+ }, int);
53
+ }
54
+ /**
55
+ * 监听错误
56
+ */
57
+ onError() {
58
+ if (this.socket) {
59
+ this.socket.onerror = () => {
60
+ const count = this.autoReconnect?.reconnectMaxCount || reconnectMaxCount;
61
+ if (this.autoReconnect && this.reconnectCount < count) {
62
+ this.reconnectCount++;
63
+ this.connect();
64
+ }
65
+ };
66
+ }
67
+ }
68
+ /**
69
+ * 关闭连接
70
+ */
71
+ close() {
72
+ this.socket && this.socket.close();
73
+ this.delay && clearTimeout(this.delay);
74
+ this.timer && clearInterval(this.timer);
75
+ this.socket = null;
76
+ }
77
+ /**
78
+ * 监听消息
79
+ * @param callback
80
+ */
81
+ onMessage(callback) {
82
+ if (this.socket) {
83
+ this.socket.onmessage = (data) => {
84
+ try {
85
+ const res = JSON.parse(data.data);
86
+ callback(res);
87
+ } catch (err) {
88
+ callback(data);
89
+ }
90
+ };
91
+ }
92
+ }
93
+ /**
94
+ * 发送消息
95
+ * @param data
96
+ */
97
+ send(data) {
98
+ if (!this.socket) return;
99
+ if (this.socket.readyState === this.socket.OPEN) {
100
+ this.socket.send(JSON.stringify(data));
101
+ } else if (this.socket.readyState === this.socket.CONNECTING) {
102
+ this.delay = setTimeout(() => {
103
+ this.socket?.send(data);
104
+ }, timeout);
105
+ } else {
106
+ this.connect();
107
+ this.delay = setTimeout(() => {
108
+ this.socket?.send(data);
109
+ }, timeout);
110
+ }
111
+ }
112
+ }
113
+
114
+ export { WS };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oeos-components/utils",
3
- "version": "0.0.19",
3
+ "version": "0.0.22",
4
4
  "description": "utils of oeos-components",
5
5
  "license": "MIT",
6
6
  "type": "commonjs",
@@ -25,9 +25,11 @@
25
25
  "@vue/reactivity": "^3.5.18",
26
26
  "@vue/shared": "^3.5.18",
27
27
  "consola": "^3.4.2",
28
+ "dayjs": "^1.11.19",
28
29
  "element-plus": "^2.11.5",
29
30
  "es-toolkit": "^1.39.10",
30
31
  "lodash-es": "^4.17.21",
32
+ "qs": "^6.15.0",
31
33
  "vue": "3.4.15"
32
34
  }
33
35
  }