mirror of
https://github.com/HiMeditator/auto-caption.git
synced 2026-02-21 00:14:42 +08:00
feat(python-subprocess): 尝试字幕显示新解决方案
- 使用 python 子进程解析字幕 - 通过 websocket 通信将字幕传递给软件
This commit is contained in:
@@ -3,6 +3,16 @@ import path from 'path'
|
||||
import { electronApp, optimizer, is } from '@electron-toolkit/utils'
|
||||
import icon from '../../resources/icon.png?asset'
|
||||
|
||||
import { PythonConnector } from './pyComm';
|
||||
|
||||
const pythonConnector = new PythonConnector();
|
||||
setTimeout(() => {
|
||||
pythonConnector.send({
|
||||
command: 'process_data',
|
||||
payload: { some: 'data' }
|
||||
});
|
||||
}, 2000);
|
||||
|
||||
let mainWindow: BrowserWindow | undefined
|
||||
|
||||
function createMainWindow(): void {
|
||||
@@ -32,7 +42,7 @@ function createMainWindow(): void {
|
||||
if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
|
||||
mainWindow.loadURL(process.env['ELECTRON_RENDERER_URL'])
|
||||
} else {
|
||||
mainWindow.loadFile(path.join(__dirname, '../renderer/index.html'))
|
||||
mainWindow.loadFile(path.join(__dirname, '../renderer/main/index.html'))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
52
src/main/pyComm.ts
Normal file
52
src/main/pyComm.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import WebSocket from 'ws';
|
||||
|
||||
export class PythonConnector {
|
||||
ws: WebSocket | null;
|
||||
|
||||
constructor() {
|
||||
this.ws = null;
|
||||
this.connect();
|
||||
}
|
||||
|
||||
connect() {
|
||||
this.ws = new WebSocket('ws://localhost:8765');
|
||||
|
||||
this.ws.on('open', () => {
|
||||
console.log('Python server connected');
|
||||
this.send({ message: 'Electron Initialized' });
|
||||
});
|
||||
|
||||
this.ws.on('message', (data) => {
|
||||
const message = JSON.parse(data.toString());
|
||||
console.log('Get message from Python:', message);
|
||||
|
||||
// 在这里处理来自 Python 的消息
|
||||
if (message.notification) {
|
||||
this.handleNotification(message.notification);
|
||||
}
|
||||
});
|
||||
|
||||
this.ws.on('close', () => {
|
||||
console.log('Connection closed. Reconnecting...');
|
||||
setTimeout(() => this.connect(), 3000);
|
||||
});
|
||||
|
||||
this.ws.on('error', (error) => {
|
||||
console.error('WebSocket Error:', error);
|
||||
});
|
||||
}
|
||||
|
||||
send(data) {
|
||||
if (this.ws && this.ws.readyState === WebSocket.OPEN) {
|
||||
this.ws.send(JSON.stringify(data));
|
||||
} else {
|
||||
console.error('WebSocket not connected');
|
||||
}
|
||||
}
|
||||
|
||||
handleNotification(notification) {
|
||||
// 处理 Python 主动推送的通知
|
||||
console.log('Handel notification:', notification);
|
||||
// 可以在这里更新 UI 或触发其他操作
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user