ly-utils-lib 2.4.0 → 2.6.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/README.md +114 -0
- package/dist/index-Cq1GhjpY.d.cts +229 -0
- package/dist/index-Cq1GhjpY.d.ts +229 -0
- package/dist/index.cjs +336 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +336 -1
- package/dist/index.js.map +1 -1
- package/dist/websocket.cjs +338 -0
- package/dist/websocket.cjs.map +1 -0
- package/dist/websocket.d.cts +1 -0
- package/dist/websocket.d.ts +1 -0
- package/dist/websocket.js +330 -0
- package/dist/websocket.js.map +1 -0
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -8,6 +8,7 @@ export { i as map } from './index-C0qUnb9Y.cjs';
|
|
|
8
8
|
export { i as storage } from './index-Ba1rjTzj.cjs';
|
|
9
9
|
export { i as utils } from './index-B80SEVzM.cjs';
|
|
10
10
|
export { i as crypto } from './index-Bg1ise7y.cjs';
|
|
11
|
+
export { i as websocket } from './index-Cq1GhjpY.cjs';
|
|
11
12
|
export { Dayjs, default as dayjs } from 'dayjs';
|
|
12
13
|
import 'es-toolkit';
|
|
13
14
|
import 'xlsx';
|
package/dist/index.d.ts
CHANGED
|
@@ -8,6 +8,7 @@ export { i as map } from './index-C0qUnb9Y.js';
|
|
|
8
8
|
export { i as storage } from './index-Ba1rjTzj.js';
|
|
9
9
|
export { i as utils } from './index-B80SEVzM.js';
|
|
10
10
|
export { i as crypto } from './index-Bg1ise7y.js';
|
|
11
|
+
export { i as websocket } from './index-Cq1GhjpY.js';
|
|
11
12
|
export { Dayjs, default as dayjs } from 'dayjs';
|
|
12
13
|
import 'es-toolkit';
|
|
13
14
|
import 'xlsx';
|
package/dist/index.js
CHANGED
|
@@ -3281,6 +3281,341 @@ var pbkdf2 = (password, salt, iterations = 1e3, keySize = 32) => {
|
|
|
3281
3281
|
var uuid2 = () => {
|
|
3282
3282
|
return CryptoJS.lib.WordArray.random(16).toString();
|
|
3283
3283
|
};
|
|
3284
|
+
|
|
3285
|
+
// src/modules/websocket/index.ts
|
|
3286
|
+
var websocket_exports = {};
|
|
3287
|
+
__export(websocket_exports, {
|
|
3288
|
+
WebSocketClient: () => WebSocketClient,
|
|
3289
|
+
WebSocketState: () => WebSocketState,
|
|
3290
|
+
createWebSocket: () => createWebSocket,
|
|
3291
|
+
default: () => websocket_default,
|
|
3292
|
+
quickConnect: () => quickConnect
|
|
3293
|
+
});
|
|
3294
|
+
var WebSocketState = /* @__PURE__ */ ((WebSocketState2) => {
|
|
3295
|
+
WebSocketState2[WebSocketState2["CONNECTING"] = 0] = "CONNECTING";
|
|
3296
|
+
WebSocketState2[WebSocketState2["OPEN"] = 1] = "OPEN";
|
|
3297
|
+
WebSocketState2[WebSocketState2["CLOSING"] = 2] = "CLOSING";
|
|
3298
|
+
WebSocketState2[WebSocketState2["CLOSED"] = 3] = "CLOSED";
|
|
3299
|
+
return WebSocketState2;
|
|
3300
|
+
})(WebSocketState || {});
|
|
3301
|
+
var WebSocketClient = class {
|
|
3302
|
+
constructor(options) {
|
|
3303
|
+
this.ws = null;
|
|
3304
|
+
this.handlers = {};
|
|
3305
|
+
this.reconnectTimer = null;
|
|
3306
|
+
this.heartbeatTimer = null;
|
|
3307
|
+
this.heartbeatTimeoutTimer = null;
|
|
3308
|
+
this.reconnectCount = 0;
|
|
3309
|
+
this.isManualDisconnect = false;
|
|
3310
|
+
this.messageQueue = [];
|
|
3311
|
+
this.connectResolve = null;
|
|
3312
|
+
this.connectReject = null;
|
|
3313
|
+
this.options = {
|
|
3314
|
+
url: options.url,
|
|
3315
|
+
protocol: options.protocol || (options.url.startsWith("https") ? "wss" : "ws"),
|
|
3316
|
+
protocols: options.protocols || [],
|
|
3317
|
+
autoReconnect: options.autoReconnect ?? true,
|
|
3318
|
+
reconnectAttempts: options.reconnectAttempts ?? 3,
|
|
3319
|
+
reconnectInterval: options.reconnectInterval ?? 3e3,
|
|
3320
|
+
heartbeatInterval: options.heartbeatInterval ?? 3e4,
|
|
3321
|
+
heartbeatMessage: options.heartbeatMessage ?? "ping",
|
|
3322
|
+
heartbeatTimeout: options.heartbeatTimeout ?? 5e3,
|
|
3323
|
+
connectTimeout: options.connectTimeout ?? 1e4,
|
|
3324
|
+
debug: options.debug ?? true
|
|
3325
|
+
};
|
|
3326
|
+
if (options.autoReconnect !== false) {
|
|
3327
|
+
this.connect();
|
|
3328
|
+
}
|
|
3329
|
+
}
|
|
3330
|
+
/**
|
|
3331
|
+
* 连接 WebSocket
|
|
3332
|
+
*/
|
|
3333
|
+
connect() {
|
|
3334
|
+
if (this.ws && (this.ws.readyState === WebSocket.OPEN || this.ws.readyState === WebSocket.CONNECTING)) {
|
|
3335
|
+
this.log("WebSocket already connected or connecting");
|
|
3336
|
+
return;
|
|
3337
|
+
}
|
|
3338
|
+
this.isManualDisconnect = false;
|
|
3339
|
+
this.reconnectCount = 0;
|
|
3340
|
+
const protocol = this.options.url.startsWith("ws://") || this.options.url.startsWith("wss://") ? "" : `${this.options.protocol}://`;
|
|
3341
|
+
const wsUrl = protocol ? `${protocol}${this.options.url}` : this.options.url;
|
|
3342
|
+
this.log(`Connecting to ${wsUrl}`);
|
|
3343
|
+
try {
|
|
3344
|
+
this.ws = new WebSocket(wsUrl, this.options.protocols);
|
|
3345
|
+
this.setupEventHandlers();
|
|
3346
|
+
setTimeout(() => {
|
|
3347
|
+
if (this.ws && this.ws.readyState === WebSocket.CONNECTING) {
|
|
3348
|
+
this.ws.close();
|
|
3349
|
+
this.log("Connection timeout");
|
|
3350
|
+
if (this.connectReject) {
|
|
3351
|
+
this.connectReject(new Error("Connection timeout"));
|
|
3352
|
+
this.connectResolve = null;
|
|
3353
|
+
this.connectReject = null;
|
|
3354
|
+
}
|
|
3355
|
+
}
|
|
3356
|
+
}, this.options.connectTimeout);
|
|
3357
|
+
} catch (error) {
|
|
3358
|
+
this.log("Connection failed:", error);
|
|
3359
|
+
this.handleError(error);
|
|
3360
|
+
}
|
|
3361
|
+
}
|
|
3362
|
+
/**
|
|
3363
|
+
* 断开连接
|
|
3364
|
+
*/
|
|
3365
|
+
disconnect() {
|
|
3366
|
+
this.isManualDisconnect = true;
|
|
3367
|
+
this.cleanup();
|
|
3368
|
+
if (this.ws) {
|
|
3369
|
+
this.ws.close();
|
|
3370
|
+
this.ws = null;
|
|
3371
|
+
}
|
|
3372
|
+
this.log("Disconnected manually");
|
|
3373
|
+
}
|
|
3374
|
+
/**
|
|
3375
|
+
* 发送消息(异步)
|
|
3376
|
+
*/
|
|
3377
|
+
async send(data) {
|
|
3378
|
+
if (!this.isConnected()) {
|
|
3379
|
+
return new Promise((resolve, reject) => {
|
|
3380
|
+
this.messageQueue.push(data);
|
|
3381
|
+
const checkConnection = () => {
|
|
3382
|
+
if (this.isConnected()) {
|
|
3383
|
+
this.sendSync(data);
|
|
3384
|
+
resolve();
|
|
3385
|
+
} else if (!this.ws) {
|
|
3386
|
+
reject(new Error("WebSocket is not connected"));
|
|
3387
|
+
} else {
|
|
3388
|
+
setTimeout(checkConnection, 100);
|
|
3389
|
+
}
|
|
3390
|
+
};
|
|
3391
|
+
checkConnection();
|
|
3392
|
+
});
|
|
3393
|
+
}
|
|
3394
|
+
this.sendSync(data);
|
|
3395
|
+
}
|
|
3396
|
+
/**
|
|
3397
|
+
* 发送消息(同步)
|
|
3398
|
+
*/
|
|
3399
|
+
sendSync(data) {
|
|
3400
|
+
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
|
|
3401
|
+
this.log("WebSocket is not connected, message queued");
|
|
3402
|
+
this.messageQueue.push(data);
|
|
3403
|
+
return;
|
|
3404
|
+
}
|
|
3405
|
+
try {
|
|
3406
|
+
const message = typeof data === "object" ? JSON.stringify(data) : data;
|
|
3407
|
+
this.ws.send(message);
|
|
3408
|
+
this.log("Message sent:", message);
|
|
3409
|
+
} catch (error) {
|
|
3410
|
+
this.log("Send error:", error);
|
|
3411
|
+
this.handleError(error);
|
|
3412
|
+
}
|
|
3413
|
+
}
|
|
3414
|
+
/**
|
|
3415
|
+
* 获取连接状态
|
|
3416
|
+
*/
|
|
3417
|
+
getState() {
|
|
3418
|
+
return this.ws?.readyState ?? 3 /* CLOSED */;
|
|
3419
|
+
}
|
|
3420
|
+
/**
|
|
3421
|
+
* 是否已连接
|
|
3422
|
+
*/
|
|
3423
|
+
isConnected() {
|
|
3424
|
+
return this.ws?.readyState === WebSocket.OPEN;
|
|
3425
|
+
}
|
|
3426
|
+
/**
|
|
3427
|
+
* 获取 WebSocket 实例
|
|
3428
|
+
*/
|
|
3429
|
+
getInstance() {
|
|
3430
|
+
return this.ws;
|
|
3431
|
+
}
|
|
3432
|
+
/**
|
|
3433
|
+
* 设置事件处理器
|
|
3434
|
+
*/
|
|
3435
|
+
on(event, handler) {
|
|
3436
|
+
this.handlers[event] = handler;
|
|
3437
|
+
}
|
|
3438
|
+
/**
|
|
3439
|
+
* 移除事件处理器
|
|
3440
|
+
*/
|
|
3441
|
+
off(event, handler) {
|
|
3442
|
+
if (!handler || this.handlers[event] === handler) {
|
|
3443
|
+
delete this.handlers[event];
|
|
3444
|
+
}
|
|
3445
|
+
}
|
|
3446
|
+
/**
|
|
3447
|
+
* 重新连接
|
|
3448
|
+
*/
|
|
3449
|
+
reconnect() {
|
|
3450
|
+
this.disconnect();
|
|
3451
|
+
this.reconnectCount = 0;
|
|
3452
|
+
this.isManualDisconnect = false;
|
|
3453
|
+
this.connect();
|
|
3454
|
+
}
|
|
3455
|
+
/**
|
|
3456
|
+
* 设置事件处理器
|
|
3457
|
+
*/
|
|
3458
|
+
setupEventHandlers() {
|
|
3459
|
+
if (!this.ws) return;
|
|
3460
|
+
this.ws.onopen = (event) => {
|
|
3461
|
+
this.log("Connected");
|
|
3462
|
+
this.reconnectCount = 0;
|
|
3463
|
+
while (this.messageQueue.length > 0) {
|
|
3464
|
+
const message = this.messageQueue.shift();
|
|
3465
|
+
if (message) {
|
|
3466
|
+
this.sendSync(message);
|
|
3467
|
+
}
|
|
3468
|
+
}
|
|
3469
|
+
this.startHeartbeat();
|
|
3470
|
+
if (this.handlers.onOpen) {
|
|
3471
|
+
this.handlers.onOpen(event);
|
|
3472
|
+
}
|
|
3473
|
+
if (this.connectResolve) {
|
|
3474
|
+
this.connectResolve();
|
|
3475
|
+
this.connectResolve = null;
|
|
3476
|
+
this.connectReject = null;
|
|
3477
|
+
}
|
|
3478
|
+
};
|
|
3479
|
+
this.ws.onmessage = (event) => {
|
|
3480
|
+
this.log("Message received:", event.data);
|
|
3481
|
+
let data = event.data;
|
|
3482
|
+
if (typeof event.data === "string") {
|
|
3483
|
+
try {
|
|
3484
|
+
data = JSON.parse(event.data);
|
|
3485
|
+
} catch {
|
|
3486
|
+
}
|
|
3487
|
+
}
|
|
3488
|
+
if (this.handlers.onMessage) {
|
|
3489
|
+
this.handlers.onMessage(data, event);
|
|
3490
|
+
}
|
|
3491
|
+
};
|
|
3492
|
+
this.ws.onclose = (event) => {
|
|
3493
|
+
this.log("Connection closed:", event.code, event.reason);
|
|
3494
|
+
this.cleanup();
|
|
3495
|
+
if (this.handlers.onClose) {
|
|
3496
|
+
this.handlers.onClose(event);
|
|
3497
|
+
}
|
|
3498
|
+
if (!this.isManualDisconnect && this.options.autoReconnect) {
|
|
3499
|
+
this.attemptReconnect();
|
|
3500
|
+
}
|
|
3501
|
+
};
|
|
3502
|
+
this.ws.onerror = (event) => {
|
|
3503
|
+
this.log("Error:", event);
|
|
3504
|
+
if (this.handlers.onError) {
|
|
3505
|
+
this.handlers.onError(event);
|
|
3506
|
+
}
|
|
3507
|
+
};
|
|
3508
|
+
}
|
|
3509
|
+
/**
|
|
3510
|
+
* 尝试重连
|
|
3511
|
+
*/
|
|
3512
|
+
attemptReconnect() {
|
|
3513
|
+
if (this.reconnectCount >= this.options.reconnectAttempts) {
|
|
3514
|
+
this.log("Reconnect failed, max attempts reached");
|
|
3515
|
+
this.cleanup();
|
|
3516
|
+
if (this.handlers.onReconnectFailed) {
|
|
3517
|
+
this.handlers.onReconnectFailed();
|
|
3518
|
+
}
|
|
3519
|
+
if (this.connectReject) {
|
|
3520
|
+
this.connectReject(new Error("Reconnect failed"));
|
|
3521
|
+
this.connectResolve = null;
|
|
3522
|
+
this.connectReject = null;
|
|
3523
|
+
}
|
|
3524
|
+
return;
|
|
3525
|
+
}
|
|
3526
|
+
this.reconnectCount++;
|
|
3527
|
+
this.log(`Reconnecting... Attempt ${this.reconnectCount}/${this.options.reconnectAttempts}`);
|
|
3528
|
+
if (this.handlers.onReconnect) {
|
|
3529
|
+
this.handlers.onReconnect(this.reconnectCount);
|
|
3530
|
+
}
|
|
3531
|
+
this.reconnectTimer = setTimeout(() => {
|
|
3532
|
+
this.connect();
|
|
3533
|
+
}, this.options.reconnectInterval);
|
|
3534
|
+
}
|
|
3535
|
+
/**
|
|
3536
|
+
* 启动心跳
|
|
3537
|
+
*/
|
|
3538
|
+
startHeartbeat() {
|
|
3539
|
+
this.stopHeartbeat();
|
|
3540
|
+
this.heartbeatTimer = setInterval(() => {
|
|
3541
|
+
if (this.isConnected()) {
|
|
3542
|
+
const message = this.options.heartbeatMessage;
|
|
3543
|
+
this.log("Sending heartbeat");
|
|
3544
|
+
if (this.handlers.onHeartbeat) {
|
|
3545
|
+
this.handlers.onHeartbeat();
|
|
3546
|
+
}
|
|
3547
|
+
this.sendSync(message);
|
|
3548
|
+
this.heartbeatTimeoutTimer = setTimeout(() => {
|
|
3549
|
+
this.log("Heartbeat timeout");
|
|
3550
|
+
if (this.handlers.onHeartbeatTimeout) {
|
|
3551
|
+
this.handlers.onHeartbeatTimeout();
|
|
3552
|
+
}
|
|
3553
|
+
if (this.ws) {
|
|
3554
|
+
this.ws.close();
|
|
3555
|
+
}
|
|
3556
|
+
}, this.options.heartbeatTimeout);
|
|
3557
|
+
}
|
|
3558
|
+
}, this.options.heartbeatInterval);
|
|
3559
|
+
}
|
|
3560
|
+
/**
|
|
3561
|
+
* 停止心跳
|
|
3562
|
+
*/
|
|
3563
|
+
stopHeartbeat() {
|
|
3564
|
+
if (this.heartbeatTimer) {
|
|
3565
|
+
clearInterval(this.heartbeatTimer);
|
|
3566
|
+
this.heartbeatTimer = null;
|
|
3567
|
+
}
|
|
3568
|
+
if (this.heartbeatTimeoutTimer) {
|
|
3569
|
+
clearTimeout(this.heartbeatTimeoutTimer);
|
|
3570
|
+
this.heartbeatTimeoutTimer = null;
|
|
3571
|
+
}
|
|
3572
|
+
}
|
|
3573
|
+
/**
|
|
3574
|
+
* 清理资源
|
|
3575
|
+
*/
|
|
3576
|
+
cleanup() {
|
|
3577
|
+
this.stopHeartbeat();
|
|
3578
|
+
if (this.reconnectTimer) {
|
|
3579
|
+
clearTimeout(this.reconnectTimer);
|
|
3580
|
+
this.reconnectTimer = null;
|
|
3581
|
+
}
|
|
3582
|
+
}
|
|
3583
|
+
/**
|
|
3584
|
+
* 处理错误
|
|
3585
|
+
*/
|
|
3586
|
+
handleError(error) {
|
|
3587
|
+
this.log("Error:", error.message);
|
|
3588
|
+
if (this.handlers.onError) {
|
|
3589
|
+
this.handlers.onError(new ErrorEvent("error", { error }));
|
|
3590
|
+
}
|
|
3591
|
+
}
|
|
3592
|
+
/**
|
|
3593
|
+
* 日志输出
|
|
3594
|
+
*/
|
|
3595
|
+
log(...args) {
|
|
3596
|
+
if (this.options.debug) {
|
|
3597
|
+
console.log("[WebSocket]", ...args);
|
|
3598
|
+
}
|
|
3599
|
+
}
|
|
3600
|
+
};
|
|
3601
|
+
function createWebSocket(options) {
|
|
3602
|
+
return new WebSocketClient(options);
|
|
3603
|
+
}
|
|
3604
|
+
function quickConnect(url, handlers) {
|
|
3605
|
+
const client = new WebSocketClient({ url, autoReconnect: true });
|
|
3606
|
+
if (handlers) {
|
|
3607
|
+
Object.entries(handlers).forEach(([event, handler]) => {
|
|
3608
|
+
client.on(event, handler);
|
|
3609
|
+
});
|
|
3610
|
+
}
|
|
3611
|
+
return client;
|
|
3612
|
+
}
|
|
3613
|
+
var websocket_default = {
|
|
3614
|
+
WebSocketClient,
|
|
3615
|
+
createWebSocket,
|
|
3616
|
+
quickConnect,
|
|
3617
|
+
WebSocketState
|
|
3618
|
+
};
|
|
3284
3619
|
/**
|
|
3285
3620
|
* Utils Toolkit
|
|
3286
3621
|
* 一个功能强大的 JavaScript/TypeScript 工具函数库
|
|
@@ -3289,6 +3624,6 @@ var uuid2 = () => {
|
|
|
3289
3624
|
* @license MIT
|
|
3290
3625
|
*/
|
|
3291
3626
|
|
|
3292
|
-
export { array_exports as array, crypto_exports as crypto, date_exports as date, excel_exports as excel, map_exports as map, object_exports as object, pdf_exports as pdf, storage_exports as storage, string_exports as string, utils_exports as utils };
|
|
3627
|
+
export { array_exports as array, crypto_exports as crypto, date_exports as date, excel_exports as excel, map_exports as map, object_exports as object, pdf_exports as pdf, storage_exports as storage, string_exports as string, utils_exports as utils, websocket_exports as websocket };
|
|
3293
3628
|
//# sourceMappingURL=index.js.map
|
|
3294
3629
|
//# sourceMappingURL=index.js.map
|