Skip to content

Commit

Permalink
Sever data issue push
Browse files Browse the repository at this point in the history
  • Loading branch information
joe-allen-89 committed Jan 31, 2023
1 parent ca6c78b commit a606820
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 65 deletions.
5 changes: 5 additions & 0 deletions conf/config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
56 changes: 50 additions & 6 deletions lib/CollaborationModule.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
});
Expand All @@ -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"));
}
Expand All @@ -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 });
Expand Down
70 changes: 22 additions & 48 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
20 changes: 12 additions & 8 deletions plugins/adaptcollab/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
});
}

Expand All @@ -76,7 +80,7 @@ define(function (require) {
}

function userDataLoaded() {
socket.send(JSON.stringify(users[0]));
socket.send(users[0]);
updateUsers(users[0]);
}

Expand Down
2 changes: 0 additions & 2 deletions plugins/adaptcollab/views/adaptCollabView.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ define(function (require) {
},

initialize(users, currentUser) {
console.log(currentUser);
currentLocation = currentUser.userLocation;
console.log(currentLocation);
this.listenToEvents();
this.render(users);
},
Expand Down

0 comments on commit a606820

Please sign in to comment.