From 95cfb043de1a84dd0108681476c74f72638a3cd8 Mon Sep 17 00:00:00 2001 From: ao-anam Date: Mon, 30 Jun 2025 17:01:00 +0100 Subject: [PATCH] feat: improved connection closed error callbacks --- src/AnamClient.ts | 5 +++++ src/lib/constants.ts | 14 +++++++------- src/modules/PublicEventEmitter.ts | 4 +++- src/modules/SignallingClient.ts | 6 +++--- src/modules/StreamingClient.ts | 13 +++++-------- src/types/events/public/EventCallbacks.ts | 6 +++++- 6 files changed, 28 insertions(+), 20 deletions(-) diff --git a/src/AnamClient.ts b/src/AnamClient.ts index b290bee..438a032 100644 --- a/src/AnamClient.ts +++ b/src/AnamClient.ts @@ -26,6 +26,7 @@ import { } from './types'; import { TalkMessageStream } from './types/TalkMessageStream'; import { Buffer } from 'buffer'; +import { ConnectionClosedCode } from './lib/constants'; export default class AnamClient { private publicEventEmitter: PublicEventEmitter; private internalEventEmitter: InternalEventEmitter; @@ -410,6 +411,10 @@ export default class AnamClient { public async stopStreaming(): Promise { if (this.streamingClient) { + this.publicEventEmitter.emit( + AnamEvent.CONNECTION_CLOSED, + ConnectionClosedCode.NORMAL, + ); await this.streamingClient.stopConnection(); this.streamingClient = null; this.sessionId = null; diff --git a/src/lib/constants.ts b/src/lib/constants.ts index 0144f76..2d4132f 100644 --- a/src/lib/constants.ts +++ b/src/lib/constants.ts @@ -7,13 +7,13 @@ export const DEFAULT_API_BASE_URL = 'https://api.anam.ai'; export const DEFAULT_API_VERSION = '/v1'; // include the leading slash // Connection closed codes -export const CONNECTION_CLOSED_CODE_NORMAL = 'CONNECTION_CLOSED_CODE_NORMAL'; -export const CONNECTION_CLOSED_CODE_MICROPHONE_PERMISSION_DENIED = - 'CONNECTION_CLOSED_CODE_MICROPHONE_PERMISSION_DENIED'; -export const CONNECTION_CLOSED_CODE_SIGNALLING_CLIENT_CONNECTION_FAILURE = - 'CONNECTION_CLOSED_CODE_SIGNALLING_CLIENT_CONNECTION_FAILURE'; -export const CONNECTION_CLOSED_CODE_WEBRTC_FAILURE = - 'CONNECTION_CLOSED_CODE_WEBRTC_FAILURE'; +export enum ConnectionClosedCode { + NORMAL = 'CONNECTION_CLOSED_CODE_NORMAL', + MICROPHONE_PERMISSION_DENIED = 'CONNECTION_CLOSED_CODE_MICROPHONE_PERMISSION_DENIED', + SIGNALLING_CLIENT_CONNECTION_FAILURE = 'CONNECTION_CLOSED_CODE_SIGNALLING_CLIENT_CONNECTION_FAILURE', + WEBRTC_FAILURE = 'CONNECTION_CLOSED_CODE_WEBRTC_FAILURE', + SERVER_CLOSED_CONNECTION = 'CONNECTION_CLOSED_CODE_SERVER_CLOSED_CONNECTION', +} export const CLIENT_METADATA = { client: 'js-sdk', diff --git a/src/modules/PublicEventEmitter.ts b/src/modules/PublicEventEmitter.ts index 354f922..c267c71 100644 --- a/src/modules/PublicEventEmitter.ts +++ b/src/modules/PublicEventEmitter.ts @@ -43,9 +43,11 @@ export class PublicEventEmitter { } if (event === AnamEvent.CONNECTION_CLOSED) { + const [closeCode, details] = args; sendClientMetric( ClientMetricMeasurement.CLIENT_METRIC_MEASUREMENT_CONNECTION_CLOSED, - args[0] as string, + closeCode as string, + details ? { details: details as string } : undefined, ); } diff --git a/src/modules/SignallingClient.ts b/src/modules/SignallingClient.ts index b417627..c54fd0a 100644 --- a/src/modules/SignallingClient.ts +++ b/src/modules/SignallingClient.ts @@ -1,4 +1,4 @@ -import { CONNECTION_CLOSED_CODE_SIGNALLING_CLIENT_CONNECTION_FAILURE } from '../lib/constants'; +import { ConnectionClosedCode } from '../lib/constants'; import { InternalEventEmitter, PublicEventEmitter } from '.'; import { AnamEvent, @@ -146,7 +146,7 @@ export class SignallingClient { console.error('SignallingClient - onOpen: error in onOpen', e); this.publicEventEmitter.emit( AnamEvent.CONNECTION_CLOSED, - CONNECTION_CLOSED_CODE_SIGNALLING_CLIENT_CONNECTION_FAILURE, + ConnectionClosedCode.SIGNALLING_CLIENT_CONNECTION_FAILURE, ); } } @@ -168,7 +168,7 @@ export class SignallingClient { } this.publicEventEmitter.emit( AnamEvent.CONNECTION_CLOSED, - CONNECTION_CLOSED_CODE_SIGNALLING_CLIENT_CONNECTION_FAILURE, + ConnectionClosedCode.SIGNALLING_CLIENT_CONNECTION_FAILURE, ); } } diff --git a/src/modules/StreamingClient.ts b/src/modules/StreamingClient.ts index a9df6c7..dd1eabe 100644 --- a/src/modules/StreamingClient.ts +++ b/src/modules/StreamingClient.ts @@ -1,8 +1,4 @@ -import { - CONNECTION_CLOSED_CODE_MICROPHONE_PERMISSION_DENIED, - CONNECTION_CLOSED_CODE_NORMAL, - CONNECTION_CLOSED_CODE_WEBRTC_FAILURE, -} from '../lib/constants'; +import { ConnectionClosedCode } from '../lib/constants'; import { EngineApiRestClient, InternalEventEmitter, @@ -353,7 +349,8 @@ export class StreamingClient { console.log('StreamingClient - onSignalMessage: reason', reason); this.publicEventEmitter.emit( AnamEvent.CONNECTION_CLOSED, - CONNECTION_CLOSED_CODE_NORMAL, + ConnectionClosedCode.SERVER_CLOSED_CONNECTION, + reason, ); // close the peer connection this.shutdown(); @@ -440,12 +437,12 @@ export class StreamingClient { if (err.name === 'NotAllowedError' && err.message === 'Permission denied') { this.publicEventEmitter.emit( AnamEvent.CONNECTION_CLOSED, - CONNECTION_CLOSED_CODE_MICROPHONE_PERMISSION_DENIED, + ConnectionClosedCode.MICROPHONE_PERMISSION_DENIED, ); } else { this.publicEventEmitter.emit( AnamEvent.CONNECTION_CLOSED, - CONNECTION_CLOSED_CODE_WEBRTC_FAILURE, + ConnectionClosedCode.WEBRTC_FAILURE, ); } diff --git a/src/types/events/public/EventCallbacks.ts b/src/types/events/public/EventCallbacks.ts index 74b73c3..664f4ef 100644 --- a/src/types/events/public/EventCallbacks.ts +++ b/src/types/events/public/EventCallbacks.ts @@ -1,3 +1,4 @@ +import { ConnectionClosedCode } from '../../../lib/constants'; import { Message, MessageStreamEvent, AnamEvent } from '../../index'; export type EventCallbacks = { @@ -6,7 +7,10 @@ export type EventCallbacks = { messageEvent: MessageStreamEvent, ) => void; [AnamEvent.CONNECTION_ESTABLISHED]: () => void; - [AnamEvent.CONNECTION_CLOSED]: (reason: string) => void; + [AnamEvent.CONNECTION_CLOSED]: ( + reason: ConnectionClosedCode, + details?: string, + ) => void; [AnamEvent.INPUT_AUDIO_STREAM_STARTED]: (audioStream: MediaStream) => void; [AnamEvent.VIDEO_STREAM_STARTED]: (videoStream: MediaStream) => void; [AnamEvent.VIDEO_PLAY_STARTED]: () => void;