@sybz-components/utils 0.0.12 → 1.0.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/ws.mjs DELETED
@@ -1,169 +0,0 @@
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
- /**
17
- * 创建并立即连接一个 WebSocket 实例。
18
- *
19
- * @param url WebSocket 地址,例如 `wss://example.com/ws`。
20
- * @param options 连接配置,支持重连、心跳和 query 参数。
21
- *
22
- * @example
23
- * const ws = new WS('wss://example.com/ws', {
24
- * autoReconnect: { reconnectMaxCount: 5 },
25
- * heartbeat: { message: 'ping', interval: 5000 },
26
- * query: { token: 'abc123' },
27
- * })
28
- */
29
- constructor(url, options) {
30
- const { autoReconnect = true, query = {}, heartbeat = false } = options || {};
31
- this.autoReconnect = autoReconnect;
32
- this.heartbeat = heartbeat;
33
- this.query = query;
34
- this.url = `${url}` + qs.stringify({ ...this.query }, { addQueryPrefix: true });
35
- this.connect();
36
- }
37
- /**
38
- * 主动发起连接。
39
- *
40
- * 调用时会先关闭旧连接,再创建新的 WebSocket 实例。
41
- *
42
- * @example
43
- * const ws = new WS('wss://example.com/ws')
44
- * ws.connect()
45
- */
46
- connect() {
47
- this.close();
48
- this.socket = new WebSocket(this.url);
49
- this.onError();
50
- this.onOpen();
51
- }
52
- /**
53
- * 注册连接成功后的处理逻辑,并在需要时开启心跳。
54
- *
55
- * @example
56
- * const ws = new WS('wss://example.com/ws')
57
- * ws.onOpen()
58
- */
59
- onOpen() {
60
- if (this.socket) {
61
- this.socket.onopen = () => {
62
- this.send("ping");
63
- this.heartbeat && this.startHeartbeat();
64
- };
65
- }
66
- }
67
- /**
68
- * 开启心跳发送。
69
- *
70
- * @example
71
- * const ws = new WS('wss://example.com/ws', {
72
- * heartbeat: { message: 'ping', interval: 3000 },
73
- * })
74
- * ws.startHeartbeat()
75
- */
76
- startHeartbeat() {
77
- const msg = this.heartbeat?.message || message;
78
- const int = this.heartbeat?.interval || interval;
79
- this.timer = setInterval(() => {
80
- this.send(msg);
81
- }, int);
82
- }
83
- /**
84
- * 注册错误处理和自动重连逻辑。
85
- *
86
- * @example
87
- * const ws = new WS('wss://example.com/ws', { autoReconnect: true })
88
- * ws.onError()
89
- */
90
- onError() {
91
- if (this.socket) {
92
- this.socket.onerror = () => {
93
- const count = this.autoReconnect?.reconnectMaxCount || reconnectMaxCount;
94
- if (this.autoReconnect && this.reconnectCount < count) {
95
- this.reconnectCount++;
96
- this.connect();
97
- }
98
- };
99
- }
100
- }
101
- /**
102
- * 关闭连接并清理相关定时器。
103
- *
104
- * @example
105
- * ws.close()
106
- */
107
- close() {
108
- this.socket && this.socket.close();
109
- this.delay && clearTimeout(this.delay);
110
- this.timer && clearInterval(this.timer);
111
- this.socket = null;
112
- }
113
- /**
114
- * 监听服务端消息。
115
- *
116
- * 内部会优先尝试将消息解析为 JSON;解析失败时会回传原始 `MessageEvent`。
117
- *
118
- * @param callback 接收到消息时触发的回调。
119
- *
120
- * @example
121
- * ws.onMessage((message) => {
122
- * console.log(message)
123
- * })
124
- */
125
- onMessage(callback) {
126
- if (this.socket) {
127
- this.socket.onmessage = (data) => {
128
- try {
129
- const res = JSON.parse(data.data);
130
- callback(res);
131
- } catch {
132
- callback(data);
133
- }
134
- };
135
- }
136
- }
137
- /**
138
- * 发送消息。
139
- *
140
- * - 如果连接已就绪,会立即发送
141
- * - 如果正在连接,会延迟发送
142
- * - 如果连接已关闭,会先重新连接再发送
143
- *
144
- * @param data 需要发送的消息内容。
145
- *
146
- * @example
147
- * ws.send('hello')
148
- *
149
- * @example
150
- * ws.send(JSON.stringify({ type: 'ping' }))
151
- */
152
- send(data) {
153
- if (!this.socket) return;
154
- if (this.socket.readyState === this.socket.OPEN) {
155
- this.socket.send(JSON.stringify(data));
156
- } else if (this.socket.readyState === this.socket.CONNECTING) {
157
- this.delay = setTimeout(() => {
158
- this.socket?.send(data);
159
- }, timeout);
160
- } else {
161
- this.connect();
162
- this.delay = setTimeout(() => {
163
- this.socket?.send(data);
164
- }, timeout);
165
- }
166
- }
167
- }
168
-
169
- export { WS };