Skip to content
Snippets Groups Projects
Commit e5cbc3f7 authored by Marius Friess's avatar Marius Friess
Browse files

merge main into feat/hand-signal

parents c6016588 bdbb1aac
No related branches found
No related tags found
No related merge requests found
import * as crypto from 'crypto';
import { Server, Socket } from 'socket.io';
import { User } from '../user/user.entity';
import { Room } from '../room/room.entity';
......@@ -20,22 +19,32 @@ export interface Student extends ChannelUser {
}
export class Channel {
public readonly id: string;
public teacher?: Teacher;
public readonly students: Student[] = [];
private closeTimeout: NodeJS.Timeout;
constructor(public readonly room: Room, public readonly server: Server) {
// TODO: change id to be a 6 digit random number instead of a UUID to make it easier to type
this.id = crypto.randomUUID();
}
public students: Map<string, Student> = new Map();
constructor(
public readonly room: Room,
public readonly server: Server,
public readonly id: string,
) {}
public async joinAsStudent(client: Socket, name: string) {
await client.join(this.id);
this.students.push({ name, client, video: true, audio: true, handSignal: false });
const student = {
name,
client,
video: true,
audio: true,
handSignal: false,
};
this.students.set(client.id, student);
client.broadcast.to(this.id).emit('student-joined', {
id: client.id,
name,
name: student.name,
video: true,
audio: true,
handSignal: false,
......@@ -66,20 +75,19 @@ export class Channel {
}
public async leaveAsStudent(client: Socket) {
await client.leave(this.id);
const index = this.students.findIndex((s) => s.client.id === client.id);
const student = this.students.get(client.id);
if (index < 0) {
return;
}
this.students.splice(index, 1);
if (student) {
delete student.client;
this.students.delete(client.id);
client.broadcast.to(this.id).emit('student-left', client.id);
await client.leave(this.id);
client.broadcast.to(this.id).emit('student-left', client.id);
}
}
public isEmpty(): boolean {
return !this.teacher && this.students.length === 0;
return !this.teacher && this.students.size === 0;
}
public close() {
......@@ -91,7 +99,7 @@ export class Channel {
return this.teacher;
}
const student = this.students.find((s) => s.client.id === clientId);
const student = this.students.get(clientId);
if (student) {
return student;
......@@ -101,8 +109,7 @@ export class Channel {
}
public getStudent(clientId: string): Student {
const student = this.students.find((s) => s.client.id === clientId);
const student = this.students.get(clientId);
if (student) {
return student;
......@@ -112,7 +119,7 @@ export class Channel {
}
public changeName(client: Socket, name: string) {
const student = this.students.find((s) => s.client.id === client.id);
const student = this.students.get(client.id);
if (student) {
student.name = name;
......@@ -139,4 +146,21 @@ export class Channel {
public toString(): string {
return `Channel{${this.id}}`;
}
public clearCloseTimeout() {
if (this.closeTimeout) {
clearTimeout(this.closeTimeout);
this.closeTimeout = undefined;
}
}
public setCloseTimeout(onTimeout: () => void) {
this.clearCloseTimeout();
this.closeTimeout = setTimeout(() => {
if (this.isEmpty()) {
this.close();
onTimeout();
}
}, 1000 * 60 * 10);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment