Skip to content

Commit e1e283a

Browse files
authored
Merge pull request #137 from watson-developer-cloud/master
Rebasing
2 parents 6324564 + c706e07 commit e1e283a

File tree

1 file changed

+48
-32
lines changed
  • services/speech_to_text

1 file changed

+48
-32
lines changed

services/speech_to_text/v1.js

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -355,23 +355,12 @@ module.exports = function (RED) {
355355
return p;
356356
}
357357

358-
// If we are going to connect to STT through websockets then its going to
359-
// disconnect or timeout, so need to handle that occurrence.
360-
function connectIfNeeded() {
361-
console.log('re-establishing the connect');
362-
websocket = null;
363-
socketCreationInProcess = false;
364-
processSTTSocketStart(false)
365-
.then(() => {
366-
//return Promise.resolve();
367-
return;
368-
})
369-
.catch((err) => {
370-
//return Promise.resolve();
371-
return;
372-
});
373-
}
374358

359+
// This function generates a load of listeners, that if resolving or
360+
// rejecting promises causes problems, as nothing is waiting on those
361+
// promises. I had wanted to pause and socket activity until the 'open'
362+
// event, which was ok initially, but on subsequent socket close / Error
363+
// reopens caused problems.
375364
function processSTTSocketStart(initialConnect) {
376365
var p = new Promise(function resolver(resolve, reject) {
377366
var model = config.lang + '_' + config.band;
@@ -398,44 +387,71 @@ module.exports = function (RED) {
398387

399388
ws.on('message', (data) => {
400389
// First message will be 'state': 'listening'
401-
console.log('-----------------------');
402-
console.log('Data Received from Input');
403-
console.log(data);
390+
// console.log('-----------------------');
391+
// console.log('Data Received from Input');
392+
// console.log(data);
404393
var d = JSON.parse(data);
405394
var newMsg = {payload : JSON.parse(data)};
406-
node.send(newMsg);
407-
if (d && d.state && 'listening' === d.state){
408-
socketListening = true;
409-
resolve();
395+
if (d) {
396+
if (d.error) {
397+
// Force Expiry of Token, as that is the only Error
398+
// response from the service that we have seen.
399+
token = null;
400+
getToken(determineService())
401+
.then(() => {
402+
return;
403+
});
404+
} else if (d && d.state && 'listening' === d.state) {
405+
socketListening = true;
406+
//resolve();
407+
} else {
408+
node.send(newMsg);
409+
}
410410
}
411411
});
412412

413413
ws.on('close', () => {
414-
//if (websocket) {
415-
// websocket.close();
416-
//}
417414
websocket = null;
418415
socketListening = false;
419-
console.log('STT Socket disconnected');
416+
// console.log('STT Socket disconnected');
420417
setTimeout(connectIfNeeded, 1000);
421418
});
422419

423420
ws.on('error', (err) => {
424421
socketListening = false;
425-
console.log('Error Detected');
422+
// console.log('Error Detected');
426423
if (initialConnect) {
427-
reject(err);
424+
//reject(err);
428425
}
429426
});
430427

431-
} else {
432-
resolve();
433428
}
434-
429+
resolve();
435430
});
436431
return p;
437432
}
438433

434+
// If we are going to connect to STT through websockets then its going to
435+
// disconnect or timeout, so need to handle that occurrence.
436+
function connectIfNeeded() {
437+
// console.log('re-establishing the connect');
438+
websocket = null;
439+
socketCreationInProcess = false;
440+
441+
// The token may have expired so test for it.
442+
getToken(determineService())
443+
.then(() => {
444+
return processSTTSocketStart(false);
445+
})
446+
.then(() => {
447+
//return Promise.resolve();
448+
return;
449+
})
450+
.catch((err) => {
451+
//return Promise.resolve();
452+
return;
453+
});
454+
}
439455

440456
// While we are waiting for a connection, stack the data input
441457
// so it can be processed, when the connection becomes available.

0 commit comments

Comments
 (0)