websocket stuff
This commit is contained in:
4262
package-lock.json
generated
4262
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -2,9 +2,8 @@ import {
|
||||
BaseService,
|
||||
FullInformation,
|
||||
HomeAssistantDeskPositionResult,
|
||||
SseClientChangeEvent,
|
||||
SseService,
|
||||
TidalGetCurrent,
|
||||
WsService,
|
||||
type ServiceResult,
|
||||
} from "@dpu/shared";
|
||||
import { logInfo, logWarning } from "@dpu/shared/dist/logger.js";
|
||||
@@ -14,19 +13,19 @@ import { TidalService } from "../tidal/service";
|
||||
export class HomepageService extends BaseService<null> {
|
||||
private haService: HomeAssistantService;
|
||||
private tidalService: TidalService;
|
||||
private sseService: SseService;
|
||||
private wsService: WsService;
|
||||
private pollingInterval: ReturnType<typeof setInterval> | null = null;
|
||||
private lastPoll: FullInformation | null = null;
|
||||
|
||||
constructor(
|
||||
haService: HomeAssistantService,
|
||||
tidalService: TidalService,
|
||||
sseService: SseService,
|
||||
wsService: WsService,
|
||||
) {
|
||||
super(null);
|
||||
this.haService = haService;
|
||||
this.tidalService = tidalService;
|
||||
this.sseService = sseService;
|
||||
this.wsService = wsService;
|
||||
this.listenForClientChange();
|
||||
}
|
||||
|
||||
@@ -87,7 +86,7 @@ export class HomepageService extends BaseService<null> {
|
||||
|
||||
if (updates.length > 0) {
|
||||
logInfo("Updating clients");
|
||||
this.sseService.notifyClients({
|
||||
this.wsService.broadcast({
|
||||
type: "update",
|
||||
data: updates,
|
||||
});
|
||||
@@ -98,8 +97,8 @@ export class HomepageService extends BaseService<null> {
|
||||
}
|
||||
|
||||
listenForClientChange(): void {
|
||||
this.sseService.onClientChange((clientChange: SseClientChangeEvent) => {
|
||||
if (clientChange.clientCount === 0) {
|
||||
this.wsService.onClientChange((clients: number) => {
|
||||
if (clients === 0) {
|
||||
this.stopPolling();
|
||||
} else {
|
||||
if (!this.pollingInterval) {
|
||||
|
||||
10
src/index.ts
10
src/index.ts
@@ -19,12 +19,12 @@ import { HomeAssistantService } from "./homeassistant/service.js";
|
||||
import { TidalClient } from "./tidal/client.js";
|
||||
import { TidalService } from "./tidal/service.js";
|
||||
import { tidalRoutes } from "./tidal/routes.js";
|
||||
import { sseRoutes } from "./websocket/routes.js";
|
||||
import { SseService } from "@dpu/shared";
|
||||
import { wsRoutes } from "./websocket/routes.js";
|
||||
import { HomepageService } from "./homepage/service.js";
|
||||
import { homepageRoutes } from "./homepage/routes.js";
|
||||
import fastifyCors from "@fastify/cors";
|
||||
import fastifyWebsocket from "@fastify/websocket";
|
||||
import { WsService } from "@dpu/shared";
|
||||
|
||||
const fastify = Fastify().withTypeProvider<ZodTypeProvider>();
|
||||
|
||||
@@ -89,9 +89,9 @@ const haService = new HomeAssistantService(haClient);
|
||||
const tidalClient = new TidalClient(fastify.axios.tidal);
|
||||
const tidalService = new TidalService(tidalClient);
|
||||
|
||||
const sseService = new SseService();
|
||||
const wsService = new WsService();
|
||||
|
||||
const hpService = new HomepageService(haService, tidalService, sseService);
|
||||
const hpService = new HomepageService(haService, tidalService, wsService);
|
||||
|
||||
async function verifyAPIKey(
|
||||
request: FastifyRequest,
|
||||
@@ -110,7 +110,7 @@ const port = parseInt(Config.port, 10);
|
||||
// Register routes
|
||||
await fastify.register(homeAssistantRoutes, { haService, verifyAPIKey });
|
||||
await fastify.register(tidalRoutes, { tidalService, verifyAPIKey });
|
||||
await fastify.register(sseRoutes, { sseService });
|
||||
await fastify.register(wsRoutes, { wsService });
|
||||
await fastify.register(homepageRoutes, { hpService });
|
||||
|
||||
fastify.get(
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import { logInfo, type SseEvent, type SseService } from "@dpu/shared";
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { logInfo, WsService } from "@dpu/shared";
|
||||
|
||||
export async function sseRoutes(
|
||||
export async function wsRoutes(
|
||||
fastify: FastifyInstance,
|
||||
{
|
||||
sseService,
|
||||
wsService,
|
||||
}: {
|
||||
sseService: SseService;
|
||||
wsService: WsService;
|
||||
},
|
||||
) {
|
||||
fastify.get(
|
||||
@@ -21,21 +20,13 @@ export async function sseRoutes(
|
||||
websocket: true,
|
||||
},
|
||||
(socket, _request) => {
|
||||
const clientId = randomUUID();
|
||||
const sendEvent = (event: SseEvent) => {
|
||||
if (socket.readyState === socket.OPEN) {
|
||||
socket.send(JSON.stringify({ event: event.type, data: event.data }));
|
||||
}
|
||||
};
|
||||
|
||||
sseService.addClient({ id: clientId, send: sendEvent });
|
||||
|
||||
wsService.addClient(socket);
|
||||
socket.send(JSON.stringify({ event: "connected", data: "Connected" }));
|
||||
logInfo(`Connection for client ${clientId} established`);
|
||||
logInfo(`Connection for client established`);
|
||||
|
||||
socket.on("close", () => {
|
||||
sseService.removeClient(clientId);
|
||||
logInfo(`Connection for client ${clientId} closed`);
|
||||
wsService.removeClient(socket);
|
||||
logInfo(`Connection for client closed`);
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user