mirror of
https://github.com/HiMeditator/auto-caption.git
synced 2026-02-14 03:24:44 +08:00
refactor(项目): 尝试 Python 语音识别和内容发送
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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
42
src/main/wsConnector.ts
Normal 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');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user