This repository has been archived on 2021-06-30. You can view files and clone it, but cannot push or open issues or pull requests.
node-polyglot/app.js

68 lines
1.6 KiB
JavaScript
Raw Normal View History

require("dotenv").config();
const http = require('http');
const Speech = require('./google-cloud/Speech');
const Translate = require('./google-cloud/Translate');
/**
* HTTP Server with Websocket which is responsible for:
* - Recieving User Audio Media
* - Recieving Data relating to which users are in what channel
*/
class WebSocketServer {
constructor() {
const server = http.createServer();
/** @type {Server} */
this.io = require('socket.io')(server);
this.google = {
/** @type {Speech} */
speech: null,
/** @type {Translate} */
translate: null
}
server.listen(process.env.PORT || 1337);
this.listen();
}
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);
}
});
2018-09-04 08:41:41 +00:00
client.on('startRecognition', (lang, target) => {
if (this.google.speech) {
this.google.speech.stopRecognition();
this.google.speech = null;
}
console.log("Speech Recognition Started");
this.google.speech = new Speech()
2018-09-04 08:41:41 +00:00
this.google.speech.startRecognition(lang, target);
})
client.on('stopRecognition', () => {
// Close Speech Class
console.log("Command given to close Speech Class");
})
});
}
2018-08-29 12:24:38 +00:00
}
const wss = new WebSocketServer();