25 lines
701 B
TypeScript
25 lines
701 B
TypeScript
import "reflect-metadata";
|
|
import { Server } from "ws";
|
|
import { connect, getRoomList, updateFree } from "./db";
|
|
import { isIdMessage, isMessage } from "./types";
|
|
|
|
(async () => {
|
|
const connection = await connect();
|
|
|
|
const wss = new Server({
|
|
port: Number.parseInt(process.env.PORT || "") || 8081,
|
|
});
|
|
|
|
wss.on("connection", async (ws) => {
|
|
ws.send(JSON.stringify(await getRoomList(connection)));
|
|
|
|
ws.on("message", (data) => {
|
|
const message: unknown = JSON.parse(data.toString());
|
|
if (!isMessage(message)) throw new Error("Message corrupted");
|
|
|
|
if (isIdMessage(message))
|
|
updateFree(connection, message.args.id, message.type === "freed");
|
|
});
|
|
});
|
|
})();
|