Skip to content

Commit

Permalink
Add collection name events
Browse files Browse the repository at this point in the history
  • Loading branch information
brunnolou committed Dec 28, 2017
1 parent 720a170 commit 20c6d3a
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 18 deletions.
9 changes: 5 additions & 4 deletions app/components/Broadcast.js
Original file line number Diff line number Diff line change
@@ -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 }) => {
Expand All @@ -23,7 +24,7 @@ function Broadcast(data) {
console.log('Broadcasted event: ', [id], response.event);
});

return Object.keys(store.activeConnections);
return true;
}

module.exports = Broadcast;
4 changes: 2 additions & 2 deletions app/components/Connect.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
22 changes: 21 additions & 1 deletion app/events.js
Original file line number Diff line number Diff line change
@@ -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,
};
15 changes: 10 additions & 5 deletions app/webServer.js
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -22,9 +22,14 @@ function webServer() {
});

app.get('/update', (req, res) => {
const conns = Broadcast(events.COLLECTIONS_SAVE_AFTER);

res.send(`update was broadcasted <pre>${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;
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

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

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
8 changes: 6 additions & 2 deletions public/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('<br />---------------------------<br />');
messages.push(`<b>${new Date()}:</b><br />${JSON.stringify(obj, null, ' ')}`);

Messages.innerHTML = messages
.slice(0)
.reverse()
.join('<br /><hr /><br />');
};

0 comments on commit 20c6d3a

Please sign in to comment.