You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
185 lines
5.7 KiB
185 lines
5.7 KiB
/*
|
|
* @Author: DUDU
|
|
* @Date: 2022-12-14 14:05:56
|
|
* @LastEditors: DUDU
|
|
* @LastEditTime: 2023-04-10 14:39:18
|
|
* @Description:
|
|
*/
|
|
|
|
import { Toast } from "vant";
|
|
import { onReqNoUpdate, recorderClose, recorderInit, mergeWav } from './record.js';
|
|
import _ from 'lodash';
|
|
// let href = process.env.VUE_APP_BASE_API;
|
|
let href = window.location.origin;
|
|
// let wssHref = 'ws://172.16.18.228:8020'
|
|
let wssHref = "wss" + href.substring(5);
|
|
// const wssHref = "ws://172.16.7.78:20006";
|
|
// class newopenWebSocket{
|
|
// constructor(practiceId, connectTimes, sceneType,_this) {
|
|
// this.practiceId= practiceId
|
|
// this.connectTimes= connectTimes
|
|
// this.sceneType= sceneType
|
|
// this._this = _this
|
|
// this.trainWebsocket = new WebSocket('wss://newtec-sit.trace.com.cn/nls/nls/transcription');
|
|
// this.trainWebsocket.binaryType = "arraybuffer"; // 传输数据类型:ArrayBuffer
|
|
// this.trainWebsocket.onopen = (evt) => websocketOnOpen.call(this, evt);
|
|
// this.trainWebsocket.onmessage = (evt) => websocketOnMessage.call(this, evt);
|
|
// this.trainWebsocket.onerror = (evt) => websocketOnError.call(this, evt);
|
|
// this.trainWebsocket.onclose = (evt) => websocketOnClose.call(this, evt);
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
function openWebSocket(practiceId, connectTimes, sceneType) {
|
|
// let wsUrl = `${wssHref}/trainNls/nls/websocket/${practiceId}/${sessionStorage.getItem(
|
|
// "accessToken"
|
|
// )}/${connectTimes}/${sceneType}`;
|
|
const wsUrl = practiceId.url;
|
|
console.log(`开启长链`, wsUrl);
|
|
// this.trainWebsocket = new WebSocket(wsUrl);
|
|
this.trainWebsocket = new WebSocket(wsUrl);
|
|
// document.addEventListener('closeWebSocket', () => {
|
|
// this.trainWebsocket?.close();
|
|
// });
|
|
this.trainWebsocket.binaryType = "arraybuffer"; // 传输数据类型:ArrayBuffer
|
|
this.trainWebsocket.onopen = (evt) => websocketOnOpen.call(this, evt);
|
|
this.trainWebsocket.onmessage = (evt) => websocketOnMessage.call(this, evt);
|
|
this.trainWebsocket.onerror = (evt) => websocketOnError.call(this, evt);
|
|
this.trainWebsocket.onclose = (evt) => websocketOnClose.call(this, evt);
|
|
}
|
|
|
|
function websocketOnOpen() {
|
|
this.connectStatus = "success";
|
|
const param = {
|
|
token: this.asrData.token,
|
|
appId: this.asrData.appId,
|
|
bizNo: "",
|
|
reqNo: "",
|
|
rate: "16k",
|
|
language: "1",
|
|
resId: "",
|
|
audioStatus: "2",
|
|
eos: "3500",
|
|
bos: "5000",
|
|
voiceData: "",
|
|
isBackMid: "",
|
|
}
|
|
if (this.trainWebsocket.readyState == 1) {
|
|
recorderInit.call(this);
|
|
this.recorderEnded = false;
|
|
setTimeout(() => {
|
|
this.asrLoading.close();
|
|
this.asrLoadingState = false;
|
|
}, 300);
|
|
this.oneMinLimit = setTimeout(() => {
|
|
this.oneMinLoading = Toast.loading({
|
|
message: "5秒后停止录音",
|
|
duration: 5000,
|
|
loadingType: "spinner",
|
|
forbidClick: true,
|
|
});
|
|
this.oneMinLimit = setTimeout(() => {
|
|
mergeWav.call(this);
|
|
this.recorderEnded = true;
|
|
this.handlerTouchend();
|
|
this.text = '长按开始录音';
|
|
}, 5000);
|
|
}, 55000);
|
|
this.trainWebsocket.send(JSON.stringify(param));
|
|
}
|
|
|
|
// Toast("语音识别开启");
|
|
this.text = '松开发送录音'
|
|
console.log("🍯 websocket连接成功");
|
|
}
|
|
|
|
function websocketOnMessage(evt) {
|
|
console.log("🍯 websocket返回数据---data:", evt);
|
|
// const { content, practiceId, isEnd } = JSON.parse(evt.data);
|
|
|
|
|
|
// if (userMessage) {
|
|
// this.voiceContent = userMessage
|
|
// } else {
|
|
// if (this.identifyingTimer) clearInterval(this.identifyingTimer);
|
|
// }
|
|
// console.log('websocket触发触发触发触发触发触触发触发。。。。。。。。。。。。。。。。。。。。。。。');
|
|
// console.log('websocket返回参数:', evt.data);
|
|
const data = JSON.parse(evt.data);
|
|
if (data.code === '0000') {
|
|
const msg = data.content?.voiceMessage;
|
|
// console.log(data, '收到消息');
|
|
// let { textValue } = this;
|
|
if (msg !== undefined) {
|
|
this.talkList.push(msg)
|
|
}
|
|
if (this.recorderEnded) { // 松手后拿到答案,则清除定时器,并直接发送
|
|
setTimeout(() => {
|
|
clearTimeout(this.sendMsgTimeout)
|
|
this.closeAndSendMsg();
|
|
}, 100);
|
|
}
|
|
updateParameter(data.content.reqNo)
|
|
} else {
|
|
console.log(evt.data);
|
|
this.stop();
|
|
if (data.message == '上传音频出错,错误码为:40005') {
|
|
if (!this.recorderEnded) {
|
|
Toast('长时间未输入,语音识别断开')
|
|
}
|
|
this.longTimeNoSay = true;
|
|
this.recorderEnded = true;
|
|
this.stop();
|
|
} else {
|
|
Toast('语音识别错误,请重新打开语音功能')
|
|
}
|
|
}
|
|
}
|
|
|
|
function websocketOnError(evt) {
|
|
this.connectStatus = "error";
|
|
console.log("🍯 websocket连接失败:", evt);
|
|
websocketReconnect.call(this);
|
|
}
|
|
|
|
function websocketOnClose(evt) {
|
|
console.log("🍯 websocket关闭:", evt);
|
|
setTimeout(() => {
|
|
// Toast("语音识别关闭");
|
|
this.asrLoading.close();
|
|
this.asrLoadingState = false;
|
|
this.asrTransLoading?.close();
|
|
this.stop()
|
|
}, 500);
|
|
this.isShowClose = false
|
|
this.connectStatus = "close";
|
|
this.activeCloseFlag = true // 关闭websocker标识
|
|
this.trainWebsocket = null
|
|
}
|
|
|
|
// 重连
|
|
function websocketReconnect() {
|
|
if (this.trainWebsocket && this.trainWebsocket.readyState !== 1) {
|
|
if (this.connectTimes > 10) {
|
|
Toast("网络开小差了,请返回重新进入");
|
|
} else {
|
|
this.reconnectFlag = true;
|
|
// Toast("连接已断开,正在重连...");
|
|
this.connectTimes++;
|
|
openWebSocket.call(
|
|
this,
|
|
this.practiceId,
|
|
this.connectTimes,
|
|
this.sceneType
|
|
);
|
|
}
|
|
}
|
|
}
|
|
// 导出 updateParameter 函数
|
|
function updateParameter(newReqNo) {
|
|
onReqNoUpdate(newReqNo);
|
|
}
|
|
|
|
export { openWebSocket };
|
|
|