set => map
This commit is contained in:
26
src/sse.ts
26
src/sse.ts
@@ -1,7 +1,8 @@
|
||||
import { UUID } from "crypto";
|
||||
import { logInfo } from "./logger.js";
|
||||
|
||||
export type SseClient = {
|
||||
id: number;
|
||||
id: UUID;
|
||||
send: (data: SseEvent) => void;
|
||||
}
|
||||
|
||||
@@ -12,29 +13,26 @@ export type SseEvent = {
|
||||
}
|
||||
|
||||
export class SseService {
|
||||
private clients = new Set<SseClient>();
|
||||
private clients: Map<string, SseClient> = new Map();
|
||||
|
||||
addClient(client: SseClient): void {
|
||||
this.clients.add(client);
|
||||
this.clients.set(client.id, client);
|
||||
logInfo(
|
||||
`SSE client connected: ${client.id}. Total clients: ${this.clients.size}`,
|
||||
);
|
||||
}
|
||||
|
||||
removeClient(clientId: number): void {
|
||||
const client = [...this.clients].find((c) => c.id === clientId);
|
||||
if (client) {
|
||||
this.clients.delete(client);
|
||||
logInfo(
|
||||
`SSE client disconnected: ${clientId}. Total clients: ${this.clients.size}`,
|
||||
);
|
||||
}
|
||||
removeClient(clientId: string): void {
|
||||
this.clients.delete(clientId);
|
||||
logInfo(
|
||||
`SSE client disconnected: ${clientId}. Total clients: ${this.clients.size}`,
|
||||
);
|
||||
}
|
||||
|
||||
notifyClients(event: SseEvent): void {
|
||||
this.clients.forEach((client) => {
|
||||
client.send(event);
|
||||
});
|
||||
for (const client of this.clients.values()) {
|
||||
client.send(event);
|
||||
}
|
||||
}
|
||||
|
||||
getClientCount(): number {
|
||||
|
||||
Reference in New Issue
Block a user