@ipcom/asterisk-ari 0.0.152 → 0.0.153
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/esm/index.js
CHANGED
|
@@ -2320,17 +2320,12 @@ var DEFAULT_STARTING_DELAY = 500;
|
|
|
2320
2320
|
var DEFAULT_MAX_DELAY = 1e4;
|
|
2321
2321
|
var WebSocketClient = class extends EventEmitter3 {
|
|
2322
2322
|
/**
|
|
2323
|
-
* Creates a new
|
|
2323
|
+
* Creates a new WebSocket client instance.
|
|
2324
2324
|
*
|
|
2325
|
-
*
|
|
2326
|
-
*
|
|
2327
|
-
*
|
|
2328
|
-
* @param
|
|
2329
|
-
* @param apps - An array of application names to connect to via the WebSocket.
|
|
2330
|
-
* @param subscribedEvents - Optional. An array of WebSocketEventTypes to subscribe to. If not provided, all events will be subscribed.
|
|
2331
|
-
* @param ariClient - Optional. The AriClient instance, used for creating Channel and Playback instances when processing events.
|
|
2332
|
-
*
|
|
2333
|
-
* @throws {Error} Throws an error if the apps array is empty.
|
|
2325
|
+
* @param {BaseClient} baseClient - The base client containing connection details
|
|
2326
|
+
* @param {string[]} apps - List of applications to connect to
|
|
2327
|
+
* @param {WebSocketEventType[]} [subscribedEvents] - Optional list of events to subscribe to
|
|
2328
|
+
* @param {AriClient} [ariClient] - Optional ARI client for handling channel and playback events
|
|
2334
2329
|
*/
|
|
2335
2330
|
constructor(baseClient, apps, subscribedEvents, ariClient) {
|
|
2336
2331
|
super();
|
|
@@ -2345,8 +2340,6 @@ var WebSocketClient = class extends EventEmitter3 {
|
|
|
2345
2340
|
ws;
|
|
2346
2341
|
isReconnecting = false;
|
|
2347
2342
|
maxReconnectAttempts = DEFAULT_MAX_RECONNECT_ATTEMPTS;
|
|
2348
|
-
reconnectionAttempts = 0;
|
|
2349
|
-
lastWsUrl = "";
|
|
2350
2343
|
backOffOptions = {
|
|
2351
2344
|
numOfAttempts: DEFAULT_MAX_RECONNECT_ATTEMPTS,
|
|
2352
2345
|
startingDelay: DEFAULT_STARTING_DELAY,
|
|
@@ -2363,14 +2356,10 @@ var WebSocketClient = class extends EventEmitter3 {
|
|
|
2363
2356
|
}
|
|
2364
2357
|
};
|
|
2365
2358
|
/**
|
|
2366
|
-
* Establishes a WebSocket connection
|
|
2367
|
-
*
|
|
2368
|
-
* This method constructs the WebSocket URL using the base URL, credentials,
|
|
2369
|
-
* application names, and subscribed events. It then initiates the connection
|
|
2370
|
-
* using the constructed URL.
|
|
2359
|
+
* Establishes a WebSocket connection.
|
|
2371
2360
|
*
|
|
2372
|
-
* @returns
|
|
2373
|
-
* @throws
|
|
2361
|
+
* @returns {Promise<void>} Resolves when connection is established
|
|
2362
|
+
* @throws {Error} If connection fails
|
|
2374
2363
|
*/
|
|
2375
2364
|
async connect() {
|
|
2376
2365
|
const { baseUrl, username, password } = this.baseClient.getCredentials();
|
|
@@ -2385,24 +2374,15 @@ var WebSocketClient = class extends EventEmitter3 {
|
|
|
2385
2374
|
} else {
|
|
2386
2375
|
queryParams.append("subscribeAll", "true");
|
|
2387
2376
|
}
|
|
2388
|
-
|
|
2377
|
+
const wsUrl = `${protocol}://${encodeURIComponent(username)}:${encodeURIComponent(password)}@${normalizedHost}/ari/events?${queryParams.toString()}`;
|
|
2389
2378
|
console.log("Connecting to WebSocket...");
|
|
2390
|
-
return this.initializeWebSocket(
|
|
2379
|
+
return this.initializeWebSocket(wsUrl);
|
|
2391
2380
|
}
|
|
2392
2381
|
/**
|
|
2393
|
-
* Initializes
|
|
2394
|
-
*
|
|
2395
|
-
* This method attempts to establish a WebSocket connection to the specified URL.
|
|
2396
|
-
* It sets up event listeners for the WebSocket's 'open', 'message', 'close', and 'error' events.
|
|
2397
|
-
* If the connection is successful, it emits a 'connected' event. If it's a reconnection,
|
|
2398
|
-
* it also emits a 'reconnected' event with the current apps and subscribed events.
|
|
2399
|
-
* In case of connection failure, it uses an exponential backoff strategy to retry.
|
|
2382
|
+
* Initializes WebSocket connection with reconnection logic.
|
|
2400
2383
|
*
|
|
2401
|
-
* @param wsUrl - The WebSocket URL to connect to
|
|
2402
|
-
* @returns
|
|
2403
|
-
* or rejects if an error occurs during the connection process.
|
|
2404
|
-
* @throws Will throw an error if the WebSocket connection cannot be established
|
|
2405
|
-
* after the maximum number of retry attempts.
|
|
2384
|
+
* @param {string} wsUrl - The WebSocket URL to connect to
|
|
2385
|
+
* @returns {Promise<void>} Resolves when connection is established
|
|
2406
2386
|
*/
|
|
2407
2387
|
async initializeWebSocket(wsUrl) {
|
|
2408
2388
|
return (0, import_exponential_backoff.backOff)(async () => {
|
|
@@ -2411,14 +2391,7 @@ var WebSocketClient = class extends EventEmitter3 {
|
|
|
2411
2391
|
this.ws = new WebSocket(wsUrl);
|
|
2412
2392
|
this.ws.on("open", () => {
|
|
2413
2393
|
console.log("WebSocket connection established successfully");
|
|
2414
|
-
if (this.isReconnecting) {
|
|
2415
|
-
this.emit("reconnected", {
|
|
2416
|
-
apps: this.apps,
|
|
2417
|
-
subscribedEvents: this.subscribedEvents
|
|
2418
|
-
});
|
|
2419
|
-
}
|
|
2420
2394
|
this.isReconnecting = false;
|
|
2421
|
-
this.reconnectionAttempts = 0;
|
|
2422
2395
|
this.emit("connected");
|
|
2423
2396
|
resolve();
|
|
2424
2397
|
});
|
|
@@ -2428,13 +2401,13 @@ var WebSocketClient = class extends EventEmitter3 {
|
|
|
2428
2401
|
`WebSocket disconnected with code ${code}. Attempting to reconnect...`
|
|
2429
2402
|
);
|
|
2430
2403
|
if (!this.isReconnecting) {
|
|
2431
|
-
this.reconnect(
|
|
2404
|
+
this.reconnect(wsUrl);
|
|
2432
2405
|
}
|
|
2433
2406
|
});
|
|
2434
2407
|
this.ws.on("error", (err) => {
|
|
2435
2408
|
console.error("WebSocket error:", err.message);
|
|
2436
2409
|
if (!this.isReconnecting) {
|
|
2437
|
-
this.reconnect(
|
|
2410
|
+
this.reconnect(wsUrl);
|
|
2438
2411
|
}
|
|
2439
2412
|
reject(err);
|
|
2440
2413
|
});
|
|
@@ -2445,16 +2418,9 @@ var WebSocketClient = class extends EventEmitter3 {
|
|
|
2445
2418
|
}, this.backOffOptions);
|
|
2446
2419
|
}
|
|
2447
2420
|
/**
|
|
2448
|
-
*
|
|
2449
|
-
*
|
|
2450
|
-
* This method parses the raw message into a WebSocketEvent, filters it based on
|
|
2451
|
-
* subscribed events (if any), processes channel and playback events, and emits
|
|
2452
|
-
* the event to listeners. It also handles any errors that occur during processing.
|
|
2421
|
+
* Processes incoming WebSocket messages.
|
|
2453
2422
|
*
|
|
2454
|
-
* @param rawMessage - The raw message
|
|
2455
|
-
* @returns void This method doesn't return a value but emits events.
|
|
2456
|
-
*
|
|
2457
|
-
* @throws Will emit an 'error' event if the message cannot be parsed or processed.
|
|
2423
|
+
* @param {string} rawMessage - The raw message received from WebSocket
|
|
2458
2424
|
*/
|
|
2459
2425
|
handleMessage(rawMessage) {
|
|
2460
2426
|
try {
|
|
@@ -2482,27 +2448,18 @@ var WebSocketClient = class extends EventEmitter3 {
|
|
|
2482
2448
|
}
|
|
2483
2449
|
}
|
|
2484
2450
|
/**
|
|
2485
|
-
* Attempts to reconnect to the WebSocket
|
|
2486
|
-
*
|
|
2487
|
-
* This method is called when the WebSocket connection is closed unexpectedly.
|
|
2488
|
-
* It increments the reconnection attempt counter, logs the attempt, and uses
|
|
2489
|
-
* the backOff utility to retry the connection with exponential delays between attempts.
|
|
2451
|
+
* Attempts to reconnect to the WebSocket.
|
|
2490
2452
|
*
|
|
2491
|
-
* @param wsUrl - The WebSocket URL to reconnect to
|
|
2492
|
-
* @returns void - This method doesn't return a value.
|
|
2493
|
-
*
|
|
2494
|
-
* @emits reconnectFailed - Emitted if all reconnection attempts fail.
|
|
2453
|
+
* @param {string} wsUrl - The WebSocket URL to reconnect to
|
|
2495
2454
|
*/
|
|
2496
2455
|
reconnect(wsUrl) {
|
|
2497
2456
|
this.isReconnecting = true;
|
|
2498
|
-
|
|
2499
|
-
|
|
2500
|
-
`Initiating reconnection attempt #${this.reconnectionAttempts}...`
|
|
2501
|
-
);
|
|
2457
|
+
console.log("Initiating reconnection attempt...");
|
|
2458
|
+
this.removeAllListeners();
|
|
2502
2459
|
(0, import_exponential_backoff.backOff)(() => this.initializeWebSocket(wsUrl), this.backOffOptions).catch(
|
|
2503
2460
|
(error) => {
|
|
2504
2461
|
console.error(
|
|
2505
|
-
|
|
2462
|
+
"Failed to reconnect after multiple attempts:",
|
|
2506
2463
|
error instanceof Error ? error.message : "Unknown error"
|
|
2507
2464
|
);
|
|
2508
2465
|
this.emit("reconnectFailed", error);
|
|
@@ -2510,13 +2467,7 @@ var WebSocketClient = class extends EventEmitter3 {
|
|
|
2510
2467
|
);
|
|
2511
2468
|
}
|
|
2512
2469
|
/**
|
|
2513
|
-
*
|
|
2514
|
-
*
|
|
2515
|
-
* This method attempts to gracefully close the WebSocket connection
|
|
2516
|
-
* and sets the WebSocket instance to undefined. If an error occurs
|
|
2517
|
-
* during the closing process, it will be caught and logged.
|
|
2518
|
-
*
|
|
2519
|
-
* @throws {Error} Logs an error message if closing the WebSocket fails.
|
|
2470
|
+
* Manually closes the WebSocket connection.
|
|
2520
2471
|
*/
|
|
2521
2472
|
close() {
|
|
2522
2473
|
try {
|
|
@@ -2532,9 +2483,19 @@ var WebSocketClient = class extends EventEmitter3 {
|
|
|
2532
2483
|
);
|
|
2533
2484
|
}
|
|
2534
2485
|
}
|
|
2486
|
+
/**
|
|
2487
|
+
* Checks if the WebSocket is currently connected.
|
|
2488
|
+
*
|
|
2489
|
+
* @returns {boolean} True if connected, false otherwise
|
|
2490
|
+
*/
|
|
2535
2491
|
isConnected() {
|
|
2536
2492
|
return this.ws?.readyState === WebSocket.OPEN;
|
|
2537
2493
|
}
|
|
2494
|
+
/**
|
|
2495
|
+
* Gets the current connection state.
|
|
2496
|
+
*
|
|
2497
|
+
* @returns {number} The WebSocket ready state
|
|
2498
|
+
*/
|
|
2538
2499
|
getState() {
|
|
2539
2500
|
return this.ws?.readyState ?? WebSocket.CLOSED;
|
|
2540
2501
|
}
|