Compare commits

...

1 Commits

Author SHA1 Message Date
Manuel Raynaud
9427b17a15 save work 2026-04-27 16:21:15 +02:00
3 changed files with 32 additions and 0 deletions

View File

@@ -18,3 +18,6 @@ export const PORT = Number(process.env.PORT || 4444);
export const SENTRY_DSN = process.env.SENTRY_DSN || '';
export const COLLABORATION_BACKEND_BASE_URL =
process.env.COLLABORATION_BACKEND_BASE_URL || 'http://app-dev:8000';
export const COLLABORATION_INACTIVITY_TIMEOUT = Number(
process.env.COLLABORATION_INACTIVITY_TIMEOUT || 0,
);

View File

@@ -1,9 +1,14 @@
import { Request } from 'express';
import * as ws from 'ws';
import { COLLABORATION_INACTIVITY_TIMEOUT } from '@/env';
import { hocuspocusServer } from '@/servers/hocuspocusServer';
import { setupInactivityTimeout } from '@/utils';
export const collaborationWSHandler = (ws: ws.WebSocket, req: Request) => {
if (COLLABORATION_INACTIVITY_TIMEOUT > 0) {
setupInactivityTimeout(ws, COLLABORATION_INACTIVITY_TIMEOUT);
}
try {
hocuspocusServer.hocuspocus.handleConnection(ws, req);
} catch (error) {

View File

@@ -1,3 +1,5 @@
import * as ws from 'ws';
import { COLLABORATION_LOGGING } from './env';
export function logger(...args: unknown[]) {
@@ -9,3 +11,25 @@ export function logger(...args: unknown[]) {
export const toBase64 = function (str: Uint8Array) {
return Buffer.from(str).toString('base64');
};
export function setupInactivityTimeout(
socket: ws.WebSocket,
delayMs: number,
): void {
const closeInactive = () => {
logger('Closing inactive WebSocket connection after', delayMs, 'ms');
socket.close();
};
let timer = setTimeout(closeInactive, delayMs);
socket.on('message', () => {
logger('clear closeInactive timer');
clearTimeout(timer);
timer = setTimeout(closeInactive, delayMs);
});
socket.on('close', () => {
clearTimeout(timer);
});
}