From 684839b5aad532925ff347f45b1886ca1d426856 Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Thu, 27 Sep 2018 11:16:55 +0100 Subject: [PATCH] Use broadcast to avoid useless send --- lib/modules/webserver/server.js | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/lib/modules/webserver/server.js b/lib/modules/webserver/server.js index 239677a44..bea969cb7 100644 --- a/lib/modules/webserver/server.js +++ b/lib/modules/webserver/server.js @@ -6,8 +6,6 @@ const express = require('express'); const fs = require('../../core/fs'); require('http-shutdown').extend(); -const WEB_SOCKET_STATE_OPEN = 1; - class Server { constructor(options) { this.buildDir = options.buildDir; @@ -33,22 +31,18 @@ class Server { const main = serveStatic(this.buildDir, {'index': ['index.html', 'index.htm']}); this.app = express(); - expressWebSocket(this.app); + const expressWs = expressWebSocket(this.app); this.app.use(main); this.app.use('/coverage', coverage); this.app.use(coverageStyle); - 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'); - }); + this.app.ws('/', (_ws, _req) => {}); + const wss = expressWs.getWss('/'); + + self.events.on('outputDone', () => { + wss.clients.forEach(function (client) { + client.send('outputDone'); }); });