From 1cd1effb98e128fcc4d8dcb0de69cfb48eb39790 Mon Sep 17 00:00:00 2001 From: Paoda Date: Wed, 29 Aug 2018 13:24:38 +0100 Subject: [PATCH] Websocket Connection working. --- .vscode/launch.json | 14 ++++++++++ README.md | 2 +- app.js | 68 +++++++++++++++++++++++++++++++++++++++++++++ package.json | 7 +++-- yarn.lock | 14 ++++++++++ 5 files changed, 102 insertions(+), 3 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 app.js diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..c51bab9 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,14 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "node", + "request": "launch", + "name": "Launch Program", + "program": "${workspaceFolder}\\app.js" + } + ] +} \ No newline at end of file diff --git a/README.md b/README.md index 6e98513..b4f0c89 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ # node-polyglot -Rails server to run alongside betriceo/polyglot +Rails server to run alongside https://github.com/beatriceo/polyglot diff --git a/app.js b/app.js new file mode 100644 index 0000000..8649556 --- /dev/null +++ b/app.js @@ -0,0 +1,68 @@ +require('dotenv').config(); +const WebSocket = require('ws'); + +class ActionCable { + constructor(url, channel) { + this.url = url; + this.channel = channel; + this.connected = false; + + this.io = new WebSocket(url, ['actioncable-v1-json', 'actioncable-unsupported']); + + this.setUpEventListeners(); + + } + + setUpEventListeners() { + this.io.on('open', (msg) => { + console.log('Connected to ' + this.url) + this.connected = true; + this.subscribe(); + }) + + this.io.on('close', (msg) => { + console.log("Disconnected from ActionCable Server"); + this.connected = false; + }) + + this.io.on('message', (msg) => { + console.log(msg); + }) + } + + + send(command, identifier) { + this.io.send(JSON.stringify({command, identifier})); + } + + subscribe() { + this.io.send(JSON.stringify( + { + command: "subscribe", identifier: JSON.stringify({channel: this.channel}) + })); + } +} + +/** + * { + * "identifier": "{\"channel\":\"VideoSessionChannel\"}","message":{"type":"JOIN_ROOM","from":"9921"}} + */ + +class WebRTC { + constructor(username) { + this.username = "NodeServer"; + } + + handleJoin(data) { + data = { + identifier: "{\"channel\":\"VideoSessionChannel\"}", + message: { + type: "JOIN_ROOM", + from: "9921" + } + } + } + + +} +const ac = new ActionCable('ws://0.0.0.0:3000/cable', process.env.DEFAULT_CHANNEL); \ No newline at end of file diff --git a/package.json b/package.json index b4e56f1..40eea1d 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "description": "Rails server to run alongside https://github.com/beatriceo/polyglot", "main": "app.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "echo \"Error: no test specified\" && exit 1", + "start": "node app.js" }, "repository": { "type": "git", @@ -18,6 +19,8 @@ "homepage": "https://github.com/Paoda/node-polyglot#readme", "dependencies": { "@google-cloud/speech": "^2.0.0", - "@google-cloud/translate": "^1.1.0" + "@google-cloud/translate": "^1.1.0", + "dotenv": "^6.0.0", + "ws": "^6.0.0" } } diff --git a/yarn.lock b/yarn.lock index d386647..d090918 100644 --- a/yarn.lock +++ b/yarn.lock @@ -206,6 +206,10 @@ assign-symbols@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/assign-symbols/-/assign-symbols-1.0.0.tgz#59667f41fadd4f20ccbc2bb96b8d4f7f78ec0367" +async-limiter@~1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/async-limiter/-/async-limiter-1.0.0.tgz#78faed8c3d074ab81f22b4e985d79e8738f720f8" + async@^2.3.0, async@^2.4.0: version "2.6.1" resolved "https://registry.yarnpkg.com/async/-/async-2.6.1.tgz#b245a23ca71930044ec53fa46aa00a3e87c6a610" @@ -490,6 +494,10 @@ dir-glob@^2.0.0: arrify "^1.0.1" path-type "^3.0.0" +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: version "3.6.0" resolved "https://registry.yarnpkg.com/duplexify/-/duplexify-3.6.0.tgz#592903f5d80b38d037220541264d69a198fb3410" @@ -2012,6 +2020,12 @@ wrappy@1: version "1.0.2" resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f" +ws@^6.0.0: + version "6.0.0" + resolved "https://registry.yarnpkg.com/ws/-/ws-6.0.0.tgz#eaa494aded00ac4289d455bac8d84c7c651cef35" + dependencies: + async-limiter "~1.0.0" + xtend@^4.0.0, xtend@~4.0.1: version "4.0.1" resolved "https://registry.yarnpkg.com/xtend/-/xtend-4.0.1.tgz#a5c6d532be656e23db820efb943a1f04998d63af"