2018-08-30 14:22:57 +00:00
|
|
|
require("dotenv").config();
|
|
|
|
const http = require('http');
|
2018-08-31 13:33:32 +00:00
|
|
|
const Speech = require('./google-cloud/Speech');
|
|
|
|
const Translate = require('./google-cloud/Translate');
|
2018-08-29 17:20:48 +00:00
|
|
|
|
2018-08-30 14:22:57 +00:00
|
|
|
/**
|
|
|
|
* HTTP Server with Websocket which is responsible for:
|
|
|
|
* - Recieving User Audio Media
|
|
|
|
* - Recieving Data relating to which users are in what channel
|
|
|
|
*/
|
2018-08-30 10:49:09 +00:00
|
|
|
class WebSocketServer {
|
|
|
|
constructor() {
|
2018-08-30 14:22:57 +00:00
|
|
|
const server = http.createServer();
|
2018-08-29 17:20:48 +00:00
|
|
|
|
2018-08-31 13:33:32 +00:00
|
|
|
/** @type {Server} */
|
|
|
|
this.io = require('socket.io')(server);
|
|
|
|
|
|
|
|
this.google = {
|
|
|
|
/** @type {Speech} */
|
|
|
|
speech: null,
|
|
|
|
/** @type {Translate} */
|
|
|
|
translate: null
|
|
|
|
}
|
2018-08-29 17:20:48 +00:00
|
|
|
|
2018-08-31 13:33:32 +00:00
|
|
|
server.listen(process.env.PORT || 1337);
|
|
|
|
|
|
|
|
this.listen();
|
2018-08-29 17:20:48 +00:00
|
|
|
}
|
2018-08-30 14:22:57 +00:00
|
|
|
|
2018-08-31 13:33:32 +00:00
|
|
|
listen() {
|
|
|
|
this.io.on('connection', (client) => {
|
|
|
|
console.log("User Connected to Server.");
|
|
|
|
|
|
|
|
client.on('disconnect', () => {
|
|
|
|
console.log("Client Disconnected.");
|
|
|
|
});
|
|
|
|
|
|
|
|
client.on('join', () => {
|
|
|
|
client.emit("welcome", "Welcome to The Polyglot NodeJS Server");
|
|
|
|
});
|
|
|
|
|
|
|
|
client.on('binary', (data) => {
|
|
|
|
if (this.google.speech.enabled) {
|
|
|
|
this.google.speech.getStream().write(data);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
client.on('startRecognition', (lang) => {
|
|
|
|
if (this.google.speech) {
|
|
|
|
this.google.speech.stopRecognition();
|
|
|
|
this.google.speech = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log("Speech Recognition Started");
|
|
|
|
this.google.speech = new Speech()
|
|
|
|
this.google.speech.startRecognition(lang);
|
|
|
|
})
|
2018-08-30 14:22:57 +00:00
|
|
|
|
2018-08-31 13:33:32 +00:00
|
|
|
client.on('stopRecognition', () => {
|
|
|
|
// Close Speech Class
|
|
|
|
console.log("Command given to close Speech Class");
|
|
|
|
})
|
|
|
|
});
|
2018-08-29 17:20:48 +00:00
|
|
|
}
|
2018-08-29 12:24:38 +00:00
|
|
|
}
|
2018-08-29 17:20:48 +00:00
|
|
|
|
2018-08-30 10:49:09 +00:00
|
|
|
const wss = new WebSocketServer();
|