@ipcom/asterisk-ari 0.0.24 → 0.0.26
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/cjs/index.cjs +404 -57
- package/dist/cjs/index.cjs.map +4 -4
- package/dist/esm/index.js +403 -57
- package/dist/esm/index.js.map +4 -4
- package/dist/types/ari-client/ariClient.d.ts +126 -18
- package/dist/types/ari-client/ariClient.d.ts.map +1 -1
- package/dist/types/ari-client/interfaces/bridges.types.d.ts +38 -0
- package/dist/types/ari-client/interfaces/bridges.types.d.ts.map +1 -0
- package/dist/types/ari-client/interfaces/events.types.d.ts +78 -0
- package/dist/types/ari-client/interfaces/events.types.d.ts.map +1 -1
- package/dist/types/ari-client/interfaces/index.d.ts +1 -0
- package/dist/types/ari-client/interfaces/index.d.ts.map +1 -1
- package/dist/types/ari-client/interfaces/playbacks.types.d.ts +10 -3
- package/dist/types/ari-client/interfaces/playbacks.types.d.ts.map +1 -1
- package/dist/types/ari-client/resources/asterisk.d.ts +1 -1
- package/dist/types/ari-client/resources/asterisk.d.ts.map +1 -1
- package/dist/types/ari-client/resources/bridges.d.ts +46 -0
- package/dist/types/ari-client/resources/bridges.d.ts.map +1 -1
- package/dist/types/ari-client/resources/playbacks.d.ts +51 -5
- package/dist/types/ari-client/resources/playbacks.d.ts.map +1 -1
- package/dist/types/ari-client/websocketClient.d.ts +56 -5
- package/dist/types/ari-client/websocketClient.d.ts.map +1 -1
- package/dist/types/index.d.ts +2 -1
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/esm/index.js
CHANGED
|
@@ -572,6 +572,7 @@ var require_backoff = __commonJS({
|
|
|
572
572
|
|
|
573
573
|
// src/ari-client/ariClient.ts
|
|
574
574
|
var import_exponential_backoff = __toESM(require_backoff(), 1);
|
|
575
|
+
import { EventEmitter as EventEmitter3 } from "events";
|
|
575
576
|
|
|
576
577
|
// src/ari-client/baseClient.ts
|
|
577
578
|
import axios from "axios";
|
|
@@ -745,6 +746,95 @@ var Asterisk = class {
|
|
|
745
746
|
}
|
|
746
747
|
};
|
|
747
748
|
|
|
749
|
+
// src/ari-client/resources/bridges.ts
|
|
750
|
+
var Bridges = class {
|
|
751
|
+
constructor(client) {
|
|
752
|
+
this.client = client;
|
|
753
|
+
}
|
|
754
|
+
/**
|
|
755
|
+
* Lists all active bridges.
|
|
756
|
+
*/
|
|
757
|
+
async list() {
|
|
758
|
+
return this.client.get("/bridges");
|
|
759
|
+
}
|
|
760
|
+
/**
|
|
761
|
+
* Creates a new bridge.
|
|
762
|
+
*/
|
|
763
|
+
async createBridge(request) {
|
|
764
|
+
return this.client.post("/bridges", request);
|
|
765
|
+
}
|
|
766
|
+
/**
|
|
767
|
+
* Retrieves details of a specific bridge.
|
|
768
|
+
*/
|
|
769
|
+
async getDetails(bridgeId) {
|
|
770
|
+
return this.client.get(`/bridges/${bridgeId}`);
|
|
771
|
+
}
|
|
772
|
+
/**
|
|
773
|
+
* Destroys (deletes) a specific bridge.
|
|
774
|
+
*/
|
|
775
|
+
async destroy(bridgeId) {
|
|
776
|
+
return this.client.delete(`/bridges/${bridgeId}`);
|
|
777
|
+
}
|
|
778
|
+
/**
|
|
779
|
+
* Adds a channel or multiple channels to a bridge.
|
|
780
|
+
*/
|
|
781
|
+
async addChannels(bridgeId, request) {
|
|
782
|
+
const queryParams = new URLSearchParams({
|
|
783
|
+
channel: Array.isArray(request.channel) ? request.channel.join(",") : request.channel,
|
|
784
|
+
...request.role && { role: request.role }
|
|
785
|
+
}).toString();
|
|
786
|
+
await this.client.post(
|
|
787
|
+
`/bridges/${bridgeId}/addChannel?${queryParams}`
|
|
788
|
+
);
|
|
789
|
+
}
|
|
790
|
+
/**
|
|
791
|
+
* Removes a channel or multiple channels from a bridge.
|
|
792
|
+
*/
|
|
793
|
+
async removeChannels(bridgeId, request) {
|
|
794
|
+
const queryParams = new URLSearchParams({
|
|
795
|
+
channel: Array.isArray(request.channel) ? request.channel.join(",") : request.channel
|
|
796
|
+
}).toString();
|
|
797
|
+
await this.client.post(
|
|
798
|
+
`/bridges/${bridgeId}/removeChannel?${queryParams}`
|
|
799
|
+
);
|
|
800
|
+
}
|
|
801
|
+
/**
|
|
802
|
+
* Plays media to a bridge.
|
|
803
|
+
*/
|
|
804
|
+
async playMedia(bridgeId, request) {
|
|
805
|
+
const queryParams = new URLSearchParams({
|
|
806
|
+
...request.lang && { lang: request.lang },
|
|
807
|
+
...request.offsetms && { offsetms: request.offsetms.toString() },
|
|
808
|
+
...request.skipms && { skipms: request.skipms.toString() },
|
|
809
|
+
...request.playbackId && { playbackId: request.playbackId }
|
|
810
|
+
}).toString();
|
|
811
|
+
return this.client.post(
|
|
812
|
+
`/bridges/${bridgeId}/play?${queryParams}`,
|
|
813
|
+
{ media: request.media }
|
|
814
|
+
);
|
|
815
|
+
}
|
|
816
|
+
/**
|
|
817
|
+
* Stops media playback on a bridge.
|
|
818
|
+
*/
|
|
819
|
+
async stopPlayback(bridgeId, playbackId) {
|
|
820
|
+
await this.client.delete(`/bridges/${bridgeId}/play/${playbackId}`);
|
|
821
|
+
}
|
|
822
|
+
/**
|
|
823
|
+
* Sets the video source for a bridge.
|
|
824
|
+
*/
|
|
825
|
+
async setVideoSource(bridgeId, channelId) {
|
|
826
|
+
await this.client.post(
|
|
827
|
+
`/bridges/${bridgeId}/videoSource?channelId=${encodeURIComponent(channelId)}`
|
|
828
|
+
);
|
|
829
|
+
}
|
|
830
|
+
/**
|
|
831
|
+
* Clears the video source for a bridge.
|
|
832
|
+
*/
|
|
833
|
+
async clearVideoSource(bridgeId) {
|
|
834
|
+
await this.client.delete(`/bridges/${bridgeId}/videoSource`);
|
|
835
|
+
}
|
|
836
|
+
};
|
|
837
|
+
|
|
748
838
|
// src/ari-client/resources/channels.ts
|
|
749
839
|
function toQueryParams2(options) {
|
|
750
840
|
return new URLSearchParams(
|
|
@@ -1092,8 +1182,10 @@ var Endpoints = class {
|
|
|
1092
1182
|
};
|
|
1093
1183
|
|
|
1094
1184
|
// src/ari-client/resources/playbacks.ts
|
|
1095
|
-
|
|
1185
|
+
import { EventEmitter } from "events";
|
|
1186
|
+
var Playbacks = class extends EventEmitter {
|
|
1096
1187
|
constructor(client) {
|
|
1188
|
+
super();
|
|
1097
1189
|
this.client = client;
|
|
1098
1190
|
}
|
|
1099
1191
|
/**
|
|
@@ -1106,17 +1198,22 @@ var Playbacks = class {
|
|
|
1106
1198
|
return this.client.get(`/playbacks/${playbackId}`);
|
|
1107
1199
|
}
|
|
1108
1200
|
/**
|
|
1109
|
-
* Controls a specific playback
|
|
1201
|
+
* Controls a specific playback by performing various operations such as pause, resume, restart, reverse, forward, or stop.
|
|
1110
1202
|
*
|
|
1111
1203
|
* @param playbackId - The unique identifier of the playback to control.
|
|
1112
|
-
* @param
|
|
1204
|
+
* @param operation - The operation to perform on the playback. Possible values are:
|
|
1205
|
+
* - "pause": Pauses the playback.
|
|
1206
|
+
* - "unpause": Resumes a paused playback.
|
|
1207
|
+
* - "restart": Restarts the playback from the beginning.
|
|
1208
|
+
* - "reverse": Reverses the playback direction.
|
|
1209
|
+
* - "forward": Moves the playback forward.
|
|
1210
|
+
* - "stop": Stops the playback.
|
|
1113
1211
|
* @returns A promise that resolves when the control operation is successfully executed.
|
|
1114
1212
|
*/
|
|
1115
|
-
async control(playbackId,
|
|
1116
|
-
await this.client.post(
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
);
|
|
1213
|
+
async control(playbackId, operation) {
|
|
1214
|
+
await this.client.post(`/playbacks/${playbackId}/control`, {
|
|
1215
|
+
operation
|
|
1216
|
+
});
|
|
1120
1217
|
}
|
|
1121
1218
|
/**
|
|
1122
1219
|
* Stops a specific playback.
|
|
@@ -1127,6 +1224,50 @@ var Playbacks = class {
|
|
|
1127
1224
|
async stop(playbackId) {
|
|
1128
1225
|
await this.client.post(`/playbacks/${playbackId}/stop`);
|
|
1129
1226
|
}
|
|
1227
|
+
/**
|
|
1228
|
+
* Registers a listener for playback events.
|
|
1229
|
+
* The listener is triggered for events such as "PlaybackFinished".
|
|
1230
|
+
*
|
|
1231
|
+
* @param eventType - The type of event to listen for.
|
|
1232
|
+
* @param playbackId - The ID of the playback to associate with this listener.
|
|
1233
|
+
* @param callback - The callback function to execute when the event occurs.
|
|
1234
|
+
*/
|
|
1235
|
+
registerListener(eventType, playbackId, callback) {
|
|
1236
|
+
this.on(`${eventType}:${playbackId}`, callback);
|
|
1237
|
+
}
|
|
1238
|
+
/**
|
|
1239
|
+
* Unregisters a listener for playback events.
|
|
1240
|
+
*
|
|
1241
|
+
* @param eventType - The type of event to stop listening for.
|
|
1242
|
+
* @param playbackId - The ID of the playback associated with the listener.
|
|
1243
|
+
* @param callback - The callback function to remove.
|
|
1244
|
+
*/
|
|
1245
|
+
unregisterListener(eventType, playbackId, callback) {
|
|
1246
|
+
this.off(`${eventType}:${playbackId}`, callback);
|
|
1247
|
+
}
|
|
1248
|
+
/**
|
|
1249
|
+
* Checks if a listener is already registered for a specific event and playback.
|
|
1250
|
+
*
|
|
1251
|
+
* @param eventType - The type of event to check.
|
|
1252
|
+
* @param playbackId - The playback ID associated with the listener.
|
|
1253
|
+
* @returns True if a listener is already registered, false otherwise.
|
|
1254
|
+
*/
|
|
1255
|
+
isListenerRegistered(eventType, playbackId) {
|
|
1256
|
+
return this.listenerCount(`${eventType}:${playbackId}`) > 0;
|
|
1257
|
+
}
|
|
1258
|
+
/**
|
|
1259
|
+
* Emits playback events received via WebSocket.
|
|
1260
|
+
* This method should be called by the WebSocket client when playback events occur.
|
|
1261
|
+
*
|
|
1262
|
+
* @param eventType - The type of the WebSocket event.
|
|
1263
|
+
* @param data - The data associated with the event.
|
|
1264
|
+
*/
|
|
1265
|
+
emitPlaybackEvent(eventType, data) {
|
|
1266
|
+
if ("playbackId" in data) {
|
|
1267
|
+
this.emit(`${eventType}:${data.playbackId}`, data);
|
|
1268
|
+
}
|
|
1269
|
+
this.emit(eventType, data);
|
|
1270
|
+
}
|
|
1130
1271
|
};
|
|
1131
1272
|
|
|
1132
1273
|
// src/ari-client/resources/sounds.ts
|
|
@@ -1161,16 +1302,25 @@ var Sounds = class {
|
|
|
1161
1302
|
};
|
|
1162
1303
|
|
|
1163
1304
|
// src/ari-client/websocketClient.ts
|
|
1305
|
+
import { EventEmitter as EventEmitter2 } from "events";
|
|
1164
1306
|
import WebSocket from "ws";
|
|
1165
|
-
var WebSocketClient = class {
|
|
1166
|
-
|
|
1307
|
+
var WebSocketClient = class extends EventEmitter2 {
|
|
1308
|
+
/**
|
|
1309
|
+
* Creates a new WebSocketClient instance.
|
|
1310
|
+
* @param url - The WebSocket server URL to connect to.
|
|
1311
|
+
*/
|
|
1167
1312
|
constructor(url) {
|
|
1313
|
+
super();
|
|
1168
1314
|
this.url = url;
|
|
1169
1315
|
}
|
|
1170
1316
|
ws = null;
|
|
1171
1317
|
isClosedManually = false;
|
|
1172
|
-
// Para evitar reconexões automáticas quando fechado manualmente
|
|
1173
1318
|
isReconnecting = false;
|
|
1319
|
+
/**
|
|
1320
|
+
* Establishes a connection to the WebSocket server.
|
|
1321
|
+
* @returns A Promise that resolves when the connection is established, or rejects if an error occurs.
|
|
1322
|
+
* @throws Will throw an error if the connection fails.
|
|
1323
|
+
*/
|
|
1174
1324
|
async connect() {
|
|
1175
1325
|
if (this.isReconnecting) return;
|
|
1176
1326
|
return new Promise((resolve, reject) => {
|
|
@@ -1188,47 +1338,70 @@ var WebSocketClient = class {
|
|
|
1188
1338
|
this.ws.on("close", (code, reason) => {
|
|
1189
1339
|
console.warn(`WebSocket desconectado: ${code} - ${reason}`);
|
|
1190
1340
|
this.isReconnecting = false;
|
|
1341
|
+
this.emit("close", { code, reason });
|
|
1342
|
+
});
|
|
1343
|
+
this.ws.on("message", (rawData) => {
|
|
1344
|
+
this.handleMessage(rawData);
|
|
1191
1345
|
});
|
|
1192
1346
|
});
|
|
1193
1347
|
}
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
try {
|
|
1199
|
-
await this.connect();
|
|
1200
|
-
console.log("Reconex\xE3o bem-sucedida.");
|
|
1201
|
-
} catch (err) {
|
|
1202
|
-
console.error("Erro ao tentar reconectar:", err);
|
|
1203
|
-
} finally {
|
|
1204
|
-
this.isReconnecting = false;
|
|
1205
|
-
}
|
|
1206
|
-
}
|
|
1348
|
+
/**
|
|
1349
|
+
* Checks if the WebSocket connection is currently open.
|
|
1350
|
+
* @returns True if the connection is open, false otherwise.
|
|
1351
|
+
*/
|
|
1207
1352
|
isConnected() {
|
|
1208
1353
|
return this.ws?.readyState === WebSocket.OPEN;
|
|
1209
1354
|
}
|
|
1355
|
+
/**
|
|
1356
|
+
* Adds a listener for WebSocket events.
|
|
1357
|
+
* @param event - The event type to listen for.
|
|
1358
|
+
* @param callback - The function to call when the event occurs.
|
|
1359
|
+
* @returns The WebSocketClient instance for chaining.
|
|
1360
|
+
*/
|
|
1210
1361
|
on(event, callback) {
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1362
|
+
super.on(event, callback);
|
|
1363
|
+
return this;
|
|
1364
|
+
}
|
|
1365
|
+
/**
|
|
1366
|
+
* Removes a specific listener from a WebSocket event.
|
|
1367
|
+
* @param event - The event type to remove the listener from.
|
|
1368
|
+
* @param callback - The function to remove from the event listeners.
|
|
1369
|
+
* @returns The WebSocketClient instance for chaining.
|
|
1370
|
+
*/
|
|
1371
|
+
off(event, callback) {
|
|
1372
|
+
super.off(event, callback);
|
|
1373
|
+
return this;
|
|
1374
|
+
}
|
|
1375
|
+
/**
|
|
1376
|
+
* Removes all listeners for a specific event, or all events if no event is specified.
|
|
1377
|
+
* @param event - Optional. The event to remove all listeners from.
|
|
1378
|
+
* @returns The WebSocketClient instance for chaining.
|
|
1379
|
+
*/
|
|
1380
|
+
removeAllListeners(event) {
|
|
1381
|
+
super.removeAllListeners(event);
|
|
1382
|
+
return this;
|
|
1383
|
+
}
|
|
1384
|
+
/**
|
|
1385
|
+
* Handles incoming WebSocket messages.
|
|
1386
|
+
* @param rawData - The raw data received from the WebSocket.
|
|
1387
|
+
*/
|
|
1388
|
+
handleMessage(rawData) {
|
|
1389
|
+
try {
|
|
1390
|
+
const decodedData = JSON.parse(rawData.toString());
|
|
1391
|
+
if (decodedData?.type) {
|
|
1392
|
+
this.emit(decodedData.type, decodedData);
|
|
1393
|
+
} else {
|
|
1394
|
+
console.warn("Mensagem recebida sem tipo:", decodedData);
|
|
1395
|
+
}
|
|
1396
|
+
} catch (err) {
|
|
1397
|
+
console.error("Erro ao decodificar mensagem do WebSocket:", err);
|
|
1230
1398
|
}
|
|
1231
1399
|
}
|
|
1400
|
+
/**
|
|
1401
|
+
* Sends data through the WebSocket connection.
|
|
1402
|
+
* @param data - The data to send.
|
|
1403
|
+
* @throws Will throw an error if the WebSocket is not connected.
|
|
1404
|
+
*/
|
|
1232
1405
|
send(data) {
|
|
1233
1406
|
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
|
|
1234
1407
|
throw new Error("WebSocket n\xE3o est\xE1 conectado.");
|
|
@@ -1239,6 +1412,9 @@ var WebSocketClient = class {
|
|
|
1239
1412
|
}
|
|
1240
1413
|
});
|
|
1241
1414
|
}
|
|
1415
|
+
/**
|
|
1416
|
+
* Closes the WebSocket connection manually.
|
|
1417
|
+
*/
|
|
1242
1418
|
close() {
|
|
1243
1419
|
if (this.ws) {
|
|
1244
1420
|
this.isClosedManually = true;
|
|
@@ -1262,21 +1438,29 @@ var AriClient = class {
|
|
|
1262
1438
|
this.playbacks = new Playbacks(this.baseClient);
|
|
1263
1439
|
this.sounds = new Sounds(this.baseClient);
|
|
1264
1440
|
this.asterisk = new Asterisk(this.baseClient);
|
|
1441
|
+
this.bridges = new Bridges(this.baseClient);
|
|
1265
1442
|
}
|
|
1266
1443
|
wsClient = null;
|
|
1267
1444
|
baseClient;
|
|
1268
1445
|
isReconnecting = false;
|
|
1446
|
+
eventEmitter = new EventEmitter3();
|
|
1269
1447
|
channels;
|
|
1270
1448
|
endpoints;
|
|
1271
1449
|
applications;
|
|
1272
1450
|
playbacks;
|
|
1273
1451
|
sounds;
|
|
1274
1452
|
asterisk;
|
|
1453
|
+
bridges;
|
|
1275
1454
|
/**
|
|
1276
1455
|
* Connects to the ARI WebSocket for a specific application.
|
|
1456
|
+
* This function establishes a WebSocket connection to the Asterisk ARI, sets up event listeners,
|
|
1457
|
+
* and ensures the application is registered. It uses an exponential backoff strategy for connection attempts.
|
|
1277
1458
|
*
|
|
1278
|
-
* @param app - The application
|
|
1279
|
-
* @
|
|
1459
|
+
* @param app - The name of the application to connect to. This is required and used to identify the application in ARI.
|
|
1460
|
+
* @param subscribedEvents - Optional array of WebSocketEventType to subscribe to specific events.
|
|
1461
|
+
* If not provided or empty, it subscribes to all events.
|
|
1462
|
+
* @returns A Promise that resolves when the WebSocket connection is successfully established and the application is registered.
|
|
1463
|
+
* @throws Error if the 'app' parameter is not provided, or if connection attempts fail after multiple retries.
|
|
1280
1464
|
*/
|
|
1281
1465
|
async connectWebSocket(app, subscribedEvents) {
|
|
1282
1466
|
if (!app) {
|
|
@@ -1304,6 +1488,11 @@ var AriClient = class {
|
|
|
1304
1488
|
return !this.wsClient?.isConnected();
|
|
1305
1489
|
}
|
|
1306
1490
|
};
|
|
1491
|
+
if (this.wsClient?.isConnected()) {
|
|
1492
|
+
console.log("WebSocket j\xE1 conectado. Removendo listeners antigos...");
|
|
1493
|
+
this.wsClient.removeAllListeners();
|
|
1494
|
+
this.wsClient.close();
|
|
1495
|
+
}
|
|
1307
1496
|
this.wsClient = new WebSocketClient(wsUrl);
|
|
1308
1497
|
try {
|
|
1309
1498
|
await (0, import_exponential_backoff.backOff)(async () => {
|
|
@@ -1311,6 +1500,7 @@ var AriClient = class {
|
|
|
1311
1500
|
throw new Error("WebSocketClient instance is null.");
|
|
1312
1501
|
}
|
|
1313
1502
|
await this.wsClient.connect();
|
|
1503
|
+
this.integrateWebSocketEvents();
|
|
1314
1504
|
console.log(`WebSocket conectado para o app: ${app}`);
|
|
1315
1505
|
await this.ensureAppRegistered(app);
|
|
1316
1506
|
}, backoffOptions);
|
|
@@ -1324,6 +1514,103 @@ var AriClient = class {
|
|
|
1324
1514
|
this.isReconnecting = false;
|
|
1325
1515
|
}
|
|
1326
1516
|
}
|
|
1517
|
+
/**
|
|
1518
|
+
* Integrates WebSocket events with playback listeners.
|
|
1519
|
+
*/
|
|
1520
|
+
integrateWebSocketEvents() {
|
|
1521
|
+
if (!this.wsClient) {
|
|
1522
|
+
throw new Error("WebSocket client n\xE3o est\xE1 conectado.");
|
|
1523
|
+
}
|
|
1524
|
+
const eventHandlers = {
|
|
1525
|
+
PlaybackFinished: (data) => {
|
|
1526
|
+
if ("playbackId" in data) {
|
|
1527
|
+
this.playbacks.emitPlaybackEvent("PlaybackFinished", data);
|
|
1528
|
+
}
|
|
1529
|
+
this.emitGlobalEvent(data);
|
|
1530
|
+
},
|
|
1531
|
+
ChannelStateChange: (data) => {
|
|
1532
|
+
if ("channel" in data) {
|
|
1533
|
+
console.log("Estado do canal alterado:", data.channel);
|
|
1534
|
+
}
|
|
1535
|
+
this.emitGlobalEvent(data);
|
|
1536
|
+
},
|
|
1537
|
+
BridgeDestroyed: (data) => {
|
|
1538
|
+
if ("bridge" in data) {
|
|
1539
|
+
console.log("Bridge destru\xEDda:", data.bridge);
|
|
1540
|
+
}
|
|
1541
|
+
this.emitGlobalEvent(data);
|
|
1542
|
+
},
|
|
1543
|
+
// Adicione mais eventos conforme necessário
|
|
1544
|
+
// Exemplo:
|
|
1545
|
+
ChannelDtmfReceived: (data) => {
|
|
1546
|
+
if ("channel" in data) {
|
|
1547
|
+
console.log("DTMF recebido no canal:", data.channel);
|
|
1548
|
+
}
|
|
1549
|
+
this.emitGlobalEvent(data);
|
|
1550
|
+
}
|
|
1551
|
+
};
|
|
1552
|
+
for (const [eventType, handler] of Object.entries(eventHandlers)) {
|
|
1553
|
+
if (handler) {
|
|
1554
|
+
this.wsClient.on(eventType, handler);
|
|
1555
|
+
}
|
|
1556
|
+
}
|
|
1557
|
+
console.log("Todos os eventos do WebSocket foram registrados.");
|
|
1558
|
+
}
|
|
1559
|
+
/**
|
|
1560
|
+
* Registra um listener para eventos globais.
|
|
1561
|
+
* @param callback - A função a ser executada quando um evento global for recebido.
|
|
1562
|
+
*/
|
|
1563
|
+
onGlobalEvent(callback) {
|
|
1564
|
+
this.eventEmitter.on("globalEvent", callback);
|
|
1565
|
+
}
|
|
1566
|
+
/**
|
|
1567
|
+
* Remove um listener para eventos globais.
|
|
1568
|
+
* @param callback - A função a ser removida dos eventos globais.
|
|
1569
|
+
*/
|
|
1570
|
+
offGlobalEvent(callback) {
|
|
1571
|
+
this.eventEmitter.off("globalEvent", callback);
|
|
1572
|
+
}
|
|
1573
|
+
/**
|
|
1574
|
+
* Emite um evento global.
|
|
1575
|
+
* @param data - Os dados do evento a serem emitidos.
|
|
1576
|
+
*/
|
|
1577
|
+
emitGlobalEvent(data) {
|
|
1578
|
+
this.eventEmitter.emit("globalEvent", data);
|
|
1579
|
+
}
|
|
1580
|
+
/**
|
|
1581
|
+
* Unregisters a listener for playback events.
|
|
1582
|
+
*
|
|
1583
|
+
* This method removes a specific listener registered for a playback event type,
|
|
1584
|
+
* ensuring that no further notifications are sent to the callback function.
|
|
1585
|
+
*
|
|
1586
|
+
* @param eventType - The type of event to stop listening for.
|
|
1587
|
+
* @param playbackId - The unique ID of the playback associated with the listener.
|
|
1588
|
+
* @param callback - The callback function to remove from the listener.
|
|
1589
|
+
*/
|
|
1590
|
+
unregisterPlaybackListener(eventType, playbackId, callback) {
|
|
1591
|
+
this.playbacks.unregisterListener(eventType, playbackId, callback);
|
|
1592
|
+
}
|
|
1593
|
+
/**
|
|
1594
|
+
* Registers a listener for playback events.
|
|
1595
|
+
* The listener is triggered for events such as "PlaybackFinished".
|
|
1596
|
+
*
|
|
1597
|
+
* @param eventType - The type of event to listen for.
|
|
1598
|
+
* @param playbackId - The ID of the playback to associate with this listener.
|
|
1599
|
+
* @param callback - The callback function to execute when the event occurs.
|
|
1600
|
+
*/
|
|
1601
|
+
registerPlaybackListener(eventType, playbackId, callback) {
|
|
1602
|
+
this.playbacks.registerListener(eventType, playbackId, callback);
|
|
1603
|
+
}
|
|
1604
|
+
/**
|
|
1605
|
+
* Checks if a listener is already registered for a specific event and playback.
|
|
1606
|
+
*
|
|
1607
|
+
* @param eventType - The type of event to check.
|
|
1608
|
+
* @param playbackId - The playback ID associated with the listener.
|
|
1609
|
+
* @returns True if a listener is already registered, false otherwise.
|
|
1610
|
+
*/
|
|
1611
|
+
isPlaybackListenerRegistered(eventType, playbackId) {
|
|
1612
|
+
return this.playbacks.isListenerRegistered(eventType, playbackId);
|
|
1613
|
+
}
|
|
1327
1614
|
/**
|
|
1328
1615
|
* Ensures the ARI application is registered.
|
|
1329
1616
|
*
|
|
@@ -1355,23 +1642,72 @@ var AriClient = class {
|
|
|
1355
1642
|
return this.wsClient ? this.wsClient.isConnected() : false;
|
|
1356
1643
|
}
|
|
1357
1644
|
/**
|
|
1358
|
-
* Registers a
|
|
1359
|
-
*
|
|
1360
|
-
*
|
|
1645
|
+
* Registers a listener for WebSocket events.
|
|
1646
|
+
*
|
|
1647
|
+
* This function allows you to attach a callback function to a specific WebSocket event type.
|
|
1648
|
+
* The callback will be executed whenever an event of the specified type is received.
|
|
1649
|
+
*
|
|
1650
|
+
* @template T - The type of WebSocket event, extending WebSocketEvent["type"].
|
|
1651
|
+
* @param {T} event - The type of WebSocket event to listen for.
|
|
1652
|
+
* @param {(data: Extract<WebSocketEvent, { type: T }>) => void} callback - The function to be called when the event is received.
|
|
1653
|
+
* The callback receives the event data as its parameter.
|
|
1654
|
+
* @throws {Error} Throws an error if the WebSocket client is not connected when this method is called.
|
|
1655
|
+
* @returns {void} This function doesn't return a value.
|
|
1656
|
+
*/
|
|
1657
|
+
onWebSocketEvent(event, callback) {
|
|
1658
|
+
if (!this.wsClient) {
|
|
1659
|
+
throw new Error("WebSocket n\xE3o est\xE1 conectado.");
|
|
1660
|
+
}
|
|
1661
|
+
this.wsClient.on(event, callback);
|
|
1662
|
+
}
|
|
1663
|
+
/**
|
|
1664
|
+
* Removes a specific listener for WebSocket events.
|
|
1361
1665
|
*
|
|
1362
|
-
*
|
|
1363
|
-
*
|
|
1364
|
-
*
|
|
1365
|
-
* @
|
|
1666
|
+
* This function unregisters a previously registered callback function for a specific WebSocket event type.
|
|
1667
|
+
* It's useful for removing event handlers when they are no longer needed or for cleaning up resources.
|
|
1668
|
+
*
|
|
1669
|
+
* @template T - The type of WebSocket event, extending WebSocketEvent["type"].
|
|
1670
|
+
* @param {T} event - The type of WebSocket event to remove the listener from.
|
|
1671
|
+
* @param {(data: Extract<WebSocketEvent, { type: T }>) => void} callback - The callback function to be removed.
|
|
1672
|
+
* This should be the same function reference that was used when adding the event listener.
|
|
1673
|
+
* @throws {Error} Throws an error if the WebSocket client is not connected when this method is called.
|
|
1674
|
+
* @returns {void} This function doesn't return a value.
|
|
1675
|
+
*/
|
|
1676
|
+
offWebSocketEvent(event, callback) {
|
|
1677
|
+
if (!this.wsClient) {
|
|
1678
|
+
throw new Error("WebSocket n\xE3o est\xE1 conectado.");
|
|
1679
|
+
}
|
|
1680
|
+
this.wsClient.off(event, callback);
|
|
1681
|
+
}
|
|
1682
|
+
/**
|
|
1683
|
+
* Removes all listeners for a specific WebSocket event type.
|
|
1684
|
+
*
|
|
1685
|
+
* This function removes all event listeners that have been registered for a particular
|
|
1686
|
+
* WebSocket event type. It's useful for cleaning up event listeners when they are no
|
|
1687
|
+
* longer needed or before re-registering new listeners.
|
|
1688
|
+
*
|
|
1689
|
+
* @param event - The type of WebSocket event for which all listeners should be removed.
|
|
1690
|
+
* This should be a string identifying the event type (e.g., 'message', 'close', etc.).
|
|
1691
|
+
*
|
|
1692
|
+
* @throws {Error} Throws an error if the WebSocket client is not connected when this method is called.
|
|
1693
|
+
*
|
|
1694
|
+
* @returns {void} This function doesn't return a value.
|
|
1366
1695
|
*/
|
|
1367
|
-
|
|
1696
|
+
removeAllWebSocketListeners(event) {
|
|
1368
1697
|
if (!this.wsClient) {
|
|
1369
|
-
throw new Error("WebSocket
|
|
1698
|
+
throw new Error("WebSocket n\xE3o est\xE1 conectado.");
|
|
1370
1699
|
}
|
|
1371
|
-
this.wsClient.
|
|
1700
|
+
this.wsClient.removeAllListeners(event);
|
|
1372
1701
|
}
|
|
1373
1702
|
/**
|
|
1374
|
-
* Closes the WebSocket connection.
|
|
1703
|
+
* Closes the WebSocket connection and removes all associated listeners.
|
|
1704
|
+
*
|
|
1705
|
+
* This function terminates the active WebSocket connection if one exists,
|
|
1706
|
+
* and cleans up by removing all event listeners attached to it. After calling
|
|
1707
|
+
* this function, the WebSocket client will be set to null, effectively
|
|
1708
|
+
* ending the connection and preparing for potential future connections.
|
|
1709
|
+
*
|
|
1710
|
+
* @returns {void} This function doesn't return a value.
|
|
1375
1711
|
*/
|
|
1376
1712
|
closeWebSocket() {
|
|
1377
1713
|
if (this.wsClient) {
|
|
@@ -1783,13 +2119,22 @@ var AriClient = class {
|
|
|
1783
2119
|
}
|
|
1784
2120
|
/**
|
|
1785
2121
|
* Controls a specific playback in the Asterisk server.
|
|
2122
|
+
* This function allows manipulation of an ongoing playback, such as pausing, resuming, or skipping.
|
|
1786
2123
|
*
|
|
1787
2124
|
* @param playbackId - The unique identifier of the playback to control.
|
|
2125
|
+
* This should be a string that uniquely identifies the playback in the Asterisk system.
|
|
1788
2126
|
* @param controlRequest - An object containing the control operation details.
|
|
2127
|
+
* This object should conform to the PlaybackControlRequest interface,
|
|
2128
|
+
* which includes an 'operation' property specifying the control action to perform.
|
|
1789
2129
|
* @returns A Promise that resolves when the control operation is successfully executed.
|
|
2130
|
+
* The promise resolves to void, indicating no specific return value.
|
|
2131
|
+
* If an error occurs during the operation, the promise will be rejected with an error object.
|
|
2132
|
+
* @throws Will throw an error if the playback control operation fails, e.g., if the playback doesn't exist
|
|
2133
|
+
* or the requested operation is invalid.
|
|
1790
2134
|
*/
|
|
1791
2135
|
async controlPlayback(playbackId, controlRequest) {
|
|
1792
|
-
|
|
2136
|
+
const { operation } = controlRequest;
|
|
2137
|
+
return this.playbacks.control(playbackId, operation);
|
|
1793
2138
|
}
|
|
1794
2139
|
/**
|
|
1795
2140
|
* Stops a specific playback in the Asterisk server.
|
|
@@ -1903,6 +2248,7 @@ export {
|
|
|
1903
2248
|
Applications,
|
|
1904
2249
|
AriClient,
|
|
1905
2250
|
Asterisk,
|
|
2251
|
+
Bridges,
|
|
1906
2252
|
Channels,
|
|
1907
2253
|
Endpoints,
|
|
1908
2254
|
Playbacks,
|