fix(backend): prevent realtime connections getting prepared for closed websocket

The setAdapter function checks if the websocket is closed.
If this is the case then an error is thrown and the whole process will be canceled. If the adapter isn't set before the realtime connection object is prepared then the connection will subscribe to all the events and THEN the process will be canceled. Because the MessageTransporter has no adapter (and won't get one), the connection will never get a disconnect event and clean up.

This causes the flood of "cant send message over closed websocket" messages.

Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
Tilman Vatteroth 2023-06-27 15:12:16 +02:00
parent f15aed3ed3
commit bb54746ccf

View file

@ -87,20 +87,22 @@ export class WebsocketGateway implements OnGatewayConnection {
await this.realtimeNoteService.getOrCreateRealtimeNote(note);
const websocketTransporter = new MessageTransporter();
websocketTransporter.setAdapter(
new BackendWebsocketAdapter(clientSocket),
);
const permissions = await this.noteService.toNotePermissionsDto(note);
const acceptEdits: boolean = userCanEdit(
permissions as NotePermissions,
user?.username,
);
const connection = new RealtimeConnection(
websocketTransporter,
user,
realtimeNote,
acceptEdits,
);
websocketTransporter.setAdapter(
new BackendWebsocketAdapter(clientSocket),
);
realtimeNote.addClient(connection);