fix null safety in StreamingApiServerService.ts
This commit is contained in:
parent
6b281af9fa
commit
e1639f70df
1 changed files with 13 additions and 9 deletions
|
|
@ -35,7 +35,7 @@ const MAX_CONNECTIONS_PER_CLIENT = 32;
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class StreamingApiServerService implements OnApplicationShutdown {
|
export class StreamingApiServerService implements OnApplicationShutdown {
|
||||||
#wss: WebSocket.WebSocketServer;
|
#wss?: WebSocket.WebSocketServer;
|
||||||
#connections = new Map<WebSocket.WebSocket, number>();
|
#connections = new Map<WebSocket.WebSocket, number>();
|
||||||
#connectionsByClient = new Map<string, Set<WebSocket.WebSocket>>(); // key: IP / user ID -> value: connection
|
#connectionsByClient = new Map<string, Set<WebSocket.WebSocket>>(); // key: IP / user ID -> value: connection
|
||||||
#cleanConnectionsIntervalId: NodeJS.Timeout | null = null;
|
#cleanConnectionsIntervalId: NodeJS.Timeout | null = null;
|
||||||
|
|
@ -100,7 +100,7 @@ export class StreamingApiServerService implements OnApplicationShutdown {
|
||||||
|
|
||||||
@bindThis
|
@bindThis
|
||||||
public attach(server: http.Server): void {
|
public attach(server: http.Server): void {
|
||||||
this.#wss = new WebSocket.WebSocketServer({
|
const wss = this.#wss = new WebSocket.WebSocketServer({
|
||||||
noServer: true,
|
noServer: true,
|
||||||
perMessageDeflate: this.config.websocketCompression,
|
perMessageDeflate: this.config.websocketCompression,
|
||||||
});
|
});
|
||||||
|
|
@ -212,7 +212,7 @@ export class StreamingApiServerService implements OnApplicationShutdown {
|
||||||
|
|
||||||
await stream.init();
|
await stream.init();
|
||||||
|
|
||||||
this.#wss.handleUpgrade(request, socket, head, (ws) => {
|
wss.handleUpgrade(request, socket, head, (ws) => {
|
||||||
connectionsForClient.add(ws);
|
connectionsForClient.add(ws);
|
||||||
|
|
||||||
// Call before emit() in case it throws an error.
|
// Call before emit() in case it throws an error.
|
||||||
|
|
@ -241,7 +241,7 @@ export class StreamingApiServerService implements OnApplicationShutdown {
|
||||||
};
|
};
|
||||||
|
|
||||||
ws.on('error', onWsInitError);
|
ws.on('error', onWsInitError);
|
||||||
this.#wss.emit('connection', ws, request, {
|
wss.emit('connection', ws, request, {
|
||||||
stream, user, app,
|
stream, user, app,
|
||||||
});
|
});
|
||||||
ws.off('error', onWsInitError);
|
ws.off('error', onWsInitError);
|
||||||
|
|
@ -321,14 +321,18 @@ export class StreamingApiServerService implements OnApplicationShutdown {
|
||||||
this.#connectionsByClient.clear();
|
this.#connectionsByClient.clear();
|
||||||
|
|
||||||
await new Promise<void>((resolve, reject) => {
|
await new Promise<void>((resolve, reject) => {
|
||||||
|
if (this.#wss) {
|
||||||
this.#wss.close(err => {
|
this.#wss.close(err => {
|
||||||
if (err) reject(err);
|
if (err) reject(err);
|
||||||
else resolve();
|
else resolve();
|
||||||
});
|
});
|
||||||
|
} else {
|
||||||
|
resolve();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Don't disconnect this until *after* close returns
|
// Don't disconnect this until *after* close returns
|
||||||
this.#wss.off('error', this.onWsError);
|
this.#wss?.off('error', this.onWsError);
|
||||||
}
|
}
|
||||||
|
|
||||||
@bindThis
|
@bindThis
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue