From 767e436eb8ba3d38bd68e90e18203b039662dce1 Mon Sep 17 00:00:00 2001 From: abearxiong Date: Mon, 23 Jun 2025 10:38:01 +0800 Subject: [PATCH] fix: fix ws --- package.json | 6 ++++-- src/asr/provider/funasr/ws.ts | 12 ++++++++---- src/asr/provider/ws-server.ts | 2 +- src/ws-adapter/index.ts | 23 +++++++++++------------ 4 files changed, 24 insertions(+), 19 deletions(-) diff --git a/package.json b/package.json index 1ec0b6b..9b6c38a 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,8 @@ "types": "types/index.d.ts", "files": [ "dist", - "src" + "src", + "examples" ], "publishConfig": { "access": "public" @@ -83,7 +84,8 @@ "ws": "npm:@kevisual/ws" }, "exports": { - "./src/*": "./src/*" + "./src/*": "./src/*", + "./examples/*": "./examples/*" }, "packageManager": "pnpm@10.11.1" } \ No newline at end of file diff --git a/src/asr/provider/funasr/ws.ts b/src/asr/provider/funasr/ws.ts index db2c267..b25bd53 100644 --- a/src/asr/provider/funasr/ws.ts +++ b/src/asr/provider/funasr/ws.ts @@ -1,4 +1,5 @@ // import WebSocket from 'ws'; +import { EventEmitter } from 'eventemitter3'; import { WSServer, WSSOptions } from '../../provider/ws-server.ts'; export type VideoWSOptions = { @@ -9,6 +10,7 @@ export type VideoWSOptions = { isFile?: boolean; onConnect?: () => void; wav_format?: string; + emitter?: EventEmitter; } & { wsOptions?: WSSOptions['wsOptions']; }; @@ -29,7 +31,7 @@ type OpenRequest = { // '2pass' - 双通道模式, 'online' - 在线模式, 'offline' - 离线模式 mode: VideoWsMode; // 音频格式: - wav_format?: string; + wav_format?: string; // 'wav' - PCM格式, 'mp3' - MP3格式等 // 音频采样率(单位: Hz): audio_fs?: number; // 热词列表: @@ -74,8 +76,6 @@ export class VideoWS extends WSServer { request.wav_format = 'PCM'; request.audio_fs = file_sample_rate; } - console.log('request', request); - this.ws.send(JSON.stringify(request)); } async stop() { @@ -119,7 +119,11 @@ export class VideoWS extends WSServer { const data = event.data; try { const result = JSON.parse(data.toString()); - console.log('result', result); + if (result?.is_final !== undefined && result?.text) { + // console.log('result', result, typeof result); + this.emitter.emit('result', result); + } + console.log('onMessage-result', result); } catch (error) { console.log('error', error); } diff --git a/src/asr/provider/ws-server.ts b/src/asr/provider/ws-server.ts index 0508d35..fceffe0 100644 --- a/src/asr/provider/ws-server.ts +++ b/src/asr/provider/ws-server.ts @@ -45,7 +45,7 @@ export class WSServer { */ async onOpen() { this.connected = true; - this.onConnect(); + this?.onConnect?.(); this.emitter.emit('open'); } /** diff --git a/src/ws-adapter/index.ts b/src/ws-adapter/index.ts index 9060475..dc7e0d5 100644 --- a/src/ws-adapter/index.ts +++ b/src/ws-adapter/index.ts @@ -1,6 +1,13 @@ -const isBrowser = process?.env?.BROWSER === 'true' || typeof window !== 'undefined' && typeof window.document !== 'undefined'; -import { EventEmitter } from 'events'; - +const isBrowser = process?.env?.BROWSER === 'true' || (typeof window !== 'undefined' && typeof window.document !== 'undefined'); +const chantHttpToWs = (url: string) => { + if (url.startsWith('http://')) { + return url.replace('http://', 'ws://'); + } + if (url.startsWith('https://')) { + return url.replace('https://', 'wss://'); + } + return url; +}; type WebSocketOptions = { /** * 是否拒绝不安全的证书, in node only @@ -11,6 +18,7 @@ type WebSocketOptions = { }; export const initWs = async (url: string, options?: WebSocketOptions) => { let ws: WebSocket; + url = chantHttpToWs(url); if (isBrowser) { ws = new WebSocket(url); } else { @@ -30,12 +38,3 @@ interface EventEmitterOptions { */ captureRejections?: boolean | undefined; } -/** - * 初始化一个事件发射器 - * @param opts 事件发射器选项 - * @returns 事件发射器 - */ -export const initEmitter = (opts?: EventEmitterOptions) => { - const emitter = new EventEmitter(opts); - return emitter; -};