Able to read and send data to ActionCable
This commit is contained in:
parent
1cd1effb98
commit
3757f8b64f
176
app.js
176
app.js
|
@ -1,68 +1,148 @@
|
||||||
require('dotenv').config();
|
"use strict";
|
||||||
const WebSocket = require('ws');
|
|
||||||
|
|
||||||
|
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 {
|
class ActionCable {
|
||||||
constructor(url, channel) {
|
constructor(url, channel) {
|
||||||
this.url = url;
|
/** @type {string} */
|
||||||
this.channel = channel;
|
this.url = url;
|
||||||
this.connected = false;
|
/** @type {string} */
|
||||||
|
this.channel = channel;
|
||||||
|
/** @type {boolean} */
|
||||||
|
this.connected = false;
|
||||||
|
|
||||||
this.io = new WebSocket(url, ['actioncable-v1-json', 'actioncable-unsupported']);
|
/** @type {WebSocket} */
|
||||||
|
this.io = new WebSocket(url, [
|
||||||
|
"actioncable-v1-json",
|
||||||
|
"actioncable-unsupported"
|
||||||
|
]);
|
||||||
|
|
||||||
this.setUpEventListeners();
|
/** @type {WebRTC} */
|
||||||
|
this.rtc = new WebRTC("NodeServer");
|
||||||
|
|
||||||
}
|
this.setUpEventListeners();
|
||||||
|
}
|
||||||
|
|
||||||
setUpEventListeners() {
|
/**
|
||||||
this.io.on('open', (msg) => {
|
* Sets up the Websocket event listeners.
|
||||||
console.log('Connected to ' + this.url)
|
*/
|
||||||
this.connected = true;
|
setUpEventListeners() {
|
||||||
this.subscribe();
|
this.io.on("open", data => {
|
||||||
})
|
console.log("Connected to " + this.url);
|
||||||
|
this.connected = true;
|
||||||
|
this.subscribe();
|
||||||
|
});
|
||||||
|
|
||||||
this.io.on('close', (msg) => {
|
this.io.on("close", data => {
|
||||||
console.log("Disconnected from ActionCable Server");
|
console.log("Disconnected from ActionCable Server");
|
||||||
this.connected = false;
|
this.connected = false;
|
||||||
})
|
});
|
||||||
|
|
||||||
this.io.on('message', (msg) => {
|
this.io.on("message", data => {
|
||||||
console.log(msg);
|
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(command, identifier) {
|
/**
|
||||||
this.io.send(JSON.stringify({command, identifier}));
|
* 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 }));
|
||||||
|
}
|
||||||
|
|
||||||
subscribe() {
|
/**
|
||||||
this.io.send(JSON.stringify(
|
* Send Stringified Object to Websocket server asking to subscribe to channel
|
||||||
{
|
*/
|
||||||
command: "subscribe", identifier: JSON.stringify({channel: this.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.
|
||||||
* "identifier": "{\"channel\":\"VideoSessionChannel\"}","message":{"type":"JOIN_ROOM","from":"9921"}}
|
* @param {string} username - The name of this server when joining the WebRTC Ecosystem.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class WebRTC {
|
class WebRTC {
|
||||||
constructor(username) {
|
constructor(username) {
|
||||||
this.username = "NodeServer";
|
/** @type {Array<User>} Array of Connected Users */
|
||||||
}
|
this.users = [];
|
||||||
|
/** @type {string} */
|
||||||
|
this.username = username;
|
||||||
|
}
|
||||||
|
|
||||||
handleJoin(data) {
|
/**
|
||||||
data = {
|
* Handles onmessage event when message contains JOIN
|
||||||
identifier: "{\"channel\":\"VideoSessionChannel\"}",
|
* @param {JSON} data - Objectified (???) JSON
|
||||||
message: {
|
*/
|
||||||
type: "JOIN_ROOM",
|
handleJoin(data) {
|
||||||
from: "9921"
|
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);
|
|
||||||
|
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);
|
||||||
|
|
Reference in New Issue