From 3109dbe41bbd45f51abeb675dae5ade77582fc5c Mon Sep 17 00:00:00 2001 From: Matt Nemitz Date: Mon, 4 Sep 2023 13:14:32 +0100 Subject: [PATCH 1/2] Allow Blob to be passed directly when sending audio buffer --- src/realtime/browser.ts | 11 +++-------- src/realtime/node.ts | 24 +++++++++++------------- src/types/socket-interface.ts | 2 +- 3 files changed, 15 insertions(+), 22 deletions(-) diff --git a/src/realtime/browser.ts b/src/realtime/browser.ts index e948151..a510a8f 100644 --- a/src/realtime/browser.ts +++ b/src/realtime/browser.ts @@ -26,11 +26,7 @@ export class WebSocketWrapper implements ISocketWrapper { throw new Error('window is undefined - are you running in a browser?'); } - async connect( - runtimeURL: string, - authToken?: string, - appId?: string, - ): Promise { + async connect(runtimeURL: string, authToken?: string, appId?: string): Promise { const url = addQueryParamsToUrl(runtimeURL, { jwt: authToken, [SM_SDK_PARAM_NAME]: getSmSDKVersion(), @@ -68,7 +64,7 @@ export class WebSocketWrapper implements ISocketWrapper { }); } - sendAudioBuffer(buffer: ArrayBufferLike): void { + sendAudioBuffer(buffer: ArrayBufferLike | Blob): void { if (this.socket && this.isOpen()) { this.socket.send(buffer); } else console.error('Tried to send audio when socket was closed'); @@ -77,8 +73,7 @@ export class WebSocketWrapper implements ISocketWrapper { sendMessage(message: string): void { if (this.socket && this.isOpen()) { this.socket.send(message); - } else - console.error('Tried to send message when socket was closed', message); + } else console.error('Tried to send message when socket was closed', message); } isOpen(): boolean { diff --git a/src/realtime/node.ts b/src/realtime/node.ts index 021ed86..cff83c6 100644 --- a/src/realtime/node.ts +++ b/src/realtime/node.ts @@ -27,19 +27,14 @@ export class NodeWebSocketWrapper implements ISocketWrapper { throw new Error('process is undefined - are you running in node?'); } - async connect( - runtimeURL: string, - authToken?: string, - appId?: string, - ): Promise { + async connect(runtimeURL: string, authToken?: string, appId?: string): Promise { const url = addQueryParamsToUrl(runtimeURL, { [SM_SDK_PARAM_NAME]: getSmSDKVersion(), [SM_APP_PARAM_NAME]: appId, }); try { let options: ClientOptions | ClientRequestArgs | undefined; - if (authToken) - options = { headers: { Authorization: `Bearer ${authToken}` } }; + if (authToken) options = { headers: { Authorization: `Bearer ${authToken}` } }; this.socket = new WebSocket(url, { perMessageDeflate: false, ...options, @@ -71,17 +66,21 @@ export class NodeWebSocketWrapper implements ISocketWrapper { }); } - sendAudioBuffer(buffer: ArrayBufferLike): void { + sendAudioBuffer(data: ArrayBufferLike | Blob): void { if (this.socket && this.isOpen()) { - this.socket.send(buffer); + if (data instanceof Blob) { + // NOTE: Maybe we should add a log message about this potentially being poorer performance in Node + data.arrayBuffer().then((buf) => this.socket?.send(buf)); + } else { + this.socket.send(data); + } } else console.error('tried to send audio when socket was closed'); } sendMessage(message: string): void { if (this.socket && this.isOpen()) { this.socket.send(message); - } else - console.error('tried to send message when socket was closed', message); + } else console.error('tried to send message when socket was closed', message); } isOpen(): boolean { @@ -111,7 +110,6 @@ export class NodeWebSocketWrapper implements ISocketWrapper { private handleSocketMessage = (message: MessageEvent): void => { if (message.data) this.onMessage?.(JSON.parse(message.data.toString())); - if (message instanceof Buffer) - this.onMessage?.(JSON.parse(message.toString())); + if (message instanceof Buffer) this.onMessage?.(JSON.parse(message.toString())); }; } diff --git a/src/types/socket-interface.ts b/src/types/socket-interface.ts index d85a46f..888757e 100644 --- a/src/types/socket-interface.ts +++ b/src/types/socket-interface.ts @@ -6,7 +6,7 @@ export interface ISocketWrapper { onDisconnect?: (event: CloseEvent) => void; connect(url: string, authToken?: string, appId?: string): Promise; disconnect(): Promise; - sendAudioBuffer(buffer: ArrayBufferLike): void; + sendAudioBuffer(buffer: ArrayBufferLike | Blob): void; sendMessage(message: string): void; isOpen(): boolean; } From c9e4e775006b3442cf2c4cf3d6750a7d18faa31f Mon Sep 17 00:00:00 2001 From: Matt Nemitz Date: Tue, 5 Sep 2023 10:06:55 +0100 Subject: [PATCH 2/2] Format --- src/realtime/browser.ts | 9 +++++++-- src/realtime/node.ts | 15 +++++++++++---- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/realtime/browser.ts b/src/realtime/browser.ts index a510a8f..6d21daf 100644 --- a/src/realtime/browser.ts +++ b/src/realtime/browser.ts @@ -26,7 +26,11 @@ export class WebSocketWrapper implements ISocketWrapper { throw new Error('window is undefined - are you running in a browser?'); } - async connect(runtimeURL: string, authToken?: string, appId?: string): Promise { + async connect( + runtimeURL: string, + authToken?: string, + appId?: string, + ): Promise { const url = addQueryParamsToUrl(runtimeURL, { jwt: authToken, [SM_SDK_PARAM_NAME]: getSmSDKVersion(), @@ -73,7 +77,8 @@ export class WebSocketWrapper implements ISocketWrapper { sendMessage(message: string): void { if (this.socket && this.isOpen()) { this.socket.send(message); - } else console.error('Tried to send message when socket was closed', message); + } else + console.error('Tried to send message when socket was closed', message); } isOpen(): boolean { diff --git a/src/realtime/node.ts b/src/realtime/node.ts index cff83c6..977222c 100644 --- a/src/realtime/node.ts +++ b/src/realtime/node.ts @@ -27,14 +27,19 @@ export class NodeWebSocketWrapper implements ISocketWrapper { throw new Error('process is undefined - are you running in node?'); } - async connect(runtimeURL: string, authToken?: string, appId?: string): Promise { + async connect( + runtimeURL: string, + authToken?: string, + appId?: string, + ): Promise { const url = addQueryParamsToUrl(runtimeURL, { [SM_SDK_PARAM_NAME]: getSmSDKVersion(), [SM_APP_PARAM_NAME]: appId, }); try { let options: ClientOptions | ClientRequestArgs | undefined; - if (authToken) options = { headers: { Authorization: `Bearer ${authToken}` } }; + if (authToken) + options = { headers: { Authorization: `Bearer ${authToken}` } }; this.socket = new WebSocket(url, { perMessageDeflate: false, ...options, @@ -80,7 +85,8 @@ export class NodeWebSocketWrapper implements ISocketWrapper { sendMessage(message: string): void { if (this.socket && this.isOpen()) { this.socket.send(message); - } else console.error('tried to send message when socket was closed', message); + } else + console.error('tried to send message when socket was closed', message); } isOpen(): boolean { @@ -110,6 +116,7 @@ export class NodeWebSocketWrapper implements ISocketWrapper { private handleSocketMessage = (message: MessageEvent): void => { if (message.data) this.onMessage?.(JSON.parse(message.data.toString())); - if (message instanceof Buffer) this.onMessage?.(JSON.parse(message.toString())); + if (message instanceof Buffer) + this.onMessage?.(JSON.parse(message.toString())); }; }