当前位置 : 主页 > 网页制作 > Nodejs >

node.js – microsoft-cognitive:在NodeJS中为Bing Speech to Text服务获取ECONNRESET错误

来源:互联网 收集:自由互联 发布时间:2021-06-16
我正在尝试使用bing语音来从Node发送文本服务.流式传输.wav文件时出现以下错误.非常感谢任何帮助. events.js:154 throw er; // Unhandled 'error' event ^Error: read ECONNRESET at exports._errnoException (util.js
我正在尝试使用bing语音来从Node发送文本服务.流式传输.wav文件时出现以下错误.非常感谢任何帮助.

events.js:154
      throw er; // Unhandled 'error' event
     ^
Error: read ECONNRESET
    at exports._errnoException (util.js:890:11)
    at TLSWrap.onread (net.js:550:26)

Http选项如下:

var post_option = {
            hostname: 'speech.platform.bing.com',
            path: '/recognize',
            method: 'POST'
        };
        post_option.headers = {
            'Content-Type': 'audio/wav; samplerate=16000', //'audio/wav; codec=”audio/wav”; samplerate=16000; sourcerate=8000; trustsourcerate=false',
            // 'keepAlive': true,
            // 'Content-Length' : waveData.length,
            'Authorization': 'Bearer ' + OxfordAccessToken.access_token,
            // "User-Agent": "TTSNodeJS",
            'X-Search-AppId': '______',
            'X-Search-ClientID': '______'
        };

        post_option.qs = {
            'scenarios': 'ulm',
            'appid': '--------', // This magic value is required
            'locale': 'en-US',
            'device.os': 'wp7',
            'version': '3.0',
            'format': 'json',
            'requestid': '------', // can be anything
            'instanceid': '------' // can be anything   
        };

NodeJS调用SpeechToText服务

res.on('end', function(){
                OxfordAccessToken = eval ('(' + _data + ')');       
                var voiceStream = fs.createReadStream("voiceresponse.wav",{encoding: "binary"});
                var https = require('https');
                var post_req = https.request(getPostOptionsForSTT(OxfordAccessToken), (res) =>{
                    if(err){
                        console.log("Error during STT");
                    }
                    var sttResponse;
                    res.on('data', function(buffer){
                         sttResponse += buffer;
                    });
                    console.log("STT Results : "+sttResponse);
                });
                voiceStream.pipe(post_req,{ end: false });
raserside自己回答了这个问题,但这是代码,为了节省一些时间:

const querystring = require('querystring'); // or query-string, or qs

var params = {
    'scenarios': 'ulm',
    'appid': 'D4D52672-91D7-4C74-8AD8-42B1D98141A5',
    'locale': 'en-US',
    'device.os': 'wp7',
    'version': '3.0',
    'format': 'json',
    'requestid': '------', // can be anything
    'instanceid': '------' // can be anything   
};

var post_option = {
    hostname: 'speech.platform.bing.com',
    path: '/recognize?' + querystring.stringify(params),
    method: 'POST'
};

post_option.headers = {
    'Content-Type': 'audio/wav; samplerate=16000', // For example
    'Authorization': 'Bearer ' + OxfordAccessToken.access_token
};
网友评论