Skip to content

Commit

Permalink
Merge pull request #6 from waterbustech/feat/white-board
Browse files Browse the repository at this point in the history
feat/white board
  • Loading branch information
lambiengcode authored Oct 1, 2024
2 parents f94ec26 + 30636af commit 76afde8
Show file tree
Hide file tree
Showing 15 changed files with 527 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"socket.io": "^4.7.5",
"socket.io-adapter": "^2.5.4",
"socket.io-redis": "^6.1.1",
"waterbus-proto": "^1.1.5",
"waterbus-proto": "^1.1.11",
"werift": "^0.19.1",
"winston": "^3.13.0"
},
Expand Down
10 changes: 10 additions & 0 deletions src/domain/constants/socket_events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,20 @@ const SocketEvent = {

subscriberRenegotiationSSC: 'SUBSCRIBER_RENEGOTIATION_SSC',

// White board
startWhiteBoardSSC: 'START_WHITE_BOARD_SSC',
startWhiteBoardCSS: 'START_WHITE_BOARD_CSS',
updateWhiteBoardCSS: 'UPDATE_WHITE_BOARD_CSS',
updateWhiteBoardSSC: 'UPDATE_WHITE_BOARD_SSC',
cleanWhiteBoardCSS: 'CLEAN_WHITE_BOARD_CSS',
cleanWhiteBoardSSC: 'CLEAN_WHITE_BOARD_SSC',

// Chats
sendMessageSSC: 'SEND_MESSAGE_SSC',
updateMessageSSC: 'UPDATE_MESSAGE_SSC',
deleteMessageSSC: 'DELETE_MESSAGE_SSC',
newMemberJoinedSSC: 'NEW_MEMBER_JOINED_SSC',
newInvitationSSC: 'NEW_INVITATION_SSC',

// System
connection: 'connection',
Expand Down
4 changes: 4 additions & 0 deletions src/domain/models/white-board-action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum WhiteBoardAction {
add = 'add',
remove = 'remove',
}
26 changes: 26 additions & 0 deletions src/infrastructure/client-proxy/client-proxy.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ export const getGrpcClientOptions = (
case EPackage.MEETING:
url = config.getMeetingGrpcUrl();
break;
case EPackage.WHITEBOARD:
url = config.getWhiteBoardGrpcUrl();
break;
case EPackage.RECORD:
url = config.getRecordGrpcUrl();
break;
}
return {
transport: Transport.GRPC,
Expand All @@ -40,6 +46,8 @@ export const getGrpcClientOptions = (
export class ClientProxyModule {
static authClientProxy = 'authClientProxy';
static meetingClientProxy = 'meetingClientProxy';
static whiteBoardClientProxy = 'whiteBoardClientProxy';
static recordClientProxy = 'recordClientProxy';

static register(): DynamicModule {
return {
Expand All @@ -61,10 +69,28 @@ export class ClientProxyModule {
getGrpcClientOptions(config, EPackage.MEETING),
),
},
{
provide: ClientProxyModule.whiteBoardClientProxy,
inject: [EnvironmentConfigService],
useFactory: (config: EnvironmentConfigService) =>
ClientProxyFactory.create(
getGrpcClientOptions(config, EPackage.WHITEBOARD),
),
},
{
provide: ClientProxyModule.recordClientProxy,
inject: [EnvironmentConfigService],
useFactory: (config: EnvironmentConfigService) =>
ClientProxyFactory.create(
getGrpcClientOptions(config, EPackage.RECORD),
),
},
],
exports: [
ClientProxyModule.authClientProxy,
ClientProxyModule.meetingClientProxy,
ClientProxyModule.whiteBoardClientProxy,
ClientProxyModule.recordClientProxy,
],
};
}
Expand Down
8 changes: 8 additions & 0 deletions src/infrastructure/config/environment/environments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ export class EnvironmentConfigService {
return this.configService.get<string>('MEETING_GRPC_ADDRESS');
}

getRecordGrpcUrl(): string {
return this.configService.get<string>('RECORD_GRPC_ADDRESS');
}

getWhiteBoardGrpcUrl(): string {
return this.configService.get<string>('WHITE_BOARD_GRPC_ADDRESS');
}

getTurnUsername(): string {
return this.configService.get<string>('TURN_USERNAME');
}
Expand Down
73 changes: 73 additions & 0 deletions src/infrastructure/controllers/chats/chats.proto.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,77 @@ export class ChatGrpcController implements chat.ChatService {
});
}
}

@GrpcMethod('ChatService', 'newMemberJoined')
newMemberJoined(
data: chat.NewMemberJoinedRequest,
): Observable<chat.MessageResponse> {
try {
const payload = {
member: data.member,
meetingId: data.meetingId,
};

this.socketGateway.emitTo({
data: payload,
room: null,
event: SocketEvent.newMemberJoinedSSC,
socketIds: data.ccus,
});

const response: chat.MessageResponse = {
succeed: true,
};

return new Observable<chat.MessageResponse>((observer) => {
observer.next(response);
observer.complete();
});
} catch (error) {
const response: chat.MessageResponse = {
succeed: false,
};

return new Observable<chat.MessageResponse>((observer) => {
observer.next(response);
observer.complete();
});
}
}

