Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIRE-156 Add screenshare gateway #3

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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"@types/jest": "27.5.0",
"@types/node": "^16.0.0",
"@types/supertest": "^2.0.11",
"@types/ws": "^8.5.3",
"@typescript-eslint/eslint-plugin": "^5.0.0",
"@typescript-eslint/parser": "^5.0.0",
"eslint": "^8.0.1",
Expand Down
3 changes: 2 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { AppController } from './app.controller';
import { AppService } from './app.service';
import { RoomsController } from './rooms/rooms.controller';
import { RoomsService } from './rooms/rooms.service';
import { ScreenShareGateway } from './gateways/screenshare.gateway';

@Module({
imports: [
Expand All @@ -13,6 +14,6 @@ import { RoomsService } from './rooms/rooms.service';
}),
],
controllers: [AppController, RoomsController],
providers: [AppService, RoomsService],
providers: [AppService, RoomsService, ScreenShareGateway],
})
export class AppModule {}
88 changes: 88 additions & 0 deletions src/gateways/screenshare.gateway.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import {
OnGatewayDisconnect,
OnGatewayInit,
SubscribeMessage,
WebSocketGateway,
WebSocketServer,
} from '@nestjs/websockets';

import { Logger } from '@nestjs/common';
import { Server } from 'ws';
import { Socket } from 'socket.io';

@WebSocketGateway({ namespace: 'screen-share' })
export class ScreenShareGateway implements OnGatewayInit, OnGatewayDisconnect {
@WebSocketServer()
server: Server;

private activeSockets: { room: string; id: string }[] = [];

private logger: Logger = new Logger('ScreenShareGateway');

@SubscribeMessage('joinRoom')
public joinRoom(client: Socket, room: string): void {
const existingSocket = this.activeSockets?.find(
(socket) => socket.room === room && socket.id === client.id,
);

if (!existingSocket) {
this.activeSockets = [...this.activeSockets, { id: client.id, room }];
client.emit(`${room}-update-user-list`, {
users: this.activeSockets
.filter((socket) => socket.room === room && socket.id !== client.id)
.map((existingSocket) => existingSocket.id),
current: client.id,
});

client.broadcast.emit(`${room}-add-user`, {
user: client.id,
});
}
return this.logger.log(`Client ${client.id} joined ${room}`);
}

@SubscribeMessage('call-user')
public callUser(client: Socket, data: any): void {
client.to(data.to).emit('call-made', {
offer: data.offer,
socket: client.id,
});
}

@SubscribeMessage('make-answer')
public makeAnswer(client: Socket, data: any): void {
client.to(data.to).emit('answer-made', {
socket: client.id,
answer: data.answer,
});
}

@SubscribeMessage('reject-call')
public rejectCall(client: Socket, data: any): void {
client.to(data.from).emit('call-rejected', {
socket: client.id,
});
}

public afterInit(): void {
this.logger.log('Init');
}

public handleDisconnect(client: Socket): void {
const existingSocket = this.activeSockets.find(
(socket) => socket.id === client.id,
);

if (!existingSocket) return;

this.activeSockets = this.activeSockets.filter(
(socket) => socket.id !== client.id,
);

client.broadcast.emit(`${existingSocket.room}-remove-user`, {
socketId: client.id,
});

this.logger.log(`Client disconnected: ${client.id}`);
}
}
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1089,6 +1089,13 @@
dependencies:
"@types/superagent" "*"

"@types/ws@^8.5.3":
version "8.5.3"
resolved "https://registry.yarnpkg.com/@types/ws/-/ws-8.5.3.tgz#7d25a1ffbecd3c4f2d35068d0b283c037003274d"
integrity sha512-6YOoWjruKj1uLf3INHH7D3qTXwFfEsg1kf3c0uDdSBJwfa/llkwIjrAGV7j7mVgGNbzTQ3HiHKKDXl6bJPD97w==
dependencies:
"@types/node" "*"

"@types/yargs-parser@*":
version "21.0.0"
resolved "https://registry.yarnpkg.com/@types/yargs-parser/-/yargs-parser-21.0.0.tgz#0c60e537fa790f5f9472ed2776c2b71ec117351b"
Expand Down