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.
 
 
 
 

247 lines
6.9 KiB

// 心跳间隔
const HEART_BEAT_TIME = 15000;
// 心跳最大失败次数(超过此次数重连)
const HEART_BEAT_FAIL_NUM = 1;
// 重连间隔
const RECONNECT_TIME = 3000;
import { generateRequestId1 } from "./util";
export default class Audio {
constructor(option) {
this._options = option;
this.socket = null;
this.session_id = null;
this.selfCloseStatus = false
this.connectSocketTimeOut = null
this.appkey = 'hi93syrOZPxi7AZr';
this.token = '66df6ddcb4b643f08450cdf77d3de768';
}
getToken() {
return new Promise((resolve, reject) => {
console.log("-----开始请求token-----");
uni.request({
method: "GET",
dataType: "json",
// url: `http://192.168.124.118:8083/xcx/framework/agent/${this._options.agentId}`,
url: `https://des.dayunyuanjian.cn/getDemoToken?id=${this._options.agentId}`,
success: (res) => {
console.log("请求token成功", res);
resolve(res);
},
fail: (err) => {
wx.showToast({
title: "创建失败",
icon: "none",
});
console.log("请求token失败", err);
reject();
},
});
});
}
async init() {
return this.createSocket();
}
async createSocket(options) {
console.log("开始创建socket", options);
return new Promise(async (resolve, reject) => {
const origin = `wss://nls-gateway-cn-beijing.aliyuncs.com/ws/v1?token=${this.token}`;
// 建立连接
const socket = wx.connectSocket({
url: `${origin}`,
binaryType: 'arraybuffer',
success: (e) => {
console.log("创建语音长链接成功", e);
},
complete: (e) => {
console.log("socket - complete", e);
},
});
this.socket = socket;
socket.onOpen((e) => {
console.log("socket.onOpen", e);
// 监听发送
options && options.complete && options.complete();
const socketParams = {
header: {
name: 'StartSynthesis',
appkey: this.appkey,
message_id: '1c17d4f8e7894a20aecb9f774b54ba06', //generateRequestId1(),
task_id: '1c17d4f8e7894a20aecb9f774b54ba06',//generateRequestId1(),
namespace: 'FlowingSpeechSynthesizer'
},
payload: {
voice: 'xiaoyun',
format: 'pcm',
sample_rate: 16000
}
};
this.send({ data: JSON.stringify(socketParams) }, 'txt');
});
socket.onMessage((e) => {
const { data } = e;
if (data instanceof ArrayBuffer) {
this._options.onMessage && this._options.onMessage(data);
} else {
if ( Object.prototype.toString.call(data) === "[object String]" ) {
const tmp = JSON.parse(data);
if (Object.prototype.toString.call(tmp.type) === "[object Number]") {
this._options.onMessage && this._options.onMessage(tmp);
} else {
if (!this.session_id) {
this.session_id = tmp.data
}
}
}
}
// this.on(type, e);
this.createInter();
resolve();
});
// 失败
socket.onError((e) => {
console.log("websocket error 创建语音长链接报错", e);
//
});
// 关闭
socket.onClose((e) => {
console.log("websocket close 语音长链接关闭", e);
this.connectSocketTimeOut && clearTimeout(this.connectSocketTimeOut);
//非自动关闭重连
if (e.code != 1000) {
this.doConnectTimeout();
}
});
});
}
doConnectTimeout() {
if (this.selfCloseStatus){
// 主动退出 不在重连
return
}
// 重连一次
console.log("websocket 异常关闭 开始重连");
this.connectSocketTimeOut = setTimeout(() => {
this.createSocket({
complete: function (res) {
// this.reconnectLock = false;
},
});
}, RECONNECT_TIME);
}
send(e, t) {
console.log("开始请求 websocket send", e);
this.socket && this.socket.send(e);
}
getMsgData(e) {
if (e && typeof e == "string") {
const status = e.indexOf("42");
const index = e.indexOf("[");
if (status > -1 && index > -1) {
const txt = e.substring(index);
const item = JSON.parse(txt);
const [type, obj] = item;
const payload = obj.payload ? obj.payload : {};
const params = {
chatId: this._options.agentId,
type,
contentType: "text", //默认文字
timestamp: new Date().getTime(),
...payload,
};
// 缓存聊天记录
return params;
}
}
return null;
}
emit(type, text, contentType) {
console.log("emit", type, text, generateRequestId1());
switch (type) {
case "send":
// 发送消息
// 发送消息
// const socketParams = {
// request_id: generateRequestId1()
// };
const socketParams = {
header: {
name: 'RunSynthesis',
appkey: this.appkey,
namespace: 'FlowingSpeechSynthesizer',
message_id: '1c17d4f8e7894a20aecb9f774b54ba06',
task_id: '1c17d4f8e7894a20aecb9f774b54ba06', // generateRequestId1(),
},
payload: {
text: text
}
// action: "ACTION_SYNTHESIS",
// data: text,
};
this.send({ data: JSON.stringify(socketParams) }, contentType);
break;
}
}
// 监听
on(type, params) {
switch (type) {
case "reply":
// 回复结束
const tmpParams = {
...params,
name: this.robotObj.name,
headImage: this.robotObj.headImage,
};
if (
params &&
params.type === "reply" &&
!params.is_from_self &&
params.can_rating
) {
// 回复消息
console.log("reply回复内容", tmpParams.content, tmpParams);
this._options.onMessage && this._options.onMessage(tmpParams);
}
if (!params.is_from_self && params.is_final) {
if (this.failMsg[tmpParams.request_id]) {
this.failMsg[tmpParams.request_id].status = true;
// 已回复 状态为true
}
// 回复结束 写入缓存
// setMsgData(tmpParams);
}
// 回复
break;
}
}
createInter() {
if (this.timeoutObj) {
clearTimeout(this.timeoutObj);
}
this.timeoutObj = setTimeout(() => {
console.log(111111, this.socket)
this.socket && this.socket.send && this.socket.send({ data: 3 });
}, HEART_BEAT_TIME);
}
// 关闭socket
destroy() {
if (this.socket && this.socket.readyState == 1) {
this.socket && this.socket.close();
this.socket = null;
}
}
}