@GrpcMethod('ChatService', 'newInvitation')
newInvitation(
data: chat.NewInvitationRequest,
): Observable<chat.MessageResponse> {
try {
const payload = {
meeting: data.room,
};

this.socketGateway.emitTo({
data: payload,
room: null,
event: SocketEvent.newInvitationSSC,
socketIds: data.ccus,
});

const response: chat.MessageResponse = {
succeed: true,
};

return new Observable<chat.MessageResponse>((observer) => {
observer.next(response);
observer.complete();
});
} catch (error) {
const response: chat.MessageResponse = {
succeed: false,
};

return new Observable<chat.MessageResponse>((observer) => {
observer.next(response);
observer.complete();
});
}
}
}
14 changes: 14 additions & 0 deletions src/infrastructure/gateways/gateway.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { WebRTCModule } from 'src/infrastructure/services/sfu/webrtc.module';
import { EnvironmentConfigModule } from '../config/environment/environment.module';
import { AuthGrpcService } from '../services/auth/auth.service';
import { MessageBroker } from '../services/message-broker/message-broker';
import { WhiteBoardGrpcService } from '../services/meeting/white-board.service';
import { RecordGrpcService } from '../services/meeting/record.service';

@Module({
imports: [
Expand All @@ -27,6 +29,18 @@ import { MessageBroker } from '../services/message-broker/message-broker';
useFactory: (clientProxy: ClientGrpc) =>
new MeetingGrpcService(clientProxy),
},
{
provide: WhiteBoardGrpcService,
inject: [ClientProxyModule.whiteBoardClientProxy],
useFactory: (clientProxy: ClientGrpc) =>
new WhiteBoardGrpcService(clientProxy),
},
{
provide: RecordGrpcService,
inject: [ClientProxyModule.recordClientProxy],
useFactory: (clientProxy: ClientGrpc) =>
new RecordGrpcService(clientProxy),
},
MessageBroker,
SocketGateway,
MeetingGateway,
Expand Down
6 changes: 6 additions & 0 deletions src/infrastructure/gateways/meeting/dtos/clean_board.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { ApiProperty } from '@nestjs/swagger';

export class CleanWhiteBoardDto {
@ApiProperty({ type: String })
roomId: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { ApiProperty } from '@nestjs/swagger';

export class StartWhiteBoardDto {
@ApiProperty({ type: String })
roomId: string;
}
28 changes: 28 additions & 0 deletions src/infrastructure/gateways/meeting/dtos/update_white_board.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { ApiProperty } from '@nestjs/swagger';
import { WhiteBoardAction } from 'src/domain/models/white-board-action';

export interface PaintModel {
color: string;
offsets: OffsetModel[];
width: number;
}

export interface OffsetModel {
dx: number;
dy: number;
}

export class UpdateWhiteBoardDto {
@ApiProperty({ type: String })
roomId: string;

@ApiProperty({
type: 'enum',
enum: WhiteBoardAction,
default: WhiteBoardAction.add,
})
action: string;

@ApiProperty({ type: 'simple-json' })
paints: PaintModel[];
}
36 changes: 35 additions & 1 deletion src/infrastructure/gateways/meeting/meeting.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ import { MessageBroker } from 'src/infrastructure/services/message-broker/messag
import { EnvironmentConfigService } from 'src/infrastructure/config/environment/environments';
import { SetSubscribeSubtitleDto } from './dtos/set_subscribe_subtitle.dto';
import { PublisherRenegotiationDto } from './dtos/publisher_renegotiation.dto';
import { SubscriberRenegotiationDto } from './dtos/subscriber_renegotiation.dto';
import { StartWhiteBoardDto } from './dtos/start_white_board.dto';
import { UpdateWhiteBoardDto } from './dtos/update_white_board.dto';
import { CleanWhiteBoardDto } from './dtos/clean_board.dto';

@WebSocketGateway()
export class MeetingGateway {
Expand Down Expand Up @@ -347,6 +349,34 @@ export class MeetingGateway {
});
}

@SubscribeMessage(SocketEvent.startWhiteBoardCSS)
handleStartWhiteBoard(
client: ISocketClient,
payload: StartWhiteBoardDto,
): any {
client.join(this._getWhiteBoardRoom(payload.roomId));
}

@SubscribeMessage(SocketEvent.updateWhiteBoardCSS)
handleUpdateWhiteBoard(
client: ISocketClient,
payload: UpdateWhiteBoardDto,
): any {
client
.to(this._getWhiteBoardRoom(payload.roomId))
.emit(SocketEvent.updateWhiteBoardSSC, payload);
}

@SubscribeMessage(SocketEvent.cleanWhiteBoardCSS)
handleCleanWhiteBoard(
client: ISocketClient,
payload: CleanWhiteBoardDto,
): any {
client
.to(this._getWhiteBoardRoom(payload.roomId))
.to(SocketEvent.cleanWhiteBoardSSC);
}

@SubscribeMessage(SocketEvent.setSubscribeSubtitleCSS)
handleSetSubscribeSubtitle(
client: ISocketClient,
Expand Down Expand Up @@ -386,4 +416,8 @@ export class MeetingGateway {
this.logger.debug(`Update leave room in grpc: ${succeed}`);
}
}

private _getWhiteBoardRoom(roomId: string): string {
return `white-board-${roomId}`;
}
}
6 changes: 4 additions & 2 deletions src/infrastructure/gateways/socket/socket.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,12 @@ export class SocketGateway
}: {
data: any;
event: string;
room: string;
room: string | null;
socketIds: string[];
}) {
this.server.to(room).emit(event, data);
if (room) {
this.server.to(room).emit(event, data);
}

if (socketIds) {
this.server.to(socketIds).emit(event, data);
Expand Down
Loading

0 comments on commit 76afde8

Please sign in to comment.