@ipcom/asterisk-ari 0.0.32 → 0.0.34
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
|
@@ -1676,6 +1676,8 @@ var AriClient = class {
|
|
|
1676
1676
|
baseClient;
|
|
1677
1677
|
isReconnecting = false;
|
|
1678
1678
|
eventEmitter = new EventEmitter4();
|
|
1679
|
+
wsConnections = /* @__PURE__ */ new Map();
|
|
1680
|
+
isWebSocketConnectedFlag = false;
|
|
1679
1681
|
channels;
|
|
1680
1682
|
endpoints;
|
|
1681
1683
|
applications;
|
|
@@ -1700,14 +1702,19 @@ var AriClient = class {
|
|
|
1700
1702
|
"The 'app' parameter is required to connect to the WebSocket."
|
|
1701
1703
|
);
|
|
1702
1704
|
}
|
|
1703
|
-
if (this.isReconnecting) {
|
|
1704
|
-
console.warn("Already attempting to reconnect. Skipping this attempt.");
|
|
1705
|
-
return;
|
|
1706
|
-
}
|
|
1707
|
-
this.isReconnecting = true;
|
|
1708
1705
|
const eventsParam = subscribedEvents && subscribedEvents.length > 0 ? `&event=${subscribedEvents.join(",")}` : "&subscribeAll=true";
|
|
1709
1706
|
const protocol = this.config.secure ? "wss" : "ws";
|
|
1710
|
-
const wsUrl = `${protocol}://${encodeURIComponent(
|
|
1707
|
+
const wsUrl = `${protocol}://${encodeURIComponent(
|
|
1708
|
+
this.config.username
|
|
1709
|
+
)}:${encodeURIComponent(this.config.password)}@${this.config.host}:${this.config.port}/ari/events?app=${app}${eventsParam}`;
|
|
1710
|
+
if (this.wsConnections.has(app)) {
|
|
1711
|
+
const existingWsClient = this.wsConnections.get(app);
|
|
1712
|
+
console.log(`WebSocket j\xE1 conectado para o app: ${app}. Reiniciando...`);
|
|
1713
|
+
existingWsClient?.removeAllListeners();
|
|
1714
|
+
existingWsClient?.close();
|
|
1715
|
+
}
|
|
1716
|
+
const wsClient = new WebSocketClient(wsUrl);
|
|
1717
|
+
this.wsConnections.set(app, wsClient);
|
|
1711
1718
|
const backoffOptions = {
|
|
1712
1719
|
delayFirstAttempt: false,
|
|
1713
1720
|
startingDelay: 1e3,
|
|
@@ -1717,77 +1724,87 @@ var AriClient = class {
|
|
|
1717
1724
|
jitter: "full",
|
|
1718
1725
|
retry: (error, attemptNumber) => {
|
|
1719
1726
|
console.warn(`Tentativa ${attemptNumber} falhou: ${error.message}`);
|
|
1720
|
-
return !
|
|
1727
|
+
return !wsClient.isConnected();
|
|
1721
1728
|
}
|
|
1722
1729
|
};
|
|
1723
|
-
if (this.wsClient?.isConnected()) {
|
|
1724
|
-
console.log("WebSocket j\xE1 conectado. Removendo listeners antigos...");
|
|
1725
|
-
this.wsClient.removeAllListeners();
|
|
1726
|
-
this.wsClient.close();
|
|
1727
|
-
}
|
|
1728
|
-
this.wsClient = new WebSocketClient(wsUrl);
|
|
1729
1730
|
try {
|
|
1730
1731
|
await (0, import_exponential_backoff.backOff)(async () => {
|
|
1731
1732
|
if (!this.wsClient) {
|
|
1732
1733
|
throw new Error("WebSocketClient instance is null.");
|
|
1733
1734
|
}
|
|
1734
1735
|
await this.wsClient.connect();
|
|
1735
|
-
this.
|
|
1736
|
+
this.isWebSocketConnectedFlag = true;
|
|
1737
|
+
this.integrateWebSocketEvents(app, this.wsClient);
|
|
1736
1738
|
console.log(`WebSocket conectado para o app: ${app}`);
|
|
1737
1739
|
await this.ensureAppRegistered(app);
|
|
1738
1740
|
}, backoffOptions);
|
|
1739
1741
|
} catch (err) {
|
|
1742
|
+
this.isWebSocketConnectedFlag = false;
|
|
1740
1743
|
console.error(
|
|
1741
1744
|
"N\xE3o foi poss\xEDvel conectar ao WebSocket ap\xF3s m\xFAltiplas tentativas:",
|
|
1742
1745
|
err
|
|
1743
1746
|
);
|
|
1744
1747
|
throw err;
|
|
1745
|
-
} finally {
|
|
1746
|
-
this.isReconnecting = false;
|
|
1747
1748
|
}
|
|
1748
1749
|
}
|
|
1749
1750
|
/**
|
|
1750
1751
|
* Integrates WebSocket events with playback listeners.
|
|
1751
1752
|
*/
|
|
1752
|
-
integrateWebSocketEvents() {
|
|
1753
|
-
if (!
|
|
1754
|
-
throw new Error(
|
|
1753
|
+
integrateWebSocketEvents(app, wsClient) {
|
|
1754
|
+
if (!wsClient) {
|
|
1755
|
+
throw new Error(
|
|
1756
|
+
`WebSocket client para o app '${app}' n\xE3o est\xE1 conectado.`
|
|
1757
|
+
);
|
|
1755
1758
|
}
|
|
1756
1759
|
const eventHandlers = {
|
|
1757
1760
|
PlaybackFinished: (data) => {
|
|
1758
1761
|
if ("playbackId" in data) {
|
|
1759
1762
|
this.playbacks.emitPlaybackEvent("PlaybackFinished", data);
|
|
1760
1763
|
}
|
|
1764
|
+
console.log(`[${app}] PlaybackFinished:`, data);
|
|
1761
1765
|
this.emitGlobalEvent(data);
|
|
1762
1766
|
},
|
|
1763
1767
|
ChannelStateChange: (data) => {
|
|
1764
1768
|
if ("channel" in data) {
|
|
1765
|
-
console.log(
|
|
1769
|
+
console.log(`[${app}] Estado do canal alterado:`, data.channel);
|
|
1770
|
+
this.channels.emitChannelEvent("ChannelStateChange", data);
|
|
1766
1771
|
}
|
|
1767
1772
|
this.emitGlobalEvent(data);
|
|
1768
1773
|
},
|
|
1769
1774
|
BridgeDestroyed: (data) => {
|
|
1770
1775
|
if ("bridge" in data) {
|
|
1771
|
-
console.log(
|
|
1776
|
+
console.log(`[${app}] Bridge destru\xEDda:`, data.bridge);
|
|
1772
1777
|
}
|
|
1773
1778
|
this.emitGlobalEvent(data);
|
|
1774
1779
|
},
|
|
1775
|
-
// Adicione mais eventos conforme necessário
|
|
1776
|
-
// Exemplo:
|
|
1777
1780
|
ChannelDtmfReceived: (data) => {
|
|
1778
1781
|
if ("channel" in data) {
|
|
1779
|
-
console.log(
|
|
1782
|
+
console.log(`[${app}] DTMF recebido no canal:`, data.channel);
|
|
1780
1783
|
this.channels.emitChannelEvent("ChannelDtmfReceived", data);
|
|
1781
1784
|
}
|
|
1782
1785
|
this.emitGlobalEvent(data);
|
|
1786
|
+
},
|
|
1787
|
+
// Adicione mais eventos conforme necessário
|
|
1788
|
+
EndpointStateChange: (data) => {
|
|
1789
|
+
if ("endpoint" in data) {
|
|
1790
|
+
console.log(`[${app}] Estado do endpoint alterado:`, data.endpoint);
|
|
1791
|
+
}
|
|
1792
|
+
this.emitGlobalEvent(data);
|
|
1793
|
+
},
|
|
1794
|
+
ChannelHangupRequest: (data) => {
|
|
1795
|
+
if ("channel" in data) {
|
|
1796
|
+
console.log(`[${app}] Requisi\xE7\xE3o de hangup no canal:`, data.channel);
|
|
1797
|
+
this.channels.emitChannelEvent("ChannelHangupRequest", data);
|
|
1798
|
+
}
|
|
1799
|
+
this.emitGlobalEvent(data);
|
|
1783
1800
|
}
|
|
1784
1801
|
};
|
|
1785
1802
|
for (const [eventType, handler] of Object.entries(eventHandlers)) {
|
|
1786
1803
|
if (handler) {
|
|
1787
|
-
|
|
1804
|
+
wsClient.on(eventType, handler);
|
|
1788
1805
|
}
|
|
1789
1806
|
}
|
|
1790
|
-
console.log(
|
|
1807
|
+
console.log(`[${app}] Todos os eventos do WebSocket foram registrados.`);
|
|
1791
1808
|
}
|
|
1792
1809
|
/**
|
|
1793
1810
|
* Registra um listener para eventos globais.
|
|
@@ -1887,9 +1904,12 @@ var AriClient = class {
|
|
|
1887
1904
|
* @throws {Error} Throws an error if the WebSocket client is not connected when this method is called.
|
|
1888
1905
|
* @returns {void} This function doesn't return a value.
|
|
1889
1906
|
*/
|
|
1890
|
-
onWebSocketEvent(event, callback) {
|
|
1907
|
+
async onWebSocketEvent(event, callback) {
|
|
1908
|
+
if (!this.isWebSocketConnectedFlag) {
|
|
1909
|
+
throw new Error("WebSocket ainda n\xE3o est\xE1 conectado.");
|
|
1910
|
+
}
|
|
1891
1911
|
if (!this.wsClient) {
|
|
1892
|
-
throw new Error("WebSocket n\xE3o est\xE1
|
|
1912
|
+
throw new Error("WebSocket n\xE3o est\xE1 configurado.");
|
|
1893
1913
|
}
|
|
1894
1914
|
this.wsClient.on(event, callback);
|
|
1895
1915
|
}
|