add channel communication

This commit is contained in:
marlonerler
2024-07-04 17:27:38 +02:00
parent 0270dadfd0
commit 2770d9b07d
2 changed files with 42 additions and 3 deletions
+42
View File
@@ -0,0 +1,42 @@
// TYPES
// requests
interface IncomingMessage {
//sent on new connections
connectionType?: ConnectionType;
// subscribing to channel
channelSubscription?: string;
//sending message
messageChannel?: string;
messageBody?: string;
}
// connections
enum ConnectionType {
Server = "server",
Client = "client",
}
// TRACKING
const serversOnEntireNetwork = new Set<WebSocket>();
const socketsPerChannel = new Map<string, Set<WebSocket>>();
const channelsPerSocket = new Map<WebSocket, Set<string>>();
export function trackServer(ws: WebSocket): void {
serversOnEntireNetwork.add(ws);
}
export function trackSubscription(channel: string, ws: WebSocket): void {
if (!socketsPerChannel.has(channel)) socketsPerChannel.set(channel, new Set());
if (!channelsPerSocket.has(ws)) channelsPerSocket.set(ws, new Set());
socketsPerChannel.get(channel)!.add(ws);
channelsPerSocket.get(ws)!.add(channel);
}
export function forgetConnection(ws: WebSocket): void {
serversOnEntireNetwork.delete(ws);
serversOnEntireNetwork.delete(ws);
}