Commit 0f81be18 authored by Marco Boers's avatar Marco Boers

Websocket and send stringified data

parent 11e38be9
......@@ -7,8 +7,8 @@ const net = require('net');
const dialogflow = require('dialogflow');
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 8081 });
const wss = new WebSocket.Server({port: 8081});
let ws = null;
// You can find your project ID in your Dialogflow agent settings
const projectId = 'notia-667c2'; //https://dialogflow.com/docs/agents#settings
const sessionId = 'quickstart-session-id';
......@@ -17,7 +17,7 @@ const languageCode = 'en-US';
const encoding = 'AUDIO_ENCODING_LINEAR_16';
const sampleRateHertz = 16000;
let shouldStream = true;
let detectStream = null;
let detectStream = null;
let socketData = {};
const sessionClient = new dialogflow.SessionsClient();
const sessionPath = sessionClient.sessionPath(projectId, sessionId);
......@@ -36,6 +36,10 @@ const initialStreamRequest = {
},
};
wss.on('connection', (ws) => {
});
function createDetectStream() {
detectStream = sessionClient
.streamingDetectIntent()
......@@ -55,7 +59,7 @@ function createDetectStream() {
}
}
if(data.queryResult) {
if (data.queryResult) {
shouldStream = true;
console.log(`Detected intent:`);
console.log(data.queryResult);
......@@ -63,7 +67,13 @@ function createDetectStream() {
action: data.queryResult.action,
queryText: data.queryResult.queryText
};
console.log(`SocketData: `+ JSON.stringify(socketData));
const stringifiedData = JSON.stringify(socketData);
if (ws) {
ws.send(stringifiedData);
console.log(`SocketData: ` + stringifiedData);
} else {
console.log(`SocketData: ` + stringifiedData);
}
//console.log(`Parameters:` + data.queryResult.parameters.fields.action);
}
})
......@@ -76,7 +86,6 @@ function createDetectStream() {
}
net.createServer(socket => {
if (socket._readableState) {
delete socket._readableState.decoder;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment