Changed the point of the Node Server. Now a WebSocket Server.

This commit is contained in:
Paoda 2018-08-30 11:49:09 +01:00
parent 3757f8b64f
commit 70c0d07cbf
5 changed files with 219 additions and 139 deletions

View File

@ -1,2 +1,2 @@
# node-polyglot # node-polyglot
Rails server to run alongside https://github.com/beatriceo/polyglot Node server to run alongside https://github.com/beatriceo/polyglot

154
app.js
View File

@ -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} */ class WebSocketServer {
this.io = new WebSocket(url, [ constructor() {
"actioncable-v1-json", this.io = new WebSocket.Server({
"actioncable-unsupported" port: process.env.PORT || 1337,
]);
/** @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();
}); });
this.io.on("close", data => { this.stream = WebSocketStream.createServer({
console.log("Disconnected from ActionCable Server"); server: this.io,
this.connected = false; }, this.handleStream)
});
this.io.on("message", data => { this.createEventListeners();
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 }));
}
/** handleStream(stream, req) {
* Send Stringified Object to Websocket server asking to subscribe to channel console.log(stream);
*/ }
subscribe() { createEventListeners() {
this.io.send( this.io.on('connection', (io) => {
JSON.stringify({ io.on('message', (msg) => {
command: "subscribe", console.log('Recieved: %s', msg);
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.
*/
class WebRTC {
constructor(username) {
/** @type {Array<User>} 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() { const wss = new WebSocketServer();
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);

170
app.old.js Normal file
View File

@ -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<User>} 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);

View File

@ -1,7 +1,7 @@
{ {
"name": "polyglot", "name": "polyglot",
"version": "1.0.0", "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", "main": "app.js",
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1", "test": "echo \"Error: no test specified\" && exit 1",
@ -21,6 +21,7 @@
"@google-cloud/speech": "^2.0.0", "@google-cloud/speech": "^2.0.0",
"@google-cloud/translate": "^1.1.0", "@google-cloud/translate": "^1.1.0",
"dotenv": "^6.0.0", "dotenv": "^6.0.0",
"websocket-stream": "^5.1.2",
"ws": "^6.0.0" "ws": "^6.0.0"
} }
} }

View File

@ -498,7 +498,7 @@ dotenv@^6.0.0:
version "6.0.0" version "6.0.0"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-6.0.0.tgz#24e37c041741c5f4b25324958ebbc34bca965935" 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" version "3.6.0"
resolved "https://registry.yarnpkg.com/duplexify/-/duplexify-3.6.0.tgz#592903f5d80b38d037220541264d69a198fb3410" resolved "https://registry.yarnpkg.com/duplexify/-/duplexify-3.6.0.tgz#592903f5d80b38d037220541264d69a198fb3410"
dependencies: dependencies:
@ -1603,7 +1603,7 @@ rc@^1.2.7:
minimist "^1.2.0" minimist "^1.2.0"
strip-json-comments "~2.0.1" 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" version "2.3.6"
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.3.6.tgz#b11c27d88b8ff1fbe070643cf94b0c79ae1b0aaf" resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.3.6.tgz#b11c27d88b8ff1fbe070643cf94b0c79ae1b0aaf"
dependencies: dependencies:
@ -1686,7 +1686,7 @@ rimraf@^2.6.1:
dependencies: dependencies:
glob "^7.0.5" 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" version "5.1.2"
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.2.tgz#991ec69d296e0313747d59bdfd2b745c35f8828d" 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" version "0.0.6"
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777" 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: union-value@^1.0.0:
version "1.0.0" version "1.0.0"
resolved "https://registry.yarnpkg.com/union-value/-/union-value-1.0.0.tgz#5c71c34cb5bad5dcebe3ea0cd08207ba5aa1aea4" 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" core-util-is "1.0.2"
extsprintf "^1.2.0" 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: wide-align@^1.1.0:
version "1.1.3" version "1.1.3"
resolved "https://registry.yarnpkg.com/wide-align/-/wide-align-1.1.3.tgz#ae074e6bdc0c14a431e804e624549c633b000457" resolved "https://registry.yarnpkg.com/wide-align/-/wide-align-1.1.3.tgz#ae074e6bdc0c14a431e804e624549c633b000457"
@ -2020,6 +2035,14 @@ wrappy@1:
version "1.0.2" version "1.0.2"
resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f" 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: ws@^6.0.0:
version "6.0.0" version "6.0.0"
resolved "https://registry.yarnpkg.com/ws/-/ws-6.0.0.tgz#eaa494aded00ac4289d455bac8d84c7c651cef35" resolved "https://registry.yarnpkg.com/ws/-/ws-6.0.0.tgz#eaa494aded00ac4289d455bac8d84c7c651cef35"