diff --git a/lib/bsb b/lib/bsb index ac7c278f10..87f29e042f 100755 --- a/lib/bsb +++ b/lib/bsb @@ -32,18 +32,16 @@ function notifyClients() { wsClients = wsClients.filter(x => !x.closed && !x.socket.destroyed ) var wsClientsLen = wsClients.length dlog(`Alive sockets number: ${wsClientsLen}`) - for (var i = 0; i < wsClientsLen; ++ i ) { - // in reverse order, the last pushed get notified earlier - var client = wsClients[wsClientsLen - i - 1] - if (!client.closed) { - client.sendText( - JSON.stringify( + var data = JSON.stringify( { LAST_SUCCESS_BUILD_STAMP: LAST_SUCCESS_BUILD_STAMP } ) - - ) + for (var i = 0; i < wsClientsLen; ++ i ) { + // in reverse order, the last pushed get notified earlier + var client = wsClients[wsClientsLen - i - 1] + if (!client.closed) { + client.sendText(data) } } } @@ -55,6 +53,9 @@ function setUpWebSocket() { .on('upgrade', function (req, socket, upgradeHead) { dlog("connection opened"); var ws = new WebSocket(req, socket, upgradeHead); + socket.on("error",function (err){ + dlog(`Socket Error ${err}`) + }) wsClients.push(ws) }) .on('error', function (err) { @@ -147,7 +148,10 @@ if (watch_mode) { */ var watchers = []; - + function onUncaughtException (err){ + console.error("Uncaught Exception", err) + process.exit(1) + } function onExit() { try { fs.unlinkSync(lockFileName) @@ -180,7 +184,7 @@ if (watch_mode) { process.on('SIGUSR2', onExit) process.on('SIGTERM', onExit) process.on('SIGHUP', onExit) - process.on('uncaughtException', onExit) + process.on('uncaughtException', onUncaughtException) process.stdin.on('close', onExit) // close when stdin stops if (os.platform() !== "win32") { diff --git a/lib/minisocket.js b/lib/minisocket.js index 5855e6dbfb..780a53e0a0 100644 --- a/lib/minisocket.js +++ b/lib/minisocket.js @@ -39,14 +39,14 @@ function encodeMessage(opcode, payload) { // server does not mask frames var length = payload.length; if (length < 126) { - buf = new Buffer(payload.length + 2 + 0); + buf = Buffer.allocUnsafe(payload.length + 2 + 0); // zero extra bytes b2 |= length; buf.writeUInt8(b1, 0); buf.writeUInt8(b2, 1); payload.copy(buf, 2); } else if (length < (1 << 16)) { - buf = new Buffer(payload.length + 2 + 2); + buf = Buffer.allocUnsafe(payload.length + 2 + 2); // two bytes extra b2 |= 126; buf.writeUInt8(b1, 0); @@ -55,7 +55,7 @@ function encodeMessage(opcode, payload) { buf.writeUInt16BE(length, 2); payload.copy(buf, 4); } else { - buf = new Buffer(payload.length + 2 + 8); + buf = Buffer.allocUnsafe(payload.length + 2 + 8); // eight bytes extra b2 |= 127; buf.writeUInt8(b1, 0); @@ -87,7 +87,7 @@ class MiniWebSocket { }); }; sendText(obj) { - this.socket.write(encodeMessage(opcodes.TEXT,new Buffer(obj, "utf8"))) + this.socket.write(encodeMessage(opcodes.TEXT,Buffer.from(obj, "utf8"))) }; } exports.MiniWebSocket = MiniWebSocket \ No newline at end of file