Skip to content

Commit

Permalink
feat: update AbstractLiveClient to handle binary data
Browse files Browse the repository at this point in the history
  • Loading branch information
SandraRodgers committed Jul 3, 2024
1 parent 9f8fd1e commit 54036a3
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 19 deletions.
5 changes: 5 additions & 0 deletions src/lib/enums/LiveTTSEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ export enum LiveTTSEvents {
Flushed = "Flushed",
Warning = "Warning",

/**
* Audio data event.
*/
Audio = "Audio",

/**
* Catch all for any other message event
*/
Expand Down
43 changes: 43 additions & 0 deletions src/packages/AbstractLiveClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { AbstractClient, noop } from "./AbstractClient";
import { CONNECTION_STATE, SOCKET_STATES } from "../lib/constants";
import type { DeepgramClientOptions, LiveSchema } from "../lib/types";
import type { WebSocket as WSWebSocket } from "ws";
import { LiveTTSEvents } from "../lib/enums";

/**
* Represents a constructor for a WebSocket-like object that can be used in the application.
Expand Down Expand Up @@ -258,6 +259,48 @@ export abstract class AbstractLiveClient extends AbstractClient {
* @abstract Requires subclasses to set up context aware event handlers.
*/
abstract setupConnection(): void;

/**
* Handles incoming messages from the WebSocket connection.
* @param event - The MessageEvent object representing the received message.
*/
protected handleMessage(event: MessageEvent): void {
if (typeof event.data === "string") {
try {
const data = JSON.parse(event.data);
this.handleTextMessage(data);
} catch (error) {
this.emit(LiveTTSEvents.Error, {
event,
message: "Unable to parse `data` as JSON.",
error,
});
}
} else if (event.data instanceof ArrayBuffer) {
this.handleBinaryMessage(event.data);
} else {
this.emit(LiveTTSEvents.Error, {
event,
message: "Received unknown data type.",
});
}
}

/**
* Handles text messages received from the WebSocket connection.
* @param data - The parsed JSON data.
*/
protected handleTextMessage(data: any): void {
// To be implemented by subclasses
}

/**
* Handles binary messages received from the WebSocket connection.
* @param data - The binary data.
*/
protected handleBinaryMessage(data: ArrayBuffer): void {
// To be implemented by subclasses
}
}

class WSWebSocketDummy {
Expand Down
44 changes: 25 additions & 19 deletions src/packages/SpeakLiveClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,29 +58,35 @@ export class SpeakLiveClient extends AbstractLiveClient {
};

this.conn.onmessage = (event: MessageEvent) => {
try {
const data: any = JSON.parse(event.data.toString());

if (data.type === LiveTTSEvents.Metadata) {
this.emit(LiveTTSEvents.Metadata, data);
} else if (data.type === LiveTTSEvents.Flushed) {
this.emit(LiveTTSEvents.Flushed, data);
} else if (data.type === "Warning") {
this.emit(LiveTTSEvents.Warning, data);
} else {
this.emit(LiveTTSEvents.Unhandled, data);
}
} catch (error) {
this.emit(LiveTTSEvents.Error, {
event,
message: "Unable to parse `data` as JSON.",
error,
});
}
this.handleMessage(event);
};
}
}

/**
* Handles text messages received from the WebSocket connection.
* @param data - The parsed JSON data.
*/
protected handleTextMessage(data: any): void {
if (data.type === LiveTTSEvents.Metadata) {
this.emit(LiveTTSEvents.Metadata, data);
} else if (data.type === LiveTTSEvents.Flushed) {
this.emit(LiveTTSEvents.Flushed, data);
} else if (data.type === LiveTTSEvents.Warning) {
this.emit(LiveTTSEvents.Warning, data);
} else {
this.emit(LiveTTSEvents.Unhandled, data);
}
}

/**
* Handles binary messages received from the WebSocket connection.
* @param data - The binary data.
*/
protected handleBinaryMessage(data: ArrayBuffer): void {
this.emit(LiveTTSEvents.Audio, data);
}

/**
* Sends a text input message to the server.
*
Expand Down

0 comments on commit 54036a3

Please sign in to comment.