-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from SWM-FIRE/FIRE-156-개발-be-사용자-후보-교환-시그널링-구현
FIRE-156 Add screenshare gateway
- Loading branch information
Showing
4 changed files
with
98 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters