From 62d6b2362dbe2d9e538bf508efbb2f6cdae86dd8 Mon Sep 17 00:00:00 2001 From: Josh Thomas Date: Tue, 6 Dec 2016 09:35:23 -0600 Subject: [PATCH] fix(livereload): on project build all pages connected should reload. (#513) --- src/dev-server/notification-server.ts | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/dev-server/notification-server.ts b/src/dev-server/notification-server.ts index 7071b88e..8395840d 100644 --- a/src/dev-server/notification-server.ts +++ b/src/dev-server/notification-server.ts @@ -15,16 +15,22 @@ export function createNotificationServer(config: ServeConfig) { // queue up all messages to the client function queueMessageSend(msg: WsMessage) { msgToClient.push(msg); - drainMessageQueue(); + drainMessageQueue({ + broadcast: true + }); } // drain the queue messages when the server is ready - function drainMessageQueue() { - if (wsServer) { + function drainMessageQueue(options = { broadcast: false }) { + let sendMethod = wsServer.send; + if (options.hasOwnProperty('broadcast') && options.broadcast) { + sendMethod = wss.broadcast; + } + if (wss.clients.length > 0) { let msg: any; while (msg = msgToClient.shift()) { try { - wsServer.send(JSON.stringify(msg)); + sendMethod(JSON.stringify(msg)); } catch (e) { if (e.message !== 'not opened') { Logger.error(`error sending client ws - ${e.message}`); @@ -64,7 +70,11 @@ export function createNotificationServer(config: ServeConfig) { // create web socket server const wss = new WebSocketServer({ port: config.notificationPort }); - + wss.broadcast = function broadcast(data: any) { + wss.clients.forEach(function each(client: any) { + client.send(data); + }); + }; wss.on('connection', (ws: any) => { // we've successfully connected wsServer = ws;