feat: WebSocket 实时识别帧改走独立队列
asr 分片只与同一轮识别串行,不再排在在途模型生成后面, 避免用户边等回复边说话时 PCM 整段堆积;ping 心跳也立即应答
This commit is contained in:
+51
-10
@@ -38,6 +38,10 @@ const ASR_LIMITS = {
|
||||
sniffBytes: 16 * 1024
|
||||
};
|
||||
|
||||
// 实时识别帧走独立队列:识别分片不能被在途的模型生成阻塞,
|
||||
// 否则用户一边等回复一边说话时,PCM 会整段堆到回复结束才被处理
|
||||
const ASR_FRAME_TYPES = new Set(['asr_start', 'asr_chunk', 'asr_stop', 'asr_cancel']);
|
||||
|
||||
/**
|
||||
* WebM 解码出的 PCM 采样率归一
|
||||
*
|
||||
@@ -112,13 +116,13 @@ export default class WebSocketServerManager {
|
||||
_handleChatConnection(ws) {
|
||||
sendFrame(ws, 'system', 'connected', 'Connected to chat server');
|
||||
|
||||
// 同一连接的消息串行处理:agent 的 messages 数组是共享状态,
|
||||
// 并发跑同一个 agent 会让上下文交错、回复错乱
|
||||
// 两条独立队列:
|
||||
// - messageQueue:chat/clear/format,agent 的 messages 是共享状态,必须串行
|
||||
// - asrQueue:实时识别分片只与同一轮识别串行,不能被在途的模型生成堵住
|
||||
ws.messageQueue = Promise.resolve();
|
||||
ws.asrQueue = Promise.resolve();
|
||||
ws.on('message', (data) => {
|
||||
ws.messageQueue = ws.messageQueue
|
||||
.then(() => this._handleMessage(ws, data))
|
||||
.catch((err) => console.error('WebSocket message handling error:', err));
|
||||
this._dispatchMessage(ws, data);
|
||||
});
|
||||
|
||||
ws.on('close', () => {
|
||||
@@ -126,6 +130,7 @@ export default class WebSocketServerManager {
|
||||
this._abortAsrSession(ws);
|
||||
// 断开后释放队列引用,避免连接对象被闭包长期持有
|
||||
ws.messageQueue = null;
|
||||
ws.asrQueue = null;
|
||||
console.log('Chat client disconnected');
|
||||
});
|
||||
|
||||
@@ -134,11 +139,47 @@ export default class WebSocketServerManager {
|
||||
});
|
||||
}
|
||||
|
||||
async _handleMessage(ws, data) {
|
||||
/**
|
||||
* 帧分发:先解析,再按类型放入对应队列
|
||||
*
|
||||
* asr_* 入识别队列,其余聊天帧入聊天队列;ping 立即应答,
|
||||
* 不排在模型生成后面,避免长回复期间心跳超时。
|
||||
*
|
||||
* @param {Object} ws 连接
|
||||
* @param {Buffer|string} data 原始帧
|
||||
* @returns {Promise} 当前帧所在队列的完成 Promise
|
||||
*/
|
||||
_dispatchMessage(ws, data) {
|
||||
let msg;
|
||||
try {
|
||||
const raw = data.toString();
|
||||
msg = JSON.parse(data.toString());
|
||||
} catch (err) {
|
||||
sendFrame(ws, 'system', 'error', '消息格式错误');
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
const msg = JSON.parse(raw);
|
||||
// JSON.parse("null") / 数组 / 标量都能成功解析,但不是合法帧
|
||||
if (!msg || typeof msg !== 'object' || Array.isArray(msg)) {
|
||||
sendFrame(ws, 'system', 'error', '消息格式错误');
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
if (msg.type === 'ping') {
|
||||
sendFrame(ws, 'system', 'pong', '', msg.id);
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
const queueKey = ASR_FRAME_TYPES.has(msg.type) ? 'asrQueue' : 'messageQueue';
|
||||
const previous = ws[queueKey] || Promise.resolve();
|
||||
const current = previous
|
||||
.then(() => this._handleMessage(ws, msg))
|
||||
.catch((err) => console.error('WebSocket message handling error:', err));
|
||||
ws[queueKey] = current;
|
||||
return current;
|
||||
}
|
||||
|
||||
async _handleMessage(ws, msg) {
|
||||
try {
|
||||
// 只记录消息类型:正文含健康描述、token/userId 属敏感信息,不进日志。
|
||||
// 实时识别的分片按秒级频率到达,逐帧打日志会把日志刷满,这里跳过
|
||||
if (msg.type !== 'asr_chunk') {
|
||||
@@ -233,8 +274,8 @@ export default class WebSocketServerManager {
|
||||
|
||||
console.log('[Unknown message type]', msg.type);
|
||||
} catch (e) {
|
||||
console.error('Message parse error:', e);
|
||||
sendFrame(ws, 'system', 'error', '消息格式错误');
|
||||
console.error('WebSocket message handling error:', e);
|
||||
sendFrame(ws, 'system', 'error', '处理消息失败,请稍后重试');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user