Skip to content

Commit

Permalink
updated asr client to v 0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
tbass134 committed Aug 7, 2018
1 parent 9257f8d commit ba39a63
Show file tree
Hide file tree
Showing 8 changed files with 530 additions and 925 deletions.
85 changes: 31 additions & 54 deletions WS-node/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ var WebSocketServer = require('websocket').server;

var http = require('http');
var HttpDispatcher = require('httpdispatcher');
var nexmoApp = require('./nexmo-app')
var dispatcher = new HttpDispatcher();
const fs = require('fs');
const winston = require('winston')
Expand All @@ -16,6 +15,7 @@ var myAsrClient;
var engineStartedMs;
var connections = []


//Create a server
var server = http.createServer(function(req, res) {
handleRequest(req,res);
Expand All @@ -26,7 +26,7 @@ var io = require('socket.io').listen(server);

// When a client connects, we note it in the console
io.sockets.on('connection', function (socket) {
console.log('info','A client is connected!');
winston.log('info','A client is connected!');
});


Expand All @@ -41,7 +41,7 @@ var wsServer = new WebSocketServer({
function handleRequest(request, response){
try {
//log the request on console
console.log('info', 'handleRequest',request.url);
winston.log('info', 'handleRequest',request.url);
//Dispatch
dispatcher.dispatch(request, response);
} catch(err) {
Expand All @@ -51,118 +51,95 @@ function handleRequest(request, response){
dispatcher.setStatic('/public');
dispatcher.setStaticDirname('public');
dispatcher.onGet("/", function(req, res) {
console.log('info', 'loading index');
console.log('info', 'port', process.env.PORT)
winston.log('info', 'loading index');
winston.log('info', 'port', process.env.PORT)
fs.readFile('./public/index.html', 'utf-8', function(error, content) {
console.log('debug', 'loading Index');
winston.log('debug', 'loading Index');
res.writeHead(200, {"Content-Type": "text/html"});
res.end(content);
});
});
// Serve the ncco
dispatcher.onGet("/ncco", function(req, res) {
fs.readFile('./ncco.json', function(error, data) {
console.log('debug', 'loading ncco');
winston.log('debug', 'loading ncco');
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(data, 'utf-8');
});
});

dispatcher.onPost("/terminate", function(req, res) {
console.log('info', 'terminate called');
winston.log('info', 'terminate called');
wsServer.closeAllConnections();

res.writeHead(200, { 'Content-Type': 'application/json' });
res.end();
});


dispatcher.onGet("/answer", function(req, res) {
return nexmoApp.answer(req, res)

});

dispatcher.onGet("/conference", function(req, res) {
return nexmoApp.conference(req, res)
});

dispatcher.onGet("/events_ivr", function(req, res) {
return nexmoApp.events_ivr(req, res)
});

dispatcher.onPost("/events", function(req, res) {
return nexmoApp.events(req, res)
});

dispatcher.onPost("/events-ws", function(req, res) {
return nexmoApp.eventsWS(req, res)
});



wsServer.on('connect', function(connection) {
connections.push(connection);

console.log('info', (new Date()) + ' Connection accepted' + ' - Protocol Version ' + connection.webSocketVersion);
winston.log('info', (new Date()) + ' Connection accepted' + ' - Protocol Version ' + connection.webSocketVersion);
connection.on('message', function(message) {

if (message.type === 'utf8') {
try {
var json = JSON.parse(message.utf8Data);
console.log('info', "json", json['app']);
winston.log('info', "json", json['app']);

if (json['app'] == "audiosocket") {
VBConnect();
console.log('info', 'connecting to VB');
winston.log('info', 'connecting to VB');
}

} catch (e) {
console.log('error', 'message error catch', e)
winston.log('error', 'message error catch', e)
}
console.log('info', "utf ",message.utf8Data);
winston.log('info', "utf ",message.utf8Data);
}
else if (message.type === 'binary') {
// Reflect the message back
// connection.sendBytes(message.binaryData);
if (myAsrClient != null && asrActive) {
myAsrClient.sendData(message.binaryData)
winston.log('debug', "sendingDate ",message.binaryData);
myAsrClient.onAudio(message.binaryData)
}
}
});

connection.on('close', function(reasonCode, description) {
console.log('info', (new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.');
winston.log('info', (new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.');
wsServer.closeAllConnections();

});
});

wsServer.on('close', function(connection) {
console.log('info', 'socket closed');
winston.log('info', 'socket closed');
if (asrActive) {
io.sockets.emit('status', "disconnected");
console.log('info', 'trying to close ASR client');
myAsrClient.close();
winston.log('info', 'trying to close ASR client');
myAsrClient.endOfAudio();
myAsrClient = null;
asrActive = false;
}
else {
console.log('info', 'asr not active, cant close');
winston.log('info', 'asr not active, cant close');
}
})

wsServer.on('error', function(error) {
console.log('error', 'Websocket error', error);
winston.log('error', 'Websocket error', error);
})

var port = process.env.PORT || 8000
server.listen(port, function(){
console.log('info', "Server listening on :%s", port);
winston.log('info', "Server listening on :%s", port);
});

function VBConnect() {

console.log('debug', 'load AsrClient');
winston.log('debug', 'load AsrClient');
myAsrClient = new AsrClient()
var url = process.env.ASR_URL;
client_key = process.env.ASR_CLIENT_KEY;
Expand All @@ -177,42 +154,42 @@ function VBConnect() {
controlMessage.sampleRate = '16000'
controlMessage.windowSize = 10
myAsrClient.reserveAsr(controlMessage)
console.log('debug', "sending control message", controlMessage);
winston.log('debug', "sending control message", controlMessage);

myAsrClient.subscribeEvent('engineState', (msg) => {
console.log('info', 'Engine State Event', msg)
winston.log('info', 'Engine State Event', msg)
if (msg === 'ready') {
asrActive = true
engineStartedMs = Date.now()
console.log('info', 'Setting asrActive to true: ', asrActive, ' this.asrActive ', this.asrActive)
winston.log('info', 'Setting asrActive to true: ', asrActive, ' this.asrActive ', this.asrActive)
io.sockets.emit('status', "connected");
}
})

myAsrClient.subscribeEvent('transcript', (msg) => {
console.log('debug', 'transcript', msg);
winston.log('debug', 'transcript', msg);
io.sockets.emit('transcript', msg);
})

myAsrClient.subscribeEvent('sentiment', (msg) => {
console.log('debug', 'sentiment', msg);
winston.log('debug', 'sentiment', msg);
io.sockets.emit('sentiment', msg);

})

myAsrClient.subscribeEvent('nlp', (msg) => {
console.log('debug', 'nlp', msg);
winston.log('debug', 'nlp', msg);
io.sockets.emit('nlp', msg);
})

myAsrClient.subscribeEvent('keywords', (msg) => {
console.log('info', 'keywords', msg);
winston.log('info', 'keywords', msg);
io.sockets.emit('keywords', msg);
})


myAsrClient.subscribeEvent('latency', (msg) => {
console.log('info', 'latency', msg);
winston.log('info', 'latency', msg);
io.sockets.emit('latency', msg);
})
})
Expand Down
20 changes: 6 additions & 14 deletions WS-node/lib/asrClient.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,15 @@ function AsrClient () {
self.emit('engineState', msg) // ready means start streaming
})

self.socket.on('sentiment estimate', (msg) => {
let x = JSON.parse(msg)
console.log("sentiment", x)
self.socket.on('sentiment event', (msg) => {
let x = JSON.parse(msg)[0]
console.log('sentiment: ', x)
self.emit('sentiment', x)
})

self.socket.on('basic keywords event', (msg) => {
let x = JSON.parse(msg)
console.log('basic keywords event: ', x)
self.emit('keywords', x)
})

Expand Down Expand Up @@ -140,8 +141,7 @@ function AsrClient () {
text = text + ' ' + item.w
})
var re = /<\/s> /gi
// text = text.replace(re, '... ')
text = text.replace(re, '<br /><br />')
text = text.replace(re, '... ')
self.emit('transcript', text)
} else {
console.log('Empty transcript event!')
Expand Down Expand Up @@ -195,14 +195,6 @@ function AsrClient () {
callback(null)
})
}

self.sendData = function(data) {
self.onAudio(data);
}

self.close = function() {
self.endOfAudio()
}
}

AsrClient.prototype.convertFloat32ToInt16 = (buffer) => {
Expand All @@ -214,4 +206,4 @@ AsrClient.prototype.convertFloat32ToInt16 = (buffer) => {
return buf.buffer
}

module.exports = AsrClient
module.exports = AsrClient
Loading

0 comments on commit ba39a63

Please sign in to comment.