Skip to content

Commit

Permalink
Add cockpit events naming
Browse files Browse the repository at this point in the history
  • Loading branch information
brunnolou committed Dec 27, 2017
1 parent f74ce4a commit be256a1
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 49 deletions.
18 changes: 0 additions & 18 deletions app/cockpit.js

This file was deleted.

20 changes: 11 additions & 9 deletions app/components/Broadcast.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
const store = require('../store');
const events = require('../events');

function Broadcast(action = 'updated', data) {
function Broadcast(event, data) {
const connections = Object.values(store.activeConnections);

connections.forEach(({ connection }) => {
try {
const obj = JSON.stringify({ action, data });
console.log('obj: ', obj);
if (!Object.values(events).includes(event)) return console.log('Invalid event:', event);

connection.sendUTF(obj);
} catch (error) {
console.log(error);
}
connections.forEach(({ connection, id }) => {
if (!connection) return;

connection.send(JSON.stringify({ event, data }));

console.log('Broadcasted event: ', id, event);
});

return Object.keys(store.activeConnections);
}

module.exports = Broadcast;
25 changes: 9 additions & 16 deletions app/components/Connect.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,23 @@
const store = require('../store');
const events = require('../events');
const Broadcast = require('./Broadcast');

function Connect(connection, { resourceURL: { query } }) {
function Connect(connection, { protocol }) {
const id = store.globalCounter;

store.activeConnections[id] = {
collection: query.collection,
connection,
id,
query,
};
store.activeConnections[id] = { connection, id };

// Connected.
connection.sendUTF(JSON.stringify({ action: 'connected' }));
connection.sendUTF(JSON.stringify({ event: events.CONNECT }));
store.globalCounter += 1;

connection.on('message', (message) => {
if (message.type !== 'utf8') return;

// Broadcast.
Object.values(store.activeConnections).forEach(({ connection: conn, ...rest }) => {
conn.sendUTF(JSON.stringify({
action: 'updated',
message: message.utf8Data,
...rest,
}));
});
// Only broadcast preview messages.
if (protocol !== 'preview-protocol') return;

Broadcast(events.COLLECTIONS_PREVIEW);
});

connection.on('close', (reasonCode, description) => {
Expand Down
8 changes: 8 additions & 0 deletions app/events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
CONNECT: 'connect',
REGIONS_SAVE_AFTER: 'regions.save.after',
REGIONS_REMOVE_AFTER: 'regions.remove.after',
COLLECTIONS_SAVE_AFTER: 'collections.save.after',
COLLECTIONS_REMOVE_AFTER: 'collections.remove.after',
COLLECTIONS_PREVIEW: 'cockpit:collections.preview',
};
2 changes: 2 additions & 0 deletions app/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const webSocketServer = require('./webSocketServer');
const webServer = require('./webServer');

// Start web server
const httpServer = webServer();

// Start Sockets server
webSocketServer(httpServer);
11 changes: 7 additions & 4 deletions app/webServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const express = require('express');
const bodyParser = require('body-parser');

const config = require('./config');
const events = require('./events');
const Broadcast = require('./components/Broadcast');

function webServer() {
Expand All @@ -13,15 +14,17 @@ function webServer() {
app.use(bodyParser.json());

app.post('/update', (req, res) => {
Broadcast('update', req.body);
const { body: { event, args } } = req.body;

res.end();
Broadcast(event, args);

res.send('ok');
});

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

res.send('update was broadcasted');
res.send(`update was broadcasted <pre>${JSON.stringify(conns)}`);
});

return httpServer;
Expand Down
6 changes: 4 additions & 2 deletions app/webSocketServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ function webSocketServer(httpServer) {

// Accepts all protocols.
router.mount('*', '*', (request) => {
if (!originIsAllowed(request.origin)) return request.reject();
if (!originIsAllowed(request.origin)) {
request.reject();
return;
}

const connection = request.accept(request.origin);

// @TODO check request.protocol
Connect(connection, request);
});
}
Expand Down
42 changes: 42 additions & 0 deletions payload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

// WebHook
const WebHook = {
event: 'collections.save.after',
hook: 'requestb',
backend: 1,
args: [
'portfolio',
{
title: 'Project Ones',
description: 'clodsmcaoidnc ',
title_slug: 'project-ones',
_mby: '5a3bf33ab5262doc1197003155',
_by: '5a3bf33ab5262doc1197003155',
_modified: 1513948857,
_created: 1513878440,
_id: '5a3bf3a810d59doc1957272294',
published: true,
},
true,
],
};

// WebSocket
const WebSocket = {
event: 'cockpit:collections.preview',
entry: {
title: 'This is supper fast!!',
description: 'Oi',
published: true,
title_slug: 'this-is-supper-fast',
_mby: '5a3bf33ab5262doc1197003155',
_by: '5a3bf33ab5262doc1197003155',
_modified: 1514387407,
_created: 1513881601,
_id: '5a3c00012dad9doc1289733293',
},
lang: 'default',
};

console.log('WebHook: ', WebHook);
console.log('WebSocket: ', WebSocket);

0 comments on commit be256a1

Please sign in to comment.