Skip to content

Commit

Permalink
initial work on v2
Browse files Browse the repository at this point in the history
  • Loading branch information
tbass134 committed May 30, 2018
1 parent 638b8a9 commit 9257f8d
Show file tree
Hide file tree
Showing 7 changed files with 1,803 additions and 109 deletions.
84 changes: 53 additions & 31 deletions WS-node/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@ 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')
winston.level = process.env.LOG_LEVEL || 'info'

winston.level = 'silly'
var AsrClient = require('./lib/asrClient')
var asrActive = false
var myAsrClient;
var engineStartedMs;
var connections = []


//Create a server
var server = http.createServer(function(req, res) {
handleRequest(req,res);
Expand All @@ -27,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) {
winston.log('info','A client is connected!');
console.log('info','A client is connected!');
});


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

dispatcher.onPost("/terminate", function(req, res) {
winston.log('info', 'terminate called');
console.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);

winston.log('info', (new Date()) + ' Connection accepted' + ' - Protocol Version ' + connection.webSocketVersion);
console.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);
winston.log('info', "json", json['app']);
console.log('info', "json", json['app']);

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

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

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

});
});

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

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

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

function VBConnect() {

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

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

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

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

})

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

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


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

0 comments on commit 9257f8d

Please sign in to comment.