Skip to content

Commit

Permalink
feat: auto close calls on socket disconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
farhat-ha committed Mar 12, 2024
1 parent dd64f2f commit b0d9024
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
13 changes: 13 additions & 0 deletions packages/js/src/Modules/Janus/CallAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,22 @@ export default class CallAgent {
this._gatewayHandleId = gatewayHandleId;
this._gatewaySessionId = gatewaySessionId;
this._connection.addListener(ConnectionEvents.Message, this._onMessage);
this._connection.addListener(
ConnectionEvents.StateChange,
this._onConnectionStateChange
);
this._ringtone = createAudio(ringtoneFile, '_ringtone');
this._ringback = createAudio(ringbackFile, '_ringback');
}

private _onConnectionStateChange = async () => {
if (!this._connection.isDead) {
await Promise.all(
Object.values(this._calls).map((call: Call) => call.closeCall())
);
this._calls.clear();
}
};
private _onMessage = (data: string) => {
const msg = JSON.parse(data) as JanusResponse;
if (isSIPIncomingCallMessage(msg)) {
Expand All @@ -78,6 +90,7 @@ export default class CallAgent {
type: 'callUpdate',
call,
});
this._calls.delete(call.id);
};
private _onAccept = async (msg: JanusSIPCallAcceptedEvent) => {
const call = this._calls.get(msg.plugindata.data.call_id);
Expand Down
8 changes: 4 additions & 4 deletions packages/js/src/Modules/Janus/Client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { ICallOptions } from 'src/utils/interfaces';
import { Call } from './Call';
import CallAgent from './CallAgent';
import { Connection } from './Connection';
import { deRegister, register } from './Handler';
import KeepAliveAgent from './KeepAliveAgent';
Expand All @@ -8,8 +10,6 @@ import { Environment } from './constants';
import { IClientOptions } from './interfaces';
import { AttachSipPluginTransaction } from './transactions/AttachSIPPlugin';
import { CreateSessionTransaction } from './transactions/CreateSession';
import CallAgent from './CallAgent';
import { ICallOptions } from 'src/utils/interfaces';

export default class JanusClient {
private _gatewaySessionId: number | null = null;
Expand All @@ -28,11 +28,10 @@ export default class JanusClient {
environment: Environment.production,
});
this._options = options;
this._connection.connect();
}

public async connect() {
this._connection.connect();

const { sessionId } = await transactionManager.execute(
new CreateSessionTransaction()
);
Expand Down Expand Up @@ -82,6 +81,7 @@ export default class JanusClient {
}
public disconnect() {
// TODO - implement
this._connection.disconnect();
}

public enableWebcam() {
Expand Down
7 changes: 6 additions & 1 deletion packages/js/src/Modules/Janus/Connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type ConnectionOptions = {
};
export class Connection extends EventEmitter {
private _socket: WebSocket | null = null;

private _host: string = PROD_HOST;

constructor(options: ConnectionOptions) {
Expand All @@ -32,6 +32,11 @@ export class Connection extends EventEmitter {
this._socket.addEventListener('message', this._onMessage);
}

public disconnect() {
this._socket.close();
this.emit(ConnectionEvents.StateChange);
}

private _onOpen = () => {
this.emit(ConnectionEvents.StateChange);
};
Expand Down
2 changes: 1 addition & 1 deletion packages/js/src/Modules/Janus/KeepAliveAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export default class KeepAliveAgent {
};

private _onConnectionStateChange = () => {
if (!this._connection.isAlive) {
if (this._connection.isDead) {
return this.stop();
}
this.start();
Expand Down

0 comments on commit b0d9024

Please sign in to comment.