Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/AnamClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -410,6 +411,10 @@ export default class AnamClient {

public async stopStreaming(): Promise<void> {
if (this.streamingClient) {
this.publicEventEmitter.emit(
AnamEvent.CONNECTION_CLOSED,
ConnectionClosedCode.NORMAL,
);
await this.streamingClient.stopConnection();
this.streamingClient = null;
this.sessionId = null;
Expand Down
14 changes: 7 additions & 7 deletions src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 3 additions & 1 deletion src/modules/PublicEventEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}

Expand Down
6 changes: 3 additions & 3 deletions src/modules/SignallingClient.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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,
);
}
}
Expand All @@ -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,
);
}
}
Expand Down
13 changes: 5 additions & 8 deletions src/modules/StreamingClient.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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,
);
}

Expand Down
6 changes: 5 additions & 1 deletion src/types/events/public/EventCallbacks.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ConnectionClosedCode } from '../../../lib/constants';
import { Message, MessageStreamEvent, AnamEvent } from '../../index';

export type EventCallbacks = {
Expand All @@ -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;
Expand Down