diff --git a/README.md b/README.md index b4f0c89..749e2b1 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ # node-polyglot -Rails server to run alongside https://github.com/beatriceo/polyglot +Node server to run alongside https://github.com/beatriceo/polyglot diff --git a/app.js b/app.js index b0d5dce..f60e85b 100644 --- a/app.js +++ b/app.js @@ -1,148 +1,34 @@ -"use strict"; +require('dotenv').config(); +const WebSocket = require('ws'); +const WebSocketStream = require('websocket-stream'); -require("dotenv").config(); -const WebSocket = require("ws"); -/** - * Client responsible for communicating with ActionCable Websocket. - * @param {string} url - URL of Websocket (starts with ws:// or wss://) - * @param {string} channel - Name of ActionCable Channel. - */ -class ActionCable { - constructor(url, channel) { - /** @type {string} */ - this.url = url; - /** @type {string} */ - this.channel = channel; - /** @type {boolean} */ - this.connected = false; - /** @type {WebSocket} */ - this.io = new WebSocket(url, [ - "actioncable-v1-json", - "actioncable-unsupported" - ]); - - /** @type {WebRTC} */ - this.rtc = new WebRTC("NodeServer"); - - this.setUpEventListeners(); - } - - /** - * Sets up the Websocket event listeners. - */ - setUpEventListeners() { - this.io.on("open", data => { - console.log("Connected to " + this.url); - this.connected = true; - this.subscribe(); +class WebSocketServer { + constructor() { + this.io = new WebSocket.Server({ + port: process.env.PORT || 1337, }); - this.io.on("close", data => { - console.log("Disconnected from ActionCable Server"); - this.connected = false; - }); + this.stream = WebSocketStream.createServer({ + server: this.io, + }, this.handleStream) - this.io.on("message", data => { - data = JSON.parse(data); - console.log(data); - - if (data.message && data.message.type) { - switch (data.message.type) { - case "JOIN_ROOM": - this.rtc.handleJoin(data.message); - break; - case "REMOVE_USER": - console.log("User has Left Room"); - this.rtc.handleLeave(data.message); - break; - default: - // console.log("Pinged"); - } - } - }); + this.createEventListeners(); } - /** - * Send Stringified Object to Websocket Server to be processed - * @param {string} command - * @param {Object} identifier - */ - send(command, identifier) { - this.io.send(JSON.stringify({ command, identifier })); - } - /** - * Send Stringified Object to Websocket server asking to subscribe to channel - */ - subscribe() { - this.io.send( - JSON.stringify({ - command: "subscribe", - identifier: JSON.stringify({ channel: this.channel }) + handleStream(stream, req) { + console.log(stream); + } + createEventListeners() { + this.io.on('connection', (io) => { + io.on('message', (msg) => { + console.log('Recieved: %s', msg); }) - ); - } -} - -/** - * The Class Responsible for Handling the data from the WebSocket and Getting the MediaStream. - * @param {string} username - The name of this server when joining the WebRTC Ecosystem. - */ -class WebRTC { - constructor(username) { - /** @type {Array} Array of Connected Users */ - this.users = []; - /** @type {string} */ - this.username = username; - } - - /** - * Handles onmessage event when message contains JOIN - * @param {JSON} data - Objectified (???) JSON - */ - handleJoin(data) { - console.log("User " + data.from + " has joined the room"); - this.users.push(new User(data.from)); - - //Try and Exchange Data with the person - } - - /** - * Handles onmessage event when message contains REMOVE - * @param {JSON} data - Stringified JSON - */ - handleLeave(data) { - console.log("User " + data.from + " has left the room."); - this.users - } -} - -const ac = new ActionCable( - "ws://0.0.0.0:3000/cable", - process.env.DEFAULT_CHANNEL -); - - -class User { - constructor(id) { - this.id = id; + }) } } -(function addDeleteMethodToArrayPrototype() { - Array.prototype.delete = function(toRemove) { - let i = this.indexOf(toRemove); - - if (i !== -1) this.splice(i, 1); - - //return this; - } -})(); - -const arr = ["Brave", "New", "World"]; - -console.log(arr.delete("New")); -console.log("Length: " + arr.length); +const wss = new WebSocketServer(); diff --git a/app.old.js b/app.old.js new file mode 100644 index 0000000..3770cbb --- /dev/null +++ b/app.old.js @@ -0,0 +1,170 @@ +"use strict"; + +require("dotenv").config(); +const WebSocket = require("ws"); + +/** + * Client responsible for communicating with ActionCable Websocket. + * @param {string} url - URL of Websocket (starts with ws:// or wss://) + * @param {string} channel - Name of ActionCable Channel. + */ +class ActionCable { + constructor(url, channel) { + /** @type {string} */ + this.url = url; + /** @type {string} */ + this.channel = channel; + /** @type {boolean} */ + this.connected = false; + + /** @type {WebSocket} */ + this.io = new WebSocket(url, [ + "actioncable-v1-json", + "actioncable-unsupported" + ]); + + /** @type {WebRTC} */ + // this.rtc = new WebRTC(~~(Math.random() * 1000), this.channel, this.io); + this.rtc = new WebRTC("3929", this.channel, this.io); + + this.setUpEventListeners(); + } + + /** + * Sets up the Websocket event listeners. + */ + setUpEventListeners() { + this.io.on("open", data => { + console.log("Connected to " + this.url); + this.connected = true; + this.subscribe(); + + setTimeout(() => { + console.log("Joining Room"); + this.rtc.joinRoom() + }, 5000); + }); + + this.io.on("close", data => { + console.log("Disconnected from ActionCable Server"); + this.connected = false; + }); + + this.io.on("message", data => { + data = JSON.parse(data); + console.log(data); + + if (data.message && data.message.type) { + switch (data.message.type) { + case "JOIN_ROOM": + this.rtc.handleJoin(data.message); + break; + case "REMOVE_USER": + console.log("User has Left Room"); + this.rtc.handleLeave(data.message); + break; + default: + // console.log("Pinged"); + } + } + }); + } + + /** + * Send Stringified Object to Websocket Server to be processed + * @param {string} command + * @param {Object} identifier + */ + send(command, identifier) { + this.io.send(JSON.stringify({ command, identifier })); + } + + /** + * Send Stringified Object to Websocket server asking to subscribe to channel + */ + subscribe() { + this.io.send( + JSON.stringify({ + command: "subscribe", + identifier: JSON.stringify({ channel: this.channel }) + }) + ); + } +} + +/** + * The Class Responsible for Handling the data from the WebSocket and Getting the MediaStream. + * @param {string} username - The name of this server when joining the WebRTC Ecosystem. + * @param {string} channel + */ +class WebRTC { + constructor(username, channel, webSocket) { + /** @type {Array} Array of Connected Users */ + this.users = []; + /** @type {string} */ + this.username = username; + /** @type {string} */ + this.channel = channel + /** @type {WebSocket} */ + this.io = webSocket; + } + + /** + * Handles onmessage event when message contains JOIN + * @param {JSON} data - Objectified (???) JSON + */ + handleJoin(data) { + console.log("User " + data.from + " has joined the room"); + this.users.push(new User(data.from)); + + //Try and Exchange Data with the person + } + + joinRoom() { + this.io.send(JSON.stringify({ + command: "message", + identifier: JSON.stringify({ + channel: this.channel + }), + message: { + type: "JOIN_ROOM", from: `${this.username}` + } + })); + } + + /** + * Handles onmessage event when message contains REMOVE + * @param {JSON} data - Stringified JSON + */ + handleLeave(data) { + console.log("User " + data.from + " has left the room."); + this.users + } +} + +const ac = new ActionCable( + "ws://0.0.0.0:3000/cable", + process.env.DEFAULT_CHANNEL +); + +class User { + constructor(id) { + this.id = id; + } +} + + +(function addDeleteMethodToArrayPrototype() { + Array.prototype.delete = function(toRemove) { + let i = this.indexOf(toRemove); + + if (i !== -1) this.splice(i, 1); + + //return this; + } +})(); + +const arr = ["Brave", "New", "World"]; + +console.log(arr.delete("New")); +console.log("Length: " + arr.length); diff --git a/package.json b/package.json index 40eea1d..2749054 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "polyglot", "version": "1.0.0", - "description": "Rails server to run alongside https://github.com/beatriceo/polyglot", + "description": "Node server to run alongside https://github.com/beatriceo/polyglot", "main": "app.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", @@ -21,6 +21,7 @@ "@google-cloud/speech": "^2.0.0", "@google-cloud/translate": "^1.1.0", "dotenv": "^6.0.0", + "websocket-stream": "^5.1.2", "ws": "^6.0.0" } } diff --git a/yarn.lock b/yarn.lock index d090918..aae11bf 100644 --- a/yarn.lock +++ b/yarn.lock @@ -498,7 +498,7 @@ dotenv@^6.0.0: version "6.0.0" resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-6.0.0.tgz#24e37c041741c5f4b25324958ebbc34bca965935" -duplexify@^3.5.0, duplexify@^3.6.0: +duplexify@^3.5.0, duplexify@^3.5.1, duplexify@^3.6.0: version "3.6.0" resolved "https://registry.yarnpkg.com/duplexify/-/duplexify-3.6.0.tgz#592903f5d80b38d037220541264d69a198fb3410" dependencies: @@ -1603,7 +1603,7 @@ rc@^1.2.7: minimist "^1.2.0" strip-json-comments "~2.0.1" -readable-stream@^2.0.0, readable-stream@^2.0.6, readable-stream@^2.1.5, readable-stream@^2.2.2: +readable-stream@^2.0.0, readable-stream@^2.0.6, readable-stream@^2.1.5, readable-stream@^2.2.2, readable-stream@^2.3.3: version "2.3.6" resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.3.6.tgz#b11c27d88b8ff1fbe070643cf94b0c79ae1b0aaf" dependencies: @@ -1686,7 +1686,7 @@ rimraf@^2.6.1: dependencies: glob "^7.0.5" -safe-buffer@^5.0.1, safe-buffer@^5.1.2, safe-buffer@~5.1.0, safe-buffer@~5.1.1: +safe-buffer@^5.0.1, safe-buffer@^5.1.1, safe-buffer@^5.1.2, safe-buffer@~5.1.0, safe-buffer@~5.1.1: version "5.1.2" resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.2.tgz#991ec69d296e0313747d59bdfd2b745c35f8828d" @@ -1951,6 +1951,10 @@ typedarray@^0.0.6: version "0.0.6" resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777" +ultron@~1.1.0: + version "1.1.1" + resolved "https://registry.yarnpkg.com/ultron/-/ultron-1.1.1.tgz#9fe1536a10a664a65266a1e3ccf85fd36302bc9c" + union-value@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/union-value/-/union-value-1.0.0.tgz#5c71c34cb5bad5dcebe3ea0cd08207ba5aa1aea4" @@ -1999,6 +2003,17 @@ verror@1.10.0: core-util-is "1.0.2" extsprintf "^1.2.0" +websocket-stream@^5.1.2: + version "5.1.2" + resolved "https://registry.yarnpkg.com/websocket-stream/-/websocket-stream-5.1.2.tgz#1c31c627bcdf34f1a9bdacc9daa15bfa4816d9ad" + dependencies: + duplexify "^3.5.1" + inherits "^2.0.1" + readable-stream "^2.3.3" + safe-buffer "^5.1.1" + ws "^3.2.0" + xtend "^4.0.0" + wide-align@^1.1.0: version "1.1.3" resolved "https://registry.yarnpkg.com/wide-align/-/wide-align-1.1.3.tgz#ae074e6bdc0c14a431e804e624549c633b000457" @@ -2020,6 +2035,14 @@ wrappy@1: version "1.0.2" resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f" +ws@^3.2.0: + version "3.3.3" + resolved "https://registry.yarnpkg.com/ws/-/ws-3.3.3.tgz#f1cf84fe2d5e901ebce94efaece785f187a228f2" + dependencies: + async-limiter "~1.0.0" + safe-buffer "~5.1.0" + ultron "~1.1.0" + ws@^6.0.0: version "6.0.0" resolved "https://registry.yarnpkg.com/ws/-/ws-6.0.0.tgz#eaa494aded00ac4289d455bac8d84c7c651cef35"