aldehyde 0.2.449 → 0.2.452

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.
Files changed (42) hide show
  1. package/lib/controls/enum-badge/index.d.ts.map +1 -1
  2. package/lib/controls/enum-badge/index.js +21 -3
  3. package/lib/controls/enum-badge/index.js.map +1 -1
  4. package/lib/controls/enum-tag/index.d.ts.map +1 -1
  5. package/lib/controls/enum-tag/index.js +34 -19
  6. package/lib/controls/enum-tag/index.js.map +1 -1
  7. package/lib/form/criteria-form.js +1 -1
  8. package/lib/form/criteria-form.js.map +1 -1
  9. package/lib/lowcode-components/base-image/index.d.ts.map +1 -1
  10. package/lib/lowcode-components/base-image/index.js +5 -2
  11. package/lib/lowcode-components/base-image/index.js.map +1 -1
  12. package/lib/table/pagination.js +2 -2
  13. package/lib/table/pagination.js.map +1 -1
  14. package/lib/table/query-table.d.ts.map +1 -1
  15. package/lib/table/query-table.js +3 -2
  16. package/lib/table/query-table.js.map +1 -1
  17. package/lib/table/select-table.d.ts.map +1 -1
  18. package/lib/table/select-table.js +2 -2
  19. package/lib/table/select-table.js.map +1 -1
  20. package/lib/tmpl/hc-data-source.d.ts +2 -2
  21. package/lib/tmpl/hc-data-source.d.ts.map +1 -1
  22. package/lib/tmpl/hc-data-source.js +24 -18
  23. package/lib/tmpl/hc-data-source.js.map +1 -1
  24. package/lib/tmpl/hcservice-v3.d.ts +4 -0
  25. package/lib/tmpl/hcservice-v3.d.ts.map +1 -1
  26. package/lib/tmpl/hcservice-v3.js +16 -0
  27. package/lib/tmpl/hcservice-v3.js.map +1 -1
  28. package/lib/tmpl/web-socket.d.ts +52 -0
  29. package/lib/tmpl/web-socket.d.ts.map +1 -0
  30. package/lib/tmpl/web-socket.js +355 -0
  31. package/lib/tmpl/web-socket.js.map +1 -0
  32. package/package.json +3 -1
  33. package/src/aldehyde/controls/enum-badge/index.tsx +14 -4
  34. package/src/aldehyde/controls/enum-tag/index.tsx +29 -21
  35. package/src/aldehyde/form/criteria-form.tsx +1 -1
  36. package/src/aldehyde/lowcode-components/base-image/index.tsx +5 -2
  37. package/src/aldehyde/table/pagination.tsx +2 -2
  38. package/src/aldehyde/table/query-table.tsx +2 -1
  39. package/src/aldehyde/table/select-table.tsx +2 -1
  40. package/src/aldehyde/tmpl/hc-data-source.tsx +13 -7
  41. package/src/aldehyde/tmpl/hcservice-v3.tsx +14 -0
  42. package/src/aldehyde/tmpl/web-socket.ts +367 -0
