diff --git a/src/api.ts b/src/api.ts deleted file mode 100644 index 643945e..0000000 --- a/src/api.ts +++ /dev/null @@ -1,50 +0,0 @@ -import { WebSocket, WebSocketServer } from "ws"; -import { log } from "./log.js"; -import { timeline } from "./scheduling/Timeline.js"; -import { getRemoteAddress } from "./api/getRemoteAddress.js"; -import type { IncomingMessage } from "http"; - -const UPDATE_INTERVAL_MS = 200 as const; -let updateTimer: NodeJS.Timer | null = null; -let wsServer: WebSocketServer | null = null; - -const sendUpdate = () => { - if (!wsServer) { - log.warn(`Websocket server not initialized`); - return; - } - - if (!wsServer.clients.size) { - if (updateTimer) clearInterval(updateTimer); - return; - } - - const now = new Date(); - const message = JSON.stringify({ - time: now.toISOString(), - timeline: timeline.getItems(), - }); - - wsServer.clients.forEach((sock) => sock.send(message)); -}; -const handleConnection = (wsSocket: WebSocket, incoming: IncomingMessage) => { - log.info(`Websocket client connected from ${getRemoteAddress(incoming)}`); - - sendUpdate(); - if (!updateTimer) updateTimer = setInterval(sendUpdate, UPDATE_INTERVAL_MS); - - wsSocket.on("error", log.error); - - wsSocket.on("close", () => { - log.info(`Websocket client disconnected`); - }); - - wsSocket.on("message", (data: any) => { - log.warn(`Received message ${data} on read-only socket`); - }); -}; - -export const startWebsocketServer = () => { - wsServer = new WebSocketServer({ port: 8080 }); - wsServer.on("connection", handleConnection); -}; diff --git a/src/api/server.ts b/src/api/server.ts new file mode 100644 index 0000000..d647a92 --- /dev/null +++ b/src/api/server.ts @@ -0,0 +1,58 @@ +import { WebSocket, WebSocketServer } from "ws"; +import { log } from "../log.js"; +import { timeline } from "../scheduling/Timeline.js"; +import { getRemoteAddress } from "./getRemoteAddress.js"; +import type { IncomingMessage } from "http"; + +const UPDATE_INTERVAL_MS = 200 as const; +let updateTimer: NodeJS.Timer | null = null; +let wsServer: WebSocketServer | null = null; + +const sendUpdate = () => { + if (!wsServer) { + log.warn(`sendUpdate called with no wsServer - shouldn't happen`); + if (updateTimer) { + log.warn(`update timer still running - shouldn't happen - clearing it`); + clearInterval(updateTimer); + } + return; + } + + if (!wsServer.clients.size) { + if (updateTimer) { + log.debug(`No websocket clients connected, clearing update timer`); + clearInterval(updateTimer); + } + return; + } + + const now = new Date(); + const message = JSON.stringify({ + time: now.toISOString(), + timeline: timeline.getItems(), + }); + + wsServer.clients.forEach((sock) => sock.send(message)); +}; +const handleConnection = (sock: WebSocket, incoming: IncomingMessage) => { + log.info(`Websocket client connected from ${getRemoteAddress(incoming)}`); + + sendUpdate(); + if (!updateTimer) updateTimer = setInterval(sendUpdate, UPDATE_INTERVAL_MS); + + sock.on("error", log.error); + + sock.on("close", (code, reason) => { + log.info(`Websocket client disconnected (${code}, reason: "${reason}")`); + }); + + sock.on("message", (data: any) => { + log.warn(`Received message ${data} on read-only socket`); + }); +}; + +export const startWebsocketServer = (port: number) => { + log.info(`Starting websocket server on port ${port}`); + wsServer = new WebSocketServer({ port: port }); + wsServer.on("connection", handleConnection); +}; diff --git a/src/index.ts b/src/index.ts index 36b76cd..e806ffd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,7 +7,7 @@ import { connection } from "./connection.js"; import { endOfDay, startOfDay } from "date-fns"; import { timeline } from "./scheduling/Timeline.js"; import { makeTestSchedule } from "./scheduling/testUtils.js"; -import { startWebsocketServer } from "./api.js"; +import { startWebsocketServer } from "./api/server.js"; OpenAPI.BASE = FK_API; @@ -60,7 +60,7 @@ const runPlayout = async () => { (async () => { try { log.info(`Starting playout at ${new Date().toLocaleString()}`); - await startWebsocketServer(); + await startWebsocketServer(8080); await runPlayout(); } catch (e) { log.error(e); diff --git a/src/scheduling/InterstitialGraphics.ts b/src/scheduling/InterstitialGraphics.ts index f404b66..9da9c57 100644 --- a/src/scheduling/InterstitialGraphics.ts +++ b/src/scheduling/InterstitialGraphics.ts @@ -51,9 +51,9 @@ export class InterstitialGraphics implements ScheduleItem { async arm() { const now = new Date(); const { startsAt, endsAt, load, play, stop, clear } = this; - const loadAt = sub(startsAt, { seconds: 1 }); + const loadAt = sub(startsAt, { seconds: 10 }); const playAt = subMilliseconds(startsAt, 500); - const clearAt = add(endsAt, { seconds: 2 }); + const clearAt = add(endsAt, { seconds: 10 }); if (endsAt <= now) { log.debug(