From 701f6ae1acf4426c48b4e92082d2ed72bcc445ec Mon Sep 17 00:00:00 2001 From: hippoz <10706925-hippoz@users.noreply.gitlab.com> Date: Sun, 10 Apr 2022 21:28:36 +0300 Subject: [PATCH] replace `broadcast` with `dispatch` in the gateway code --- src/gateway/index.ts | 38 +++++++++++++++---------------- src/routes/api/v1/channels.ts | 1 + src/types/gatewayclientstate.d.ts | 2 +- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/gateway/index.ts b/src/gateway/index.ts index 76a2b9f..71019e8 100644 --- a/src/gateway/index.ts +++ b/src/gateway/index.ts @@ -10,50 +10,50 @@ import { GatewayPayloadType } from "./gatewaypayloadtype"; const GATEWAY_BATCH_INTERVAL = 25000 || process.env.GATEWAY_BATCH_INTERVAL; const GATEWAY_PING_INTERVAL = 20000 || process.env.GATEWAY_PING_INTERVAL; -// mapping between a broadcast id and a websocket client -const broadcastChannels = new Map>(); +// mapping between a dispatch id and a websocket client +const dispatchChannels = new Map>(); -function clientSubscribe(ws: WebSocket, broadcastChannel: string) { - ws.state.broadcastChannels.add(broadcastChannel); - if (!broadcastChannels.get(broadcastChannel)) { - broadcastChannels.set(broadcastChannel, new Set()); +function clientSubscribe(ws: WebSocket, dispatchChannel: string) { + ws.state.dispatchChannels.add(dispatchChannel); + if (!dispatchChannels.get(dispatchChannel)) { + dispatchChannels.set(dispatchChannel, new Set()); } - broadcastChannels.get(broadcastChannel)?.add(ws); + dispatchChannels.get(dispatchChannel)?.add(ws); } -function clientUnsubscribe(ws: WebSocket, broadcastChannel: string) { +function clientUnsubscribe(ws: WebSocket, dispatchChannel: string) { if (!ws.state) return; - ws.state.broadcastChannels.delete(broadcastChannel); + ws.state.dispatchChannels.delete(dispatchChannel); - const set = broadcastChannels.get(broadcastChannel); + const set = dispatchChannels.get(dispatchChannel); if (!set) return; set.delete(ws); if (set.size < 1) { - broadcastChannels.delete(broadcastChannel); + dispatchChannels.delete(dispatchChannel); } } function clientUnsubscribeAll(ws: WebSocket) { if (!ws.state) return; - ws.state.broadcastChannels.forEach(e => { - const set = broadcastChannels.get(e); + ws.state.dispatchChannels.forEach(e => { + const set = dispatchChannels.get(e); if (!set) return; set.delete(ws); if (set && set.size < 1) { - broadcastChannels.delete(e); + dispatchChannels.delete(e); } }); - ws.state.broadcastChannels = new Set(); + ws.state.dispatchChannels = new Set(); } -export function broadcast(channel: string, message: GatewayPayload) { - const members = broadcastChannels.get(channel); +export function dispatch(channel: string, message: GatewayPayload) { + const members = dispatchChannels.get(channel); if (!members) return; members.forEach(e => e.send(JSON.stringify(message))); @@ -133,7 +133,7 @@ export default function(server: Server) { alive: false, ready: false, lastAliveCheck: performance.now(), - broadcastChannels: new Set() + dispatchChannels: new Set() }; sendPayload(ws, { @@ -145,7 +145,6 @@ export default function(server: Server) { ws.on("close", () => { clientUnsubscribeAll(ws); - console.log(broadcastChannels); }); ws.on("message", async (rawData, isBinary) => { @@ -171,6 +170,7 @@ export default function(server: Server) { // each user should have their own list of channels that they join const channels = await query("SELECT id, name, owner_id FROM channels"); + clientSubscribe(ws, "*"); channels.rows.forEach(c => { clientSubscribe(ws, `channel:${c.id}`); }); diff --git a/src/routes/api/v1/channels.ts b/src/routes/api/v1/channels.ts index 7ab2a5f..c83b9db 100644 --- a/src/routes/api/v1/channels.ts +++ b/src/routes/api/v1/channels.ts @@ -3,6 +3,7 @@ import { body, param, validationResult } from "express-validator"; import { authenticateRoute } from "../../../auth"; import { query } from "../../../database"; import { errors } from "../../../errors"; +import { dispatch } from "../../../gateway"; const router = express.Router(); diff --git a/src/types/gatewayclientstate.d.ts b/src/types/gatewayclientstate.d.ts index 72d10c8..387089a 100644 --- a/src/types/gatewayclientstate.d.ts +++ b/src/types/gatewayclientstate.d.ts @@ -3,5 +3,5 @@ interface GatewayClientState { ready: boolean, alive: boolean, lastAliveCheck: number, - broadcastChannels: Set + dispatchChannels: Set }