ws that starts to work

This commit is contained in:
polv committed 2026-01-25 19:52:12 +07:00
1 parent fc58e68ad7
commit 0f4458912f
7 files changed
+178 -47

No files matched your search

+58
View File
@@ -0,0 +1,58 @@
// @ts-check
import { WebSocketServer } from 'ws';
import { readFileSync, mkdirSync, writeFileSync } from "fs";
const PORT = 8080
const wss = new WebSocketServer({ port: PORT });
console.log(`WebSocket server running on ws://localhost:${PORT}`);
wss.on('connection', (ws) => {
console.log('New client connected');
ws.on('message', (message) => {
const { name, ...payload } = JSON.parse(message.toString('utf-8'));
console.log(name, payload)
switch (name) {
case 'download_q': {
const { q } = payload;
try {
mkdirSync(`docs/${q}`);
} catch (e) {}
let outMd = "";
const re = /^(?<min>\d{1,2}):(?<sec>\d{2})\s*$/;
for (const line of readFileSync(`docs/${q}.md`, "utf-8").trimEnd().split("\n")) {
const m = re.exec(line);
if (m?.groups) {
const { min, sec } = m.groups;
const t = parseInt(min) * 60 + parseInt(sec);
ws.send(JSON.stringify({ name: "seek_q", t }));
outMd += `\n[${line}](./${q}/${t}.png)\n\n`;
} else {
outMd += `${line}\n`;
}
}
writeFileSync(`docs/${q}.md`, outMd);
} break;
case 'timed_q' : {
const { q, t, data } = payload;
writeFileSync(
`docs/${q}/${t}.png`,
data.replace(/^data:image\/png;base64,/, ""),
"base64",
);
} break;
}
});
ws.on('close', () => {
console.log('Client disconnected');
});
});