Skip to content

Commit 9fea573

Browse files
authored
Merge pull request #130 from anam-org/feat/improved-closed-connection-errors
feat: improved connection closed error callbacks
2 parents 26cc04d + 95cfb04 commit 9fea573

File tree

6 files changed

+28
-20
lines changed

6 files changed

+28
-20
lines changed

src/AnamClient.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
} from './types';
2727
import { TalkMessageStream } from './types/TalkMessageStream';
2828
import { Buffer } from 'buffer';
29+
import { ConnectionClosedCode } from './lib/constants';
2930
export default class AnamClient {
3031
private publicEventEmitter: PublicEventEmitter;
3132
private internalEventEmitter: InternalEventEmitter;
@@ -410,6 +411,10 @@ export default class AnamClient {
410411

411412
public async stopStreaming(): Promise<void> {
412413
if (this.streamingClient) {
414+
this.publicEventEmitter.emit(
415+
AnamEvent.CONNECTION_CLOSED,
416+
ConnectionClosedCode.NORMAL,
417+
);
413418
await this.streamingClient.stopConnection();
414419
this.streamingClient = null;
415420
this.sessionId = null;

src/lib/constants.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ export const DEFAULT_API_BASE_URL = 'https://api.anam.ai';
77
export const DEFAULT_API_VERSION = '/v1'; // include the leading slash
88

99
// Connection closed codes
10-
export const CONNECTION_CLOSED_CODE_NORMAL = 'CONNECTION_CLOSED_CODE_NORMAL';
11-
export const CONNECTION_CLOSED_CODE_MICROPHONE_PERMISSION_DENIED =
12-
'CONNECTION_CLOSED_CODE_MICROPHONE_PERMISSION_DENIED';
13-
export const CONNECTION_CLOSED_CODE_SIGNALLING_CLIENT_CONNECTION_FAILURE =
14-
'CONNECTION_CLOSED_CODE_SIGNALLING_CLIENT_CONNECTION_FAILURE';
15-
export const CONNECTION_CLOSED_CODE_WEBRTC_FAILURE =
16-
'CONNECTION_CLOSED_CODE_WEBRTC_FAILURE';
10+
export enum ConnectionClosedCode {
11+
NORMAL = 'CONNECTION_CLOSED_CODE_NORMAL',
12+
MICROPHONE_PERMISSION_DENIED = 'CONNECTION_CLOSED_CODE_MICROPHONE_PERMISSION_DENIED',
13+
SIGNALLING_CLIENT_CONNECTION_FAILURE = 'CONNECTION_CLOSED_CODE_SIGNALLING_CLIENT_CONNECTION_FAILURE',
14+
WEBRTC_FAILURE = 'CONNECTION_CLOSED_CODE_WEBRTC_FAILURE',
15+
SERVER_CLOSED_CONNECTION = 'CONNECTION_CLOSED_CODE_SERVER_CLOSED_CONNECTION',
16+
}
1717

1818
export const CLIENT_METADATA = {
1919
client: 'js-sdk',

src/modules/PublicEventEmitter.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,11 @@ export class PublicEventEmitter {
4343
}
4444

4545
if (event === AnamEvent.CONNECTION_CLOSED) {
46+
const [closeCode, details] = args;
4647
sendClientMetric(
4748
ClientMetricMeasurement.CLIENT_METRIC_MEASUREMENT_CONNECTION_CLOSED,
48-
args[0] as string,
49+
closeCode as string,
50+
details ? { details: details as string } : undefined,
4951
);
5052
}
5153

src/modules/SignallingClient.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CONNECTION_CLOSED_CODE_SIGNALLING_CLIENT_CONNECTION_FAILURE } from '../lib/constants';
1+
import { ConnectionClosedCode } from '../lib/constants';
22
import { InternalEventEmitter, PublicEventEmitter } from '.';
33
import {
44
AnamEvent,
@@ -146,7 +146,7 @@ export class SignallingClient {
146146
console.error('SignallingClient - onOpen: error in onOpen', e);
147147
this.publicEventEmitter.emit(
148148
AnamEvent.CONNECTION_CLOSED,
149-
CONNECTION_CLOSED_CODE_SIGNALLING_CLIENT_CONNECTION_FAILURE,
149+
ConnectionClosedCode.SIGNALLING_CLIENT_CONNECTION_FAILURE,
150150
);
151151
}
152152
}
@@ -168,7 +168,7 @@ export class SignallingClient {
168168
}
169169
this.publicEventEmitter.emit(
170170
AnamEvent.CONNECTION_CLOSED,
171-
CONNECTION_CLOSED_CODE_SIGNALLING_CLIENT_CONNECTION_FAILURE,
171+
ConnectionClosedCode.SIGNALLING_CLIENT_CONNECTION_FAILURE,
172172
);
173173
}
174174
}

src/modules/StreamingClient.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import {
2-
CONNECTION_CLOSED_CODE_MICROPHONE_PERMISSION_DENIED,
3-
CONNECTION_CLOSED_CODE_NORMAL,
4-
CONNECTION_CLOSED_CODE_WEBRTC_FAILURE,
5-
} from '../lib/constants';
1+
import { ConnectionClosedCode } from '../lib/constants';
62
import {
73
EngineApiRestClient,
84
InternalEventEmitter,
@@ -353,7 +349,8 @@ export class StreamingClient {
353349
console.log('StreamingClient - onSignalMessage: reason', reason);
354350
this.publicEventEmitter.emit(
355351
AnamEvent.CONNECTION_CLOSED,
356-
CONNECTION_CLOSED_CODE_NORMAL,
352+
ConnectionClosedCode.SERVER_CLOSED_CONNECTION,
353+
reason,
357354
);
358355
// close the peer connection
359356
this.shutdown();
@@ -440,12 +437,12 @@ export class StreamingClient {
440437
if (err.name === 'NotAllowedError' && err.message === 'Permission denied') {
441438
this.publicEventEmitter.emit(
442439
AnamEvent.CONNECTION_CLOSED,
443-
CONNECTION_CLOSED_CODE_MICROPHONE_PERMISSION_DENIED,
440+
ConnectionClosedCode.MICROPHONE_PERMISSION_DENIED,
444441
);
445442
} else {
446443
this.publicEventEmitter.emit(
447444
AnamEvent.CONNECTION_CLOSED,
448-
CONNECTION_CLOSED_CODE_WEBRTC_FAILURE,
445+
ConnectionClosedCode.WEBRTC_FAILURE,
449446
);
450447
}
451448

src/types/events/public/EventCallbacks.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { ConnectionClosedCode } from '../../../lib/constants';
12
import { Message, MessageStreamEvent, AnamEvent } from '../../index';
23

34
export type EventCallbacks = {
@@ -6,7 +7,10 @@ export type EventCallbacks = {
67
messageEvent: MessageStreamEvent,
78
) => void;
89
[AnamEvent.CONNECTION_ESTABLISHED]: () => void;
9-
[AnamEvent.CONNECTION_CLOSED]: (reason: string) => void;
10+
[AnamEvent.CONNECTION_CLOSED]: (
11+
reason: ConnectionClosedCode,
12+
details?: string,
13+
) => void;
1014
[AnamEvent.INPUT_AUDIO_STREAM_STARTED]: (audioStream: MediaStream) => void;
1115
[AnamEvent.VIDEO_STREAM_STARTED]: (videoStream: MediaStream) => void;
1216
[AnamEvent.VIDEO_PLAY_STARTED]: () => void;

0 commit comments

Comments
 (0)