refactor(项目): 尝试 Python 语音识别和内容发送

This commit is contained in:
himeditator
2025-06-17 21:26:16 +08:00
parent 1e83ad2199
commit d1bee65ae1
11 changed files with 158 additions and 357 deletions

View File

@@ -2,6 +2,9 @@ import { app, BrowserWindow } from 'electron'
import { electronApp, optimizer } from '@electron-toolkit/utils'
import { controlWindow } from './control'
import { captionWindow } from './caption'
import { WebSocketConnector } from './wsConnector'
const wsConnector = new WebSocketConnector()
app.whenReady().then(() => {
electronApp.setAppUserModelId('com.himeditator.autocaption')
@@ -15,6 +18,8 @@ app.whenReady().then(() => {
controlWindow.createWindow()
wsConnector.connect()
app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0){
controlWindow.createWindow()

View File

@@ -1,52 +0,0 @@
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 或触发其他操作
}
}

42
src/main/wsConnector.ts Normal file
View File

@@ -0,0 +1,42 @@
import WebSocket from 'ws';
export class WebSocketConnector {
ws: WebSocket | null;
constructor() {
this.ws = null;
}
connect() {
this.ws = new WebSocket('ws://localhost:8765');
this.ws.on('open', () => {
console.log('INFO websocket server connected');
this.send({ message: 'Electron Initialized' });
});
this.ws.on('message', this.handleMessage);
this.ws.on('close', () => {
console.log('INFO websocket connection closed');
});
this.ws.on('error', (error) => {
console.error('ERROR websocket error:', error);
});
}
handleMessage(data: any) {
const message = JSON.parse(data.toString());
console.log('INFO get message from webscoket:', message);
}
send(data: object) {
if (this.ws && this.ws.readyState === WebSocket.OPEN) {
this.ws.send(JSON.stringify(data));
} else {
console.error('ERROR send error: websocket not connected');
}
}
}