diff --git a/app/cockpit.js b/app/cockpit.js deleted file mode 100644 index 5af0999..0000000 --- a/app/cockpit.js +++ /dev/null @@ -1,18 +0,0 @@ -const fetch = require('node-fetch'); -const { cockpit } = require('./config'); - -const jsonRequest = (url, options = {}) => fetch(url, { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - ...options, -}).then(res => res.json()); - -const fetchCockpit = qs => - jsonRequest([cockpit.base, 'collections', 'get', qs.collection].join('/'), { qs }); - -module.exports = { - fetchCockpit, - jsonRequest, -}; diff --git a/app/components/Broadcast.js b/app/components/Broadcast.js index e63314b..215c478 100644 --- a/app/components/Broadcast.js +++ b/app/components/Broadcast.js @@ -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; diff --git a/app/components/Connect.js b/app/components/Connect.js index 90c1d29..a6a8497 100644 --- a/app/components/Connect.js +++ b/app/components/Connect.js @@ -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) => { diff --git a/app/events.js b/app/events.js new file mode 100644 index 0000000..744919c --- /dev/null +++ b/app/events.js @@ -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', +}; diff --git a/app/index.js b/app/index.js index 4d6bfc3..34eb781 100644 --- a/app/index.js +++ b/app/index.js @@ -1,6 +1,8 @@ const webSocketServer = require('./webSocketServer'); const webServer = require('./webServer'); +// Start web server const httpServer = webServer(); +// Start Sockets server webSocketServer(httpServer); diff --git a/app/webServer.js b/app/webServer.js index c732b3d..9b4452c 100644 --- a/app/webServer.js +++ b/app/webServer.js @@ -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() { @@ -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
${JSON.stringify(conns)}`);
   });
 
   return httpServer;
diff --git a/app/webSocketServer.js b/app/webSocketServer.js
index f87b44c..5a8a7b5 100644
--- a/app/webSocketServer.js
+++ b/app/webSocketServer.js
@@ -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);
   });
 }
diff --git a/payload.js b/payload.js
new file mode 100644
index 0000000..5a3ca44
--- /dev/null
+++ b/payload.js
@@ -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);