【问题标题】:WebSocket closing immediately using Watson Speech-to-text JS SDK使用 Watson Speech-to-text JS SDK 立即关闭 WebSocket
【发布时间】:2018-07-19 18:53:02
【问题描述】:

尝试使用提供的演示中的此代码,使用来自https://github.com/watson-developer-cloud/speech-javascript-sdk 的最新 watson-speech.js:

document.querySelector('#button').onclick = function () {

  fetch('myserverurl/api/token')
  .then(function(response) {
      return response.text();
  }).then(function (token) {

    var stream = WatsonSpeech.SpeechToText.recognizeMicrophone({
        token: token,
        outputElement: '#output' // CSS selector or DOM Element
    });

    stream.on('error', function(err) {
        console.log(err);
    });

    document.querySelector('#stop').onclick = function() {
      stream.stop();
    };

  }).catch(function(error) {
      console.log(error);
  });
};

尝试在 Chrome 控制台中启动连接时立即收到此错误: WebSocket 已经处于 CLOSING 或 CLOSED 状态。

使用 Chrome 并在本地进行测试。

【问题讨论】:

    标签: speech-to-text ibm-watson


    【解决方案1】:

    我认为这是一个范围界定错误。您在then 中声明var stream,这意味着一旦then 完成stream 不再存在,并且将被自动清理。

    在更高的范围级别添加var stream = nulllet stream = null。您仍然可以在 then 块中将其初始化为

    }).then(function (token) {
      stream = WatsonSpeech.SpeechToText.recognizeMicrophone({
              token: token,
              outputElement: '#output' // CSS selector or DOM Element
      });
      ...
    

    【讨论】:

    • 试过这个,不幸的是它没有用。出于某种原因,此代码适用于here,但不适用于我。
    猜你喜欢
    • 1970-01-01
    • 2016-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-28
    • 1970-01-01
    • 2019-05-05
    • 1970-01-01
    相关资源
    最近更新 更多