@ipcom/asterisk-ari 0.0.76 → 0.0.77

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.
@@ -49,6 +49,8 @@ var import_axios = __toESM(require("axios"), 1);
49
49
  var BaseClient = class {
50
50
  client;
51
51
  eventEmitter = new import_events.EventEmitter();
52
+ wsClients = /* @__PURE__ */ new Map();
53
+ // Gerencia os WebSocket clients
52
54
  constructor(baseUrl, username, password) {
53
55
  this.client = import_axios.default.create({
54
56
  baseURL: baseUrl,
@@ -89,9 +91,46 @@ var BaseClient = class {
89
91
  const response = await this.client.delete(path);
90
92
  return response.data;
91
93
  }
94
+ // Gerenciamento de WebSocket clients
92
95
  /**
93
- * Escuta eventos do WebSocket.
96
+ * Adiciona um WebSocket client ao gerenciador.
97
+ * @param app - Nome do aplicativo ou contexto associado.
98
+ * @param wsClient - Instância do WebSocketClient.
94
99
  */
100
+ addWebSocketClient(app, wsClient) {
101
+ if (this.wsClients.has(app)) {
102
+ throw new Error(`J\xE1 existe um WebSocket client registrado para '${app}'`);
103
+ }
104
+ this.wsClients.set(app, wsClient);
105
+ console.log(`WebSocket client adicionado para '${app}'`);
106
+ }
107
+ /**
108
+ * Remove um WebSocket client do gerenciador.
109
+ * @param app - Nome do aplicativo ou contexto associado.
110
+ */
111
+ removeWebSocketClient(app) {
112
+ if (!this.wsClients.has(app)) {
113
+ throw new Error(`Nenhum WebSocket client encontrado para '${app}'`);
114
+ }
115
+ this.wsClients.delete(app);
116
+ console.log(`WebSocket client removido para '${app}'`);
117
+ }
118
+ /**
119
+ * Obtém todos os WebSocket clients.
120
+ * @returns Um Map contendo todos os WebSocket clients registrados.
121
+ */
122
+ getWebSocketClients() {
123
+ return this.wsClients;
124
+ }
125
+ /**
126
+ * Obtém um WebSocket client específico.
127
+ * @param app - Nome do aplicativo ou contexto associado.
128
+ * @returns O WebSocket client correspondente.
129
+ */
130
+ getWebSocketClient(app) {
131
+ return this.wsClients.get(app);
132
+ }
133
+ // Gerenciamento de eventos WebSocket
95
134
  onWebSocketEvent(callback) {
96
135
  console.log("Registrando callback para eventos do WebSocket");
97
136
  this.eventEmitter.on("websocketEvent", (event) => {
@@ -99,9 +138,6 @@ var BaseClient = class {
99
138
  callback(event);
100
139
  });
101
140
  }
102
- /**
103
- * Emite eventos do WebSocket.
104
- */
105
141
  emitWebSocketEvent(event) {
106
142
  console.log("Emitindo evento do WebSocket:", event);
107
143
  this.eventEmitter.emit("websocketEvent", event);
@@ -814,8 +850,21 @@ var PlaybackInstance = class extends import_events3.EventEmitter {
814
850
  this.baseClient = baseClient;
815
851
  this.playbackId = playbackId;
816
852
  this.id = playbackId || `playback-${Date.now()}`;
817
- this.baseClient.onWebSocketEvent((event) => {
818
- if (this.isPlaybackEvent(event) && event.playbackId === this.playbackId) {
853
+ const wsClients = this.baseClient.getWebSocketClients();
854
+ const wsClient = Array.from(wsClients.values()).find(
855
+ (client) => client.isConnected()
856
+ );
857
+ if (!wsClient) {
858
+ throw new Error(
859
+ `Nenhum WebSocket conectado dispon\xEDvel para o playback: ${this.id}`
860
+ );
861
+ }
862
+ wsClient.on("message", (event) => {
863
+ if (this.isPlaybackEvent(event)) {
864
+ console.log(
865
+ `Evento recebido no PlaybackInstance: ${event.type}`,
866
+ event
867
+ );
819
868
  this.emit(event.type, event);
820
869
  }
821
870
  });
@@ -826,7 +875,15 @@ var PlaybackInstance = class extends import_events3.EventEmitter {
826
875
  * Verifica se o evento é relacionado a um playback.
827
876
  */
828
877
  isPlaybackEvent(event) {
829
- return event && typeof event === "object" && "playbackId" in event;
878
+ const isPlaybackType = event?.type.startsWith("Playback");
879
+ const isPlaybackEvent = isPlaybackType && "playbackId" in event && event.playbackId === this.id;
880
+ if (!isPlaybackEvent) {
881
+ console.log(
882
+ `Evento ignorado no isPlaybackEvent: tipo=${event.type}, playback esperado=${this.id}, evento recebido=`,
883
+ event
884
+ );
885
+ }
886
+ return isPlaybackEvent;
830
887
  }
831
888
  /**
832
889
  * Obtém os detalhes do playback.
@@ -895,6 +952,12 @@ var Playbacks = class extends import_events3.EventEmitter {
895
952
  }
896
953
  });
897
954
  }
955
+ /**
956
+ * Obtém os clientes WebSocket disponíveis.
957
+ */
958
+ getWebSocketClients() {
959
+ return this.client.getWebSocketClients();
960
+ }
898
961
  /**
899
962
  * Verifica se o evento é relacionado a um playback.
900
963
  */