From fd357b2ce2571d8595ba96eb64f828385b101f65 Mon Sep 17 00:00:00 2001 From: Bruno Lourenco Date: Mon, 20 Nov 2017 16:43:46 +0000 Subject: [PATCH] Broadcast on messages --- app/index.js | 25 ++++++++++++------------- package.json | 2 +- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/app/index.js b/app/index.js index c053c9a..54ae003 100644 --- a/app/index.js +++ b/app/index.js @@ -47,27 +47,25 @@ wsServer.on("request", function(request) { }) ); - // setInterval(() => { - // connection.sendUTF( - // JSON.stringify({ - // action: "ping" - // }) - // ); - // }, 1500); - - console.log(new Date() + " Connection accepted."); - connection.on("message", function(message) { - if (message.type === "utf8") { - console.log("Received Message: " + message.utf8Data); + if (message.type !== "utf8") return; + + connection.sendUTF( + JSON.stringify({ + action: "connected", + message: message.utf8Data + }) + ); + // Broadcast. + Object.values(allActiveConnections).forEach(connection => { connection.sendUTF( JSON.stringify({ action: "connected", message: message.utf8Data }) ); - } + }); }); connection.on("close", function(reasonCode, description) { @@ -80,6 +78,7 @@ wsServer.on("request", function(request) { }); app.use(express.static("public")); + app.all("/refresh/:collection?/:id?", function(req, res, next) { const { collection, id } = req.params; diff --git a/package.json b/package.json index 7daaf67..3826e0e 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "scripts": { "start": "node index.js", "dev": "nodemon index.js", - "deploy": "now rm ws && now --public", + "deploy": "now rm ws-refresh && now --public", "test": "echo \"Error: no test specified\" && exit 1" }, "repository": {