问题描述
在服务器端代码中,当客户端从服务器端接收到转录文本时,我们尝试使用Web套接字连接将转录文本传递给client(Angular)。 它会覆盖文本,有时有时会多次写入同一文本。我尝试使用.isFinal,但问题仍然相同。下面是代码段。
服务端代码:
const request = {
config: {
encoding: "MULAW",sampleRateHertz: 8000,languageCode: "pt-PT"
},singleUtterance : false,interimResults: true
};
recognizeStream = client
.streamingRecognize(request)
.on("error",console.error)
.on("data",data => {
//data.results[0].isFinal = true
console.log(data.results[0].alternatives[0].transcript);
wss.clients.forEach( client => {
if (client.readyState === WebSocket.OPEN) {
if (data.results[0].isFinal) {
client.send(
JSON.stringify({
event: "interim-transcription",text: data.results[0].alternatives[0].transcript
})
)
}
}
});
});
客户端代码:-
if (data.event === "interim-transcription") {
websocket.addEventListener("message",(msg)=>{
console.log("Message from websocket"+msg)
const data = JSON.parse(msg.data);
content = document.createElement('span')
content.setAttribute('class','my-1')
content.innerHTML = '<span style="font-weight: bold;">Customer: </span>' + data.text
document.getElementById("transcription-container").appendChild(content)
}
}
注意:Nodejs和Express js从服务器端和客户端:-Angular 8
任何帮助将不胜感激。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)