@@ -0,0 +1,367 @@
1
+ import SockJS from 'sockjs-client';
2
+ import { Client, IFrame, IMessage, StompSubscription } from '@stomp/stompjs';
3
+ import Units from "../units";
4
+
5
+ /**
6
+ * 使用方法:
7
+ * const webSocket= WebSocket.getInstance(WS_URL); // 创建连接
8
+ * const subscribe = webSocket.subscribe('/topic/test',{}, (val)=>{console.log("推送的数据:",val)}); // 订阅
9
+ * subscribe.unsubscribe(); // 销毁订阅
10
+ * webSocket.disconnect(); // 关闭连接
11
+ */
12
+
13
+ // 获取订阅地址
14
+ const getWebSocketConfig = (path, options) => {
15
+ const serverKey = options.serverKey;
16
+ const header = options.connectionHeaders || {};
17
+ const programCode = header.programCode || Units.programCode(serverKey);
18
+ let server = new URL(options.hydrocarbonServer ? options.hydrocarbonServer : Units.api(serverKey));
19
+ let hydrocarbonToken = null;
20
+ if (header?.hydrocarbonToken) {
21
+ hydrocarbonToken = header.hydrocarbonToken;
22
+ } else if (serverKey) {
23
+ hydrocarbonToken = Units.serverHydrocarbonToken(serverKey)
24
+ } else if (Units.hydrocarbonToken(programCode)) {
25
+ hydrocarbonToken = Units.hydrocarbonToken(programCode);
26
+ } else {
27
+ hydrocarbonToken = Units.getAnoHydrocarbonToken()
28
+ }
29
+ const url = `${server.protocol}//${server.host}${server.pathname}/${path}`;
30
+ return { url, hydrocarbonToken, programCode };
31
+ }
32
+
33
+ export interface WebSocketOptions {
34
+ reconnectDelay?: number; // 重连间隔(ms)
35
+ maxReconnectAttempts?: number; // 最大重连次数
36
+ heartbeatIncoming?: number; // 接收心跳最大间隔
37
+ heartbeatOutgoing?: number; // 发送心跳最小间隔
38
+ debug?: boolean; // 是否开启调试日志
39
+ connectionHeaders?: { [key: string]: string }; // 创建连接请求头信息
40
+ }
41
+
42
+ // 全局订阅Map类型
43
+ interface SubscriptionRecord {
44
+ topic: string;
45
+ callback: (message: any, rawMessage: IMessage) => void;
46
+ subscription: StompSubscription | null;
47
+ }
48
+
49
+ // 订阅队列
50
+ interface QueuedOperation {
51
+ type: 'SUBSCRIBE' | 'SEND';
52
+ payload: any;
53
+ }
54
+
55
+ // 连接状态枚举
56
+ enum ConnectionState {
57
+ DISCONNECTED = 'DISCONNECTED', // 断开
58
+ CONNECTING = 'CONNECTING', // 连接中
59
+ CONNECTED = 'CONNECTED', // 已连接
60
+ RECONNECTING = 'RECONNECTING', // 重连中
61
+ }
62
+
63
+ class WebSocket {
64
+ private static instances = new Map<string, WebSocket>();
65
+
66
+ /**
67
+ * 获取单例实例的唯一入口
68
+ * @param url WebSocket 服务器地址
69
+ * @param options 配置项
70
+ * @returns 返回对应 URL 的唯一 WebSocket 实例
71
+ */
72
+ public static getInstance(url: string, options: WebSocketOptions = {}): WebSocket {
73
+ if (!WebSocket.instances.has(url)) {
74
+ // 注意:这里调用的是私有的构造函数
75
+ const instance = new WebSocket(url, options);
76
+ WebSocket.instances.set(url, instance);
77
+ }
78
+ return WebSocket.instances.get(url)!;
79
+ }
80
+
81
+ /**
82
+ * 私有构造函数
83
+ * 防止外部直接使用 new WebSocket() 导致单例失效
84
+ */
85
+ private constructor(url: string, options: WebSocketOptions = {}) {
86
+ this.url = url;
87
+ this.config = { ...this.config, ...options };
88
+ this.connect();
89
+ }
90
+
91
+ private url: string;
92
+ private client: Client | null = null;
93
+ private subscriptions = new Map<string, SubscriptionRecord>();
94
+ private operationQueue: QueuedOperation[] = [];
95
+ private state: ConnectionState = ConnectionState.DISCONNECTED;
96
+ private reconnectAttempts = 0;
97
+ private reconnectTimer: any = null;
98
+ private connectPromises = new Map<string, { resolve: () => void; reject: (error: Error) => void }>();
99
+ private config: Required<WebSocketOptions> = {
100
+ reconnectDelay: 5000,
101
+ maxReconnectAttempts: 5,
102
+ heartbeatIncoming: 10000,
103
+ heartbeatOutgoing: 10000,
104
+ debug: false,
105
+ connectionHeaders: {},
106
+ };
107
+
108
+ // 建立连接
109
+ public connect(): Promise<void> {
110
+ if (this.state === ConnectionState.CONNECTED) { // 当前已连接不重复创建连接
111
+ return Promise.resolve();
112
+ }
113
+ this.state = ConnectionState.CONNECTING;
114
+ const { url: wsUrl, hydrocarbonToken, programCode } = getWebSocketConfig(this.url, this.config);
115
+ return new Promise<void>((resolve, reject) => {
116
+ const currentPromiseId = Date.now().toString();
117
+ this.connectPromises.set(currentPromiseId, { resolve, reject });
118
+ const { heartbeatIncoming, heartbeatOutgoing, reconnectDelay } = this.config;
119
+ const socket = new SockJS(wsUrl);
120
+ this.client = new Client({
121
+ webSocketFactory: () => socket,
122
+ debug: this.config.debug ? (str) => console.log('[STOMP]', str) : () => { },
123
+ heartbeatIncoming,
124
+ heartbeatOutgoing,
125
+ reconnectDelay,
126
+ connectHeaders: {
127
+ "hydrocarbon-token": hydrocarbonToken,
128
+ "hydrocarbon-program-token": programCode
129
+ },
130
+ onConnect: (frame) => { // 连接成功回调
131
+ console.log("连接成功:frame", frame);
132
+ this.state = ConnectionState.CONNECTED;
133
+ this.reconnectAttempts = 0;
134
+ this.connectPromises.forEach(({ resolve }) => resolve());
135
+ this.connectPromises.clear();
136
+ this._restoreSubscriptions();
137
+ this._flushQueue().catch(console.error);
138
+ },
139
+ onStompError: (frame: IFrame) => {
140
+ console.error('[WS] ❌ STOMP 协议错误', frame);
141
+ const error = new Error(frame.headers['message'] || 'STOMP Error');
142
+ if (this.state !== ConnectionState.DISCONNECTED) {
143
+ this.state = ConnectionState.DISCONNECTED;
144
+ this.client = null;
145
+ this._scheduleReconnect();
146
+ }
147
+ this.connectPromises.forEach(({ reject }) => reject(error));
148
+ this.connectPromises.clear();
149
+ },
150
+ onWebSocketClose: () => {
151
+ if ([ConnectionState.CONNECTED, ConnectionState.CONNECTING].includes(this.state)) {
152
+ console.warn('[WS] ⚠️ 连接意外断开,准备重连...');
153
+ this.state = ConnectionState.DISCONNECTED;
154
+ this.client = null;
155
+ this._scheduleReconnect(); // 断开重连
156
+ }
157
+ },
158
+ onWebSocketError: (error) => {
159
+ console.error('[WS] ❌ WebSocket 错误', error);
160
+ const wsError = new Error('WebSocket Error');
161
+ if (this.state !== ConnectionState.DISCONNECTED) {
162
+ this.state = ConnectionState.DISCONNECTED;
163
+ this.client = null;
164
+ this._scheduleReconnect();
165
+ }
166
+ this.connectPromises.forEach(({ reject }) => reject(wsError));
167
+ this.connectPromises.clear();
168
+ },
169
+ });
170
+ this.client.activate();
171
+ });
172
+ }
173
+
174
+ // 订阅请求
175
+ public async subscribe<T = any>(topic: string, params: { [key: string]: any }, callback: (message: T, rawMessage: IMessage) => void): Promise<StompSubscription> {
176
+ if (this.state === ConnectionState.CONNECTED && this.client) {
177
+ return this._doSubscribe(topic, callback, params);
178
+ } else {
179
+ return new Promise((resolve, reject) => {
180
+ this.operationQueue.push({
181
+ type: 'SUBSCRIBE',
182
+ payload: { topic, callback, resolve, reject, params },
183
+ });
184
+ });
185
+ }
186
+ }
187
+
188
+ // 执行实际订阅
189
+ private _doSubscribe<T>(topic: string, callback: (message: T, rawMessage: IMessage) => void, params: { [key: string]: any }): Promise<StompSubscription> {
190
+ return new Promise((resolve, reject) => {
191
+ if (!this.client) {
192
+ reject(new Error('Client not initialized'));
193
+ return;
194
+ }
195
+ try {
196
+ const stompSub = this.client.subscribe(topic, (message: IMessage) => {
197
+ try {
198
+ let body: any = message.body;
199
+ if (typeof message.body === 'string') {
200
+ body = JSON.parse(message.body);
201
+ }
202
+ callback(body, message);
203
+ } catch (e) {
204
+ console.error('[WS] JSON 解析错误', e);
205
+ callback(message.body as any, message);
206
+ }
207
+ }, params || {});
208
+ // 3. 更新内存记录
209
+ this.subscriptions.set(topic, {
210
+ topic,
211
+ callback,
212
+ subscription: stompSub // 关键:确保新的 subscription 对象被保存
213
+ });
214
+ resolve(stompSub);
215
+ } catch (error) {
216
+ reject(error);
217
+ }
218
+ });
219
+ }
220
+
221
+ // 发送消息
222
+ public send(destination: string, body: any): void {
223
+ if (this.state === ConnectionState.CONNECTED && this.client) {
224
+ try {
225
+ this.client.publish({
226
+ destination,
227
+ body: typeof body === 'string' ? body : JSON.stringify(body),
228
+ headers: { 'content-type': 'application/json' },
229
+ });
230
+ } catch (error) {
231
+ console.error('[WS] 发送消息失败:', error);
232
+ }
233
+ } else {
234
+ this.operationQueue.push({ type: 'SEND', payload: { destination, body } });
235
+ }
236
+ }
237
+
238
+ // 取消订阅
239
+ public unsubscribe(topic: string): boolean {
240
+ const record = this.subscriptions.get(topic);
241
+ if (record) {
242
+ record.subscription?.unsubscribe();
243
+ this.subscriptions.delete(topic);
244
+ console.log(`[WS] 🚫 已取消订阅: ${topic}`);
245
+ return true;
246
+ }
247
+ return false;
248
+ }
249
+
250
+ // 取消所有订阅
251
+ public unsubscribeAll(): void {
252
+ this.subscriptions.forEach((record) => {
253
+ record.subscription?.unsubscribe();
254
+ });
255
+ this.subscriptions.clear();
256
+ console.log('[WS] 🚫 已取消所有订阅');
257
+ }
258
+
259
+ // 断开连接
260
+ public disconnect(): void {
261
+ this.state = ConnectionState.DISCONNECTED;
262
+ if (this.reconnectTimer) {
263
+ clearTimeout(this.reconnectTimer);
264
+ this.reconnectTimer = null;
265
+ }
266
+ if (this.client) {
267
+ WebSocket.instances.delete(this.client.webSocket.url);
268
+ this.client.deactivate();
269
+ this.client = null;
270
+ }
271
+ this.unsubscribeAll();
272
+ this.operationQueue = [];
273
+ this.connectPromises.forEach(({ reject }) => {
274
+ reject(new Error('Connection manually disconnected'));
275
+ });
276
+ this.connectPromises.clear();
277
+ console.log('[WS] 🔌 客户端已主动断开');
278
+ }
279
+
280
+ // 当前订阅数量
281
+ public getSubscriptionCount(): number {
282
+ return this.subscriptions.size;
283
+ }
284
+
285
+ // 连接后恢复订阅
286
+ private _restoreSubscriptions() {
287
+ if (this.subscriptions.size === 0 || !this.client) return;
288
+ console.log(`[WS] 🔄 正在恢复 ${this.subscriptions.size} 个订阅...`);
289
+ this.subscriptions.forEach((record, topic) => {
290
+ try {
291
+ const stompSub = this.client.subscribe(topic, (message: IMessage) => {
292
+ try {
293
+ let body: any = message.body;
294
+ if (typeof message.body === 'string') {
295
+ body = JSON.parse(message.body);
296
+ }
297
+ record.callback(body, message);
298
+ } catch (e) {
299
+ record.callback(message.body, message);
300
+ }
301
+ });
302
+ record.subscription = stompSub;
303
+ } catch (error) {
304
+ console.error(`[WS] 恢复订阅失败: ${topic}`, error);
305
+ }
306
+ });
307
+ }
308
+
309
+ // 订阅队列
310
+ private async _flushQueue() {
311
+ if (this.operationQueue.length === 0) return;
312
+ console.log(`[WS] 🚀 正在执行 ${this.operationQueue.length} 个排队操作...`);
313
+ const queue = [...this.operationQueue];
314
+ this.operationQueue = [];
315
+ for (const op of queue) {
316
+ try {
317
+ if (op.type === 'SUBSCRIBE') {
318
+ const { topic, callback, resolve, reject, params } = op.payload;
319
+ try {
320
+ const subscription = await this._doSubscribe(topic, callback, params);
321
+ resolve(subscription);
322
+ } catch (error) {
323
+ reject(error);
324
+ }
325
+ } else if (op.type === 'SEND') {
326
+ const { destination, body } = op.payload;
327
+ this.send(destination, body);
328
+ }
329
+ } catch (error) {
330
+ console.error('[WS] 执行队列操作失败:', error);
331
+ }
332
+ }
333
+ }
334
+
335
+ // 断连重连
336
+ private _scheduleReconnect() {
337
+ if (this.reconnectAttempts >= this.config.maxReconnectAttempts) {
338
+ console.error('[WS] ❌ 重连失败:达到最大尝试次数');
339
+ return;
340
+ }
341
+ if (![ConnectionState.RECONNECTING, ConnectionState.DISCONNECTED].includes(this.state)) {
342
+ return;
343
+ }
344
+ this.reconnectAttempts++;
345
+ const delay = Math.min(this.config.reconnectDelay * Math.pow(1.5, this.reconnectAttempts - 1), 60000);
346
+ console.log(`[WS] ⏱️ ${delay}ms 后进行第 ${this.reconnectAttempts} 次重连...`);
347
+ this.reconnectTimer = setTimeout(() => {
348
+ this.connect().catch((err) => {
349
+ if (this.state !== ConnectionState.DISCONNECTED) {
350
+ console.error('[WS] 重连尝试异常:', err);
351
+ }
352
+ });
353
+ }, delay);
354
+ }
355
+
356
+ // 判断是否已连接
357
+ public isConnected(): boolean {
358
+ return this.state === ConnectionState.CONNECTED;
359
+ }
360
+
361
+ // 获取所有订阅的topic
362
+ public getSubscribedTopics(): string[] {
363
+ return Array.from(this.subscriptions.keys());
364
+ }
365
+ }
366
+
367
+ export default WebSocket;