From 7bd6b9da8f0034e48892f81ea18d0eb97cee6cf8 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Mon, 26 Feb 2018 09:45:46 -0500 Subject: [PATCH] add basic ws functionality --- lib/modules/webserver/backend/index.html | 28 +++++++++++++++++++++++ lib/modules/webserver/server.js | 29 +++++++++++++++++------- package.json | 1 + 3 files changed, 50 insertions(+), 8 deletions(-) diff --git a/lib/modules/webserver/backend/index.html b/lib/modules/webserver/backend/index.html index 01d3caf4f..43288c269 100644 --- a/lib/modules/webserver/backend/index.html +++ b/lib/modules/webserver/backend/index.html @@ -1 +1,29 @@ Welcome to Embark! + + + diff --git a/lib/modules/webserver/server.js b/lib/modules/webserver/server.js index fb9c0089f..6cf7775a8 100644 --- a/lib/modules/webserver/server.js +++ b/lib/modules/webserver/server.js @@ -7,6 +7,7 @@ const fs = require('../../core/fs'); require('http-shutdown').extend(); var express = require('express'); let path = require('path'); +var expressWebSocket = require('express-ws'); class Server { constructor(options) { @@ -64,18 +65,30 @@ class Server { this.app.use('/coverage', coverage); this.app.use(coverageStyle); - this.app.ws('/', (_ws, _req) => {}); - const wss = expressWs.getWss('/'); + expressWebSocket(this.app); - self.events.on('outputDone', () => { - wss.clients.forEach(function (client) { - client.send('outputDone'); + this.app.ws('/logs', function(ws, req) { + self.events.on("log", function(logLevel, logMsg) { + console.dir("got message " + logMsg); + ws.send(logMsg); + }); + ws.on('message', function(msg) { + console.dir("got message"); + console.dir(msg); + ws.send(msg); }); }); - self.events.on('outputError', () => { - wss.clients.forEach(function (client) { - client.send('outputError'); + this.app.ws('/', function(ws, _req) { + self.events.on('outputDone', () => { + if (ws.readyState === WEB_SOCKET_STATE_OPEN) { + return ws.send('outputDone'); + } + // if the socket wasn't yet opened, listen for the 'open' event, + // then send the 'outputDone' data + ws.addEventListener('open', _event => { + ws.send('outputDone'); + }); }); }); diff --git a/package.json b/package.json index eea4be4e1..d26292491 100644 --- a/package.json +++ b/package.json @@ -74,6 +74,7 @@ "i18n": "0.8.3", ======= "express": "^4.16.2", + "express-ws": "^3.0.0", "file-loader": "^1.1.5", "finalhandler": "^1.1.1", "follow-redirects": "^1.2.4",