mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-02-28 06:22:45 -08:00
refactor(core): normalize core service naming
Standardize core service module and export names to reduce naming ambiguity and make imports predictable across runtime, tests, scripts, and docs.
This commit is contained in:
60
src/core/services/subtitle-ws.ts
Normal file
60
src/core/services/subtitle-ws.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import * as fs from "fs";
|
||||
import * as os from "os";
|
||||
import * as path from "path";
|
||||
import WebSocket from "ws";
|
||||
import { createLogger } from "../../logger";
|
||||
|
||||
const logger = createLogger("main:subtitle-ws");
|
||||
|
||||
export function hasMpvWebsocketPlugin(): boolean {
|
||||
const mpvWebsocketPath = path.join(
|
||||
os.homedir(),
|
||||
".config",
|
||||
"mpv",
|
||||
"mpv_websocket",
|
||||
);
|
||||
return fs.existsSync(mpvWebsocketPath);
|
||||
}
|
||||
|
||||
export class SubtitleWebSocket {
|
||||
private server: WebSocket.Server | null = null;
|
||||
|
||||
public isRunning(): boolean {
|
||||
return this.server !== null;
|
||||
}
|
||||
|
||||
public start(port: number, getCurrentSubtitleText: () => string): void {
|
||||
this.server = new WebSocket.Server({ port, host: "127.0.0.1" });
|
||||
|
||||
this.server.on("connection", (ws: WebSocket) => {
|
||||
logger.info("WebSocket client connected");
|
||||
const currentText = getCurrentSubtitleText();
|
||||
if (currentText) {
|
||||
ws.send(JSON.stringify({ sentence: currentText }));
|
||||
}
|
||||
});
|
||||
|
||||
this.server.on("error", (err: Error) => {
|
||||
logger.error("WebSocket server error:", err.message);
|
||||
});
|
||||
|
||||
logger.info(`Subtitle WebSocket server running on ws://127.0.0.1:${port}`);
|
||||
}
|
||||
|
||||
public broadcast(text: string): void {
|
||||
if (!this.server) return;
|
||||
const message = JSON.stringify({ sentence: text });
|
||||
for (const client of this.server.clients) {
|
||||
if (client.readyState === WebSocket.OPEN) {
|
||||
client.send(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public stop(): void {
|
||||
if (this.server) {
|
||||
this.server.close();
|
||||
this.server = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user