Skip to content

Commit

Permalink
feat(connect): handle reconnect event
Browse files Browse the repository at this point in the history
  • Loading branch information
lambiengcode committed Aug 6, 2024
1 parent 7ab9e74 commit 0ff56a6
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 15 deletions.
4 changes: 3 additions & 1 deletion src/domain/constants/socket_events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ const SocketEvent = {
// System
connection: 'connection',
disconnect: 'disconnect',
destroy: 'destroy_',
destroy: 'destroy',
sendPodNameSSC: 'SEND_POD_NAME_SSC',
reconnect: 'reconnect_CSS',
} as const;

export default SocketEvent;
52 changes: 39 additions & 13 deletions src/infrastructure/gateways/socket/socket.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
OnGatewayConnection,
OnGatewayDisconnect,
OnGatewayInit,
SubscribeMessage,
WebSocketGateway,
WebSocketServer,
} from '@nestjs/websockets';
Expand Down Expand Up @@ -32,6 +33,7 @@ export class SocketGateway
@WebSocketServer() public server: Server;

private logger: Logger = new Logger(SocketGateway.name);
private isDestroying: Boolean = false;

afterInit(server: Server) {
this.server = server;
Expand All @@ -48,7 +50,9 @@ export class SocketGateway
userId,
});

client.join(SocketEvent.destroy + this.environment.getPodName());
this.server.to(client.id).emit(SocketEvent.sendPodNameSSC, {
podName: this.environment.getPodName(),
});
} catch (error) {
this.logger.error(error?.message, error?.stack);
return client.disconnect(true);
Expand All @@ -57,19 +61,13 @@ export class SocketGateway

async handleDisconnect(client: ISocketClient) {
try {
const info = this.rtcManager.leaveRoom({ clientId: client.id });
const info = await this.handleLeaveRoom(client);

if (info) {
client.broadcast
.to(info.roomId)
.emit(SocketEvent.participantHasLeftSSC, {
targetId: info.participantId,
});

client.leave(info.roomId);

const succeed = await this.meetingService.leaveRoom(info);
this.logger.debug(`Update leave room in grpc: ${succeed}`);
if (!this.isDestroying) {
const succeed = await this.meetingService.leaveRoom(info);
this.logger.debug(`Update leave room in grpc: ${succeed}`);
}
}

this.authService.removeCCU({
Expand All @@ -81,12 +79,20 @@ export class SocketGateway
}
}

@SubscribeMessage(SocketEvent.reconnect)
async handleReconnect(client: ISocketClient) {
await this.handleLeaveRoom(client);
}

async onModuleDestroy(signal?: string): Promise<void> {
if (signal === ShutdownSignal.SIGTERM) {
try {
this.isDestroying = true;
this.logger.debug(`Pod is shutting down...`);

this.server.emit(SocketEvent.destroy + this.environment.getPodName());
this.server.emit(SocketEvent.destroy, {
podName: this.environment.getPodName(),
});

// Delete CCU & participants in this pod
this.authService.shutDownPod({
Expand All @@ -98,6 +104,26 @@ export class SocketGateway
}
}

async handleLeaveRoom(client: ISocketClient): Promise<IClient | null> {
try {
const info = this.rtcManager.leaveRoom({ clientId: client.id });

if (info) {
client.broadcast
.to(info.roomId)
.emit(SocketEvent.participantHasLeftSSC, {
targetId: info.participantId,
});

client.leave(info.roomId);
}

return info;
} catch (error) {
this.logger.error(error?.message, error?.stack);
}
}

// Utils: Use for other service want to emit client in realtime
async emitTo({
data,
Expand Down
2 changes: 1 addition & 1 deletion src/infrastructure/services/sfu/entities/track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class Track {
) {
this.logger = new Logger(Track.name);

this.initialGoogleSTT(roomId, participantId);
// this.initialGoogleSTT(roomId, participantId);

track.onReceiveRtp.subscribe((packet) => {
if (track.kind == 'audio') {
Expand Down

0 comments on commit 0ff56a6

Please sign in to comment.