diff --git a/conf/config.schema.json b/conf/config.schema.json index 11ed4a3..67c2334 100644 --- a/conf/config.schema.json +++ b/conf/config.schema.json @@ -6,6 +6,11 @@ "description": "Whether this module is a template", "type": "string", "default": "ws://localhost:3000" + }, + "idleTime": { + "description": "Time in minutes until a user is classed as inactive", + "type": "number", + "default": 1 } } } diff --git a/lib/CollaborationModule.js b/lib/CollaborationModule.js index 65592cd..4844d42 100644 --- a/lib/CollaborationModule.js +++ b/lib/CollaborationModule.js @@ -1,8 +1,7 @@ import { AbstractModule } from "adapt-authoring-core"; import path from "path"; -import WebSocket from "ws"; -import express from "express"; -import * as http from "http"; +import { WebSocketServer } from "ws"; +let wss; /** * Module to implement Totara connect API @@ -13,10 +12,10 @@ class CollaborationModule extends AbstractModule { async init() { // do custom initialisation here - const server = await this.app.waitForModule("server"); - const PORT = 5000; + // const server = await this.app.waitForModule("server"); + /*const PORT = 5000; const app = express(); - // const server = http.createServer(app); + const server = http.createServer(app); const wsServer = new WebSocket.Server({ server: server }); @@ -36,8 +35,31 @@ class CollaborationModule extends AbstractModule { app.get("/", (req, res) => res.send("Hello World!")); server.listen(3000, () => console.log(`Lisening on port :3000`)); + */ + + this.sockets = []; await this.initRouter(); + + const [auth, server] = await this.app.waitForModule("auth", "server"); + + const router = server.api.createChildRouter("ws"); + router.addRoute({ + route: "/data", + handlers: { get: this.getData.bind(this) } + }); + auth.secureRoute(`${router.path}/data`, "GET", ["read:config"]); + + server.listeningHook.tap(() => { + wss = new WebSocketServer({ + server: server.httpServer, + path: "/socket" + }); + wss.on("connection", this.onConnection.bind(this)); + + // setInterval(() => this.send("just testing " + Date.now()), 3000); + }); + const ui = await this.app.waitForModule("ui"); ui.addUiPlugin(path.resolve(this.rootDir, "plugins")); } @@ -60,6 +82,28 @@ class CollaborationModule extends AbstractModule { }); } + async getData(req, res, next) { + res.json({ testing: true }); + } + + async onConnection(ws) { + console.log("new ws connection"); + ws.on("message", this.onMessage.bind(this)); + // this.sockets.push(ws); + } + + async onMessage(data) { + const dataTest = data.buffer.toString(); + console.log("ws message", dataTest); + wss.clients.forEach(function each(client) { + client.send(dataTest); + }); + } + + async send(data) { + this.sockets.forEach((s) => s.send(data)); + } + async returnUser(req, res, next) { const users = await this.app.waitForModule("users"); const currentUSer = await users.find({ _id: req.auth.user._id }); diff --git a/package-lock.json b/package-lock.json index 9adce78..1b4606a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,7 +13,7 @@ "adapt-authoring-users": "github:adapt-security/adapt-authoring-users", "express": "4.16.3", "handlebars": "^4.7.7", - "ws": "^3.3.3" + "ws": "^8.12.0" } }, "node_modules/@aws-crypto/ie11-detection": { @@ -1296,11 +1296,6 @@ "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", "integrity": "sha512-PCVAQswWemu6UdxsDFFX/+gVeYqKAod3D3UVm91jHwynguOwAvYPhx8nNlM++NqRcK6CxxpUafjmhIdKiHibqg==" }, - "node_modules/async-limiter": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/async-limiter/-/async-limiter-1.0.1.tgz", - "integrity": "sha512-csOlWGAcRFJaI6m+F2WKdnMKr4HhdhFVBk0H/QbJFMCr+uO2kwohwXQPxw/9OCxp05r5ghVBFSyioixx3gfkNQ==" - }, "node_modules/balanced-match": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", @@ -2387,11 +2382,6 @@ "node": ">= 0.8" } }, - "node_modules/ultron": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/ultron/-/ultron-1.1.1.tgz", - "integrity": "sha512-UIEXBNeYmKptWH6z8ZnqTeS8fV74zG0/eRU9VGkpzz+LIJNs8W/zM/L+7ctCkRrgbNnnR0xxw4bKOr0cW0N0Og==" - }, "node_modules/universalify": { "version": "0.1.2", "resolved": "https://registry.npmjs.org/universalify/-/universalify-0.1.2.tgz", @@ -2476,19 +2466,24 @@ "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==" }, "node_modules/ws": { - "version": "3.3.3", - "resolved": "https://registry.npmjs.org/ws/-/ws-3.3.3.tgz", - "integrity": "sha512-nnWLa/NwZSt4KQJu51MYlCcSQ5g7INpOrOMt4XV8j4dqTXdmlUmSHQ8/oLC069ckre0fRsgfvsKwbTdtKLCDkA==", - "dependencies": { - "async-limiter": "~1.0.0", - "safe-buffer": "~5.1.0", - "ultron": "~1.1.0" + "version": "8.12.0", + "resolved": "https://registry.npmjs.org/ws/-/ws-8.12.0.tgz", + "integrity": "sha512-kU62emKIdKVeEIOIKVegvqpXMSTAMLJozpHZaJNDYqBjzlSYXQGviYwN1osDLJ9av68qHd4a2oSjd7yD4pacig==", + "engines": { + "node": ">=10.0.0" + }, + "peerDependencies": { + "bufferutil": "^4.0.1", + "utf-8-validate": ">=5.0.2" + }, + "peerDependenciesMeta": { + "bufferutil": { + "optional": true + }, + "utf-8-validate": { + "optional": true + } } - }, - "node_modules/ws/node_modules/safe-buffer": { - "version": "5.1.2", - "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", - "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" } }, "dependencies": { @@ -3577,11 +3572,6 @@ "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", "integrity": "sha512-PCVAQswWemu6UdxsDFFX/+gVeYqKAod3D3UVm91jHwynguOwAvYPhx8nNlM++NqRcK6CxxpUafjmhIdKiHibqg==" }, - "async-limiter": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/async-limiter/-/async-limiter-1.0.1.tgz", - "integrity": "sha512-csOlWGAcRFJaI6m+F2WKdnMKr4HhdhFVBk0H/QbJFMCr+uO2kwohwXQPxw/9OCxp05r5ghVBFSyioixx3gfkNQ==" - }, "balanced-match": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", @@ -4432,11 +4422,6 @@ "random-bytes": "~1.0.0" } }, - "ultron": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/ultron/-/ultron-1.1.1.tgz", - "integrity": "sha512-UIEXBNeYmKptWH6z8ZnqTeS8fV74zG0/eRU9VGkpzz+LIJNs8W/zM/L+7ctCkRrgbNnnR0xxw4bKOr0cW0N0Og==" - }, "universalify": { "version": "0.1.2", "resolved": "https://registry.npmjs.org/universalify/-/universalify-0.1.2.tgz", @@ -4500,21 +4485,10 @@ "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==" }, "ws": { - "version": "3.3.3", - "resolved": "https://registry.npmjs.org/ws/-/ws-3.3.3.tgz", - "integrity": "sha512-nnWLa/NwZSt4KQJu51MYlCcSQ5g7INpOrOMt4XV8j4dqTXdmlUmSHQ8/oLC069ckre0fRsgfvsKwbTdtKLCDkA==", - "requires": { - "async-limiter": "~1.0.0", - "safe-buffer": "~5.1.0", - "ultron": "~1.1.0" - }, - "dependencies": { - "safe-buffer": { - "version": "5.1.2", - "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", - "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" - } - } + "version": "8.12.0", + "resolved": "https://registry.npmjs.org/ws/-/ws-8.12.0.tgz", + "integrity": "sha512-kU62emKIdKVeEIOIKVegvqpXMSTAMLJozpHZaJNDYqBjzlSYXQGviYwN1osDLJ9av68qHd4a2oSjd7yD4pacig==", + "requires": {} } } } diff --git a/package.json b/package.json index f77eb16..27c6e40 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,6 @@ "adapt-authoring-users": "github:adapt-security/adapt-authoring-users", "handlebars": "^4.7.7", "express": "4.16.3", - "ws": "^3.3.3" + "ws": "^8.12.0" } } diff --git a/plugins/adaptcollab/index.js b/plugins/adaptcollab/index.js index 6dcc8c7..a3ac793 100644 --- a/plugins/adaptcollab/index.js +++ b/plugins/adaptcollab/index.js @@ -5,12 +5,13 @@ define(function (require) { var users = []; let allActiveUsers = []; const idleTime = 1; - const socket = new WebSocket("ws://localhost:3000"); + const [protocol, serverRoot] = window.location.origin.split("//"); + const socket = new WebSocket(`ws://${serverRoot}/socket`); Origin.on("login:changed", loadData); Origin.on("user:logout", function () { - socket.close(); + //socket.close(); }); Origin.on("origin:dataReady", function init() { @@ -43,17 +44,20 @@ define(function (require) { }); socket.addEventListener("open", function (event) { - // console.log("Connected to WS Server"); + console.log("Connected to WS Server"); }); socket.addEventListener("close", function (event) { - const message = JSON.parse(event.data); + // const message = JSON.parse(event.data); }); // Listen for messages - socket.addEventListener("message", function (event, isBinary) { - const message = JSON.parse(event.data); - updateUsers(message); + socket.addEventListener("message", function (event) { + const messageUpdate = new Uint8Array(event.data).buffer; + const message = new DataView(messageUpdate); + // updateUsers(message); + console.log(event.data); + console.log(message); }); } @@ -76,7 +80,7 @@ define(function (require) { } function userDataLoaded() { - socket.send(JSON.stringify(users[0])); + socket.send(users[0]); updateUsers(users[0]); } diff --git a/plugins/adaptcollab/views/adaptCollabView.js b/plugins/adaptcollab/views/adaptCollabView.js index e9c2d4f..8a1c245 100644 --- a/plugins/adaptcollab/views/adaptCollabView.js +++ b/plugins/adaptcollab/views/adaptCollabView.js @@ -16,9 +16,7 @@ define(function (require) { }, initialize(users, currentUser) { - console.log(currentUser); currentLocation = currentUser.userLocation; - console.log(currentLocation); this.listenToEvents(); this.render(users); },