diff --git a/app/components/Broadcast.js b/app/components/Broadcast.js index f90053b..a1f4d54 100644 --- a/app/components/Broadcast.js +++ b/app/components/Broadcast.js @@ -1,11 +1,12 @@ const store = require('../store'); -const events = require('../events'); +const { isValid } = require('../events'); function Broadcast(data) { const connections = Object.values(store.activeConnections); - if (!Object.values(events).includes(data.event)) { - return console.log('Invalid event:', data.event); + if (!isValid(data.event)) { + console.log('Invalid event:', data.event); + return false; } connections.forEach(({ connection, id }) => { @@ -23,7 +24,7 @@ function Broadcast(data) { console.log('Broadcasted event: ', [id], response.event); }); - return Object.keys(store.activeConnections); + return true; } module.exports = Broadcast; diff --git a/app/components/Connect.js b/app/components/Connect.js index c95641c..83521d0 100644 --- a/app/components/Connect.js +++ b/app/components/Connect.js @@ -1,6 +1,6 @@ -const store = require('../store'); -const events = require('../events'); const Broadcast = require('./Broadcast'); +const store = require('../store'); +const { events } = require('../events'); function Connect(connection, req) { const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress; diff --git a/app/events.js b/app/events.js index 744919c..b6960a2 100644 --- a/app/events.js +++ b/app/events.js @@ -1,8 +1,28 @@ -module.exports = { +const events = { CONNECT: 'connect', REGIONS_SAVE_AFTER: 'regions.save.after', REGIONS_REMOVE_AFTER: 'regions.remove.after', COLLECTIONS_SAVE_AFTER: 'collections.save.after', + COLLECTIONS_SAVE_AFTER_NAME: 'collections.save.after.%s', COLLECTIONS_REMOVE_AFTER: 'collections.remove.after', COLLECTIONS_PREVIEW: 'cockpit:collections.preview', }; + +const isValid = (eventName) => { + if (!eventName) return false; + if (Object.values(events).includes(eventName)) return true; + + const eventsWithName = Object.values(events) + .filter(x => x.includes('.%s')) + .map(x => x.replace('.%s', '')); + + return eventsWithName.includes(eventName + .split('.') + .slice(0, -1) + .join('.')); +}; + +module.exports = { + events, + isValid, +}; diff --git a/app/webServer.js b/app/webServer.js index 0159b5a..bd045d8 100644 --- a/app/webServer.js +++ b/app/webServer.js @@ -1,9 +1,9 @@ const express = require('express'); const bodyParser = require('body-parser'); -const config = require('./config'); -const events = require('./events'); const Broadcast = require('./components/Broadcast'); +const config = require('./config'); +const { events } = require('./events'); function webServer() { const app = express(); @@ -22,9 +22,14 @@ function webServer() { }); app.get('/update', (req, res) => { - const conns = Broadcast(events.COLLECTIONS_SAVE_AFTER); - - res.send(`update was broadcasted
${JSON.stringify(conns)}`); + const result = Broadcast({ event: events.COLLECTIONS_SAVE_AFTER, data: {} }); + + if (result === true) { + res.send('update was broadcasted!'); + } else { + console.log('result: ', result); + res.send('Error! not broadcasted'); + } }); return httpServer; diff --git a/package-lock.json b/package-lock.json index b5dd7ba..aea3fde 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,5 +1,5 @@ { - "name": "ws-refresh", + "name": "cockpit-real-time", "version": "1.0.0", "lockfileVersion": 1, "requires": true, diff --git a/package.json b/package.json index c5b42a0..ec81402 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,12 @@ { - "name": "ws-refresh", - "version": "1.0.0", + "name": "cockpit-real-time-server", + "version": "0.0.1", "description": "", "main": "index.js", "scripts": { "start": "node index.js", "dev": "nodemon index.js", - "deploy": "now rm ws-refresh && now --public", + "deploy": "now rm cockpit-real-time && now --public", "test": "echo \"Error: no test specified\" && exit 1" }, "repository": { diff --git a/public/main.js b/public/main.js index 3eea4ea..a8b63cf 100644 --- a/public/main.js +++ b/public/main.js @@ -21,6 +21,10 @@ Button.onclick = () => { ws.onmessage = function onmessage(evt) { const obj = JSON.parse(evt.data); - messages.push(JSON.stringify(obj, null, ' ')); - Messages.innerHTML = messages.join('
---------------------------
'); + messages.push(`${new Date()}:
${JSON.stringify(obj, null, ' ')}`); + + Messages.innerHTML = messages + .slice(0) + .reverse() + .join('
'); };