Closes the quiz if no socket is connected anymore

parent 5a5c1418
Pipeline #21950 passed with stages
in 4 minutes
......@@ -235,6 +235,9 @@ export class QuizEntity extends AbstractEntity implements IQuizEntity {
public removeSocketFromChannel(socket: WebSocket): void {
const index = this._socketChannel.findIndex(value => value === socket);
this._socketChannel.splice(index, 1);
if (!this._socketChannel.length) {
DbDAO.update(DbCollection.Quizzes, { _id: this.id }, { state: QuizState.Inactive });
}
}
public containsSocket(socket: WebSocket): boolean {
......
......@@ -54,6 +54,7 @@ export class WebSocketRouter {
private static init(): void {
WebSocketRouter._wss.on('connection', (ws: WebSocket) => {
ws.on('close', opcode => {
this.disconnectFromChannel(ws);
LoggerService.info('Closing socket connection', opcode, `(${this.getWebSocketOpcode(opcode)})`);
});
ws.on('error', (err) => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment