From 73bc2e41877e1e73afa3dbe730842527da3006ed Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Thu, 8 Feb 2018 23:21:02 +0100 Subject: [PATCH] ipc: remove socket.io, rewrite ultra slim ipc --- CHANGELOG.md | 2 +- README.md | 2 +- app/main/callbacks.js | 7 +- app/main/ipc/client.js | 124 ++++++++++++++++++++++++ app/main/ipc/server.js | 59 +++++++++++ app/main/server.js | 26 ++--- app/package.json | 2 +- package.json | 1 - resources/docs/credits.md | 2 +- src/browser/js/app/ipc/index.js | 129 ++++++++++++++++++++++--- src/browser/js/app/managers/widgets.js | 4 +- 11 files changed, 317 insertions(+), 41 deletions(-) create mode 100644 app/main/ipc/client.js create mode 100644 app/main/ipc/server.js diff --git a/CHANGELOG.md b/CHANGELOG.md index de0eebb8..da8f28a5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,11 +6,11 @@ - regression preventing dropdown to display no option selected - crossfaders display issue on iOS - restored compatibility options for server-client communications - - prevent a client to load multiple sessions simultaneously - xy: `doubleTap` option - range: `spring` option - main - theme files changes are automatically applied + - server-client communication system rewritten -> HUGE latency reduction and stability improvement especially under stress conditions - widgets - switch: wrap non-array `values` property in an array - dropdown: wrap non-array `values` property in an array; always add a default empty value diff --git a/README.md b/README.md index 5c7187da..a675e8a0 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ The only supported client browsers are the one provided with the server and Chro Copyleft © Jean-Emmanuel @ [AMMD](http://ammd.net) 2017 -This program is a free software released under the GNU/GPL3 license, it relies on the use of several other free softwares, including [Node.js](https://nodejs.org/), [Electron](http://electron.atom.io/), [osc.js](https://github.com/colinbdclark/osc.js), [socket.io](http://socket.io), [browserify](http://browserify.org), [exorcist](https://github.com/thlorenz/exorcist), [express](http://expressjs.com), [node-sass](https://github.com/sass/node-sass), [jQuery](http://jquery.com/), [jQuery-UI](http://jqueryui.com/), [Font Awesome](http://fontawesome.io/), [source-map](https://github.com/mozilla/source-map), [mathjs](http://mathjs.org/) +This program is a free software released under the GNU/GPL3 license, it relies on the use of several other free softwares, including [Node.js](https://nodejs.org/), [Electron](http://electron.atom.io/), [osc.js](https://github.com/colinbdclark/osc.js), [browserify](http://browserify.org), [exorcist](https://github.com/thlorenz/exorcist), [express](http://expressjs.com), [node-sass](https://github.com/sass/node-sass), [jQuery](http://jquery.com/), [jQuery-UI](http://jqueryui.com/), [Font Awesome](http://fontawesome.io/), [source-map](https://github.com/mozilla/source-map), [mathjs](http://mathjs.org/) ---- diff --git a/app/main/callbacks.js b/app/main/callbacks.js index a0b38726..19895148 100644 --- a/app/main/callbacks.js +++ b/app/main/callbacks.js @@ -135,7 +135,7 @@ module.exports = { }, sessionOpened: function(data, clientId) { - clients[clientId].broadcast.emit('stateSend') + ipc.send('stateSend', null, null, clientId) }, savingSession: function(data, clientId) { @@ -158,7 +158,8 @@ module.exports = { data.args = data.preArgs ? data.preArgs.concat(value) : [value] - if (!data.noSync) clients[clientId].broadcast.emit('receiveOsc', data) + if (!data.noSync) ipc.send('receiveOsc', data, null, clientId) + }, @@ -214,7 +215,7 @@ module.exports = { } - if (!data.noSync) clients[clientId].broadcast.emit('receiveOsc', data) + if (!data.noSync) ipc.send('receiveOsc', data, null, clientId) }, diff --git a/app/main/ipc/client.js b/app/main/ipc/client.js new file mode 100644 index 00000000..d7888244 --- /dev/null +++ b/app/main/ipc/client.js @@ -0,0 +1,124 @@ +var EventEmitter = require('events').EventEmitter + +var hearbeatInterval = 25000, + hearbeatTimeout = 5000, + dieTimeout = 60000 + +class Socket extends EventEmitter { + + constructor(ws, id) { + + super() + + this.id = id + this.socket = null + + this.queue = [] + + this.hearbeat = undefined + this.hearbeatTimeout = undefined + this.on('pong', ()=>{ + console.log('pong received') + this.hearbeatTimeout = clearTimeout(this.hearbeatTimeout) + }) + this.on('ping', ()=>{ + this.socket.send(`["pong"]`) + }) + + this.dieTimeout = undefined + + this.open(ws) + + } + + open(ws) { + + this.socket = ws + + this.socket.onmessage = (e)=>{ + this.receive(e.data) + } + + this.socket.onclose = ()=>{ + console.log('close') + this.close() + } + + this.socket.onerror = ()=>{ + console.log('error') + this.close() + } + + this.dieTimeout = clearTimeout(this.dieTimeout) + + this.hearbeat = setInterval(()=>{ + console.log('ping sent') + this.socket.send(`["ping"]`) + this.hearbeatTimeout = setTimeout(()=>{ + this.socket.close() + }, hearbeatTimeout) + }, hearbeatInterval) + + } + + close() { + + this.hearbeat = clearInterval(this.hearbeat) + + this.dieTimeout = setTimeout(()=>{ + + this.emit('close') + + }, dieTimeout) + + } + + connected() { + + return this.socket.readyState == this.socket.OPEN + + } + + receive(message) { + + if (typeof message == 'string') { + + var packet = JSON.parse(message) + + if (Array.isArray(packet) && typeof packet[0] == "string") { + + this.emit(packet[0], packet[1]) + + } + + } + + } + + send(event, data) { + + var packet = JSON.stringify([event, data]) + + if (this.connected()) { + this.socket.send(packet) + } else { + this.queue.push(packet) + } + + } + + flush(){ + + for (var i in this.queue) { + + this.socket.send(this.queue[i]) + + } + + this.queue = [] + + } + +} + +module.exports = Socket diff --git a/app/main/ipc/server.js b/app/main/ipc/server.js new file mode 100644 index 00000000..49936a77 --- /dev/null +++ b/app/main/ipc/server.js @@ -0,0 +1,59 @@ +var EventEmitter = require('events').EventEmitter, + WebSocketServer = require('ws').Server, + Client = require('./client') + +class Ipc extends EventEmitter { + + constructor(server) { + + super() + + this.clients = {} + this.server = null + + this.server = new WebSocketServer({server: server}) + + this.server.on('connection', (socket, req) => { + + var id = req.url.split('/').pop() + + if (!id) return + + if (!this.clients[id]) { + + var client = new Client(socket, id) + this.clients[id] = client + this.emit('connection', client) + + client.on('close', ()=>{ + console.log('client died for real') + delete this.clients[id] + }) + + } else { + + this.clients[id].open(socket) + this.clients[id].flush() + + } + + }) + + } + + send(event, data, id, except) { + + var clients = id ? [this.clients[id]] : this.clients + + for (var k in clients) { + if (!except || this.clients[k] != this.clients[except]) { + clients[k].send(event, data) + } + } + + } + + +} + +module.exports = Ipc diff --git a/app/main/server.js b/app/main/server.js index acc3fffd..999238ff 100644 --- a/app/main/server.js +++ b/app/main/server.js @@ -2,8 +2,8 @@ var express = require('express')(), path = require('path'), http = require('http'), server = http.createServer(express), - io = require('socket.io')(server), - ipc = {}, + Ipc = require('./ipc/server') + ipc = new Ipc(server), settings = require('./settings'), zeroconf = require('./zeroconf'), appAddresses = settings.read('appAddresses'), @@ -36,34 +36,26 @@ zeroconf.publish({ port: settings.read('httpPort') }).on('error', (e)=>{ console.error(`Error: Zeroconf: ${e.message}`) - }) - -ipc.send = function(name,data,clientId) { - if (clientId) { - io.to(clientId).emit(name,data) - } else { - io.emit(name,data) - } -} +}) var bindCallbacks = function(callbacks) { - io.on('connection', function(socket) { - clients[socket.id] = socket + ipc.on('connection', function(client) { for (let name in callbacks) { - socket.on(name, (data)=>{ - callbacks[name](data,socket.id) + client.on(name, (data)=>{ + callbacks[name](data, client.id) }) } - socket.on('disconnect', function() { + client.on('close', function() { - callbacks.removeClientWidgets(socket.id) + callbacks.removeClientWidgets(client.id) }) }) + } diff --git a/app/package.json b/app/package.json index 3f1fba9e..236edc88 100644 --- a/app/package.json +++ b/app/package.json @@ -28,8 +28,8 @@ "jquery": "3.3.1", "osc": "2.2.0", "python-shell": "0.4.0", - "socket.io": "2.0.4", "vmodule": "1.0.1", + "ws": "^4.0.0", "yargs": "11.0.0" }, "scripts": { diff --git a/package.json b/package.json index 816fdd9d..a2a476ca 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,6 @@ "nodemon": "1.14.11", "screenfull": "3.3.2", "shortid": "2.2.8", - "socket.io-client": "2.0.4", "stacktrace-js": "^2.0.0", "through": "^2.3.8", "uglifyify": "4.0.5", diff --git a/resources/docs/credits.md b/resources/docs/credits.md index 0b2cc8b8..62c07f7f 100644 --- a/resources/docs/credits.md +++ b/resources/docs/credits.md @@ -6,7 +6,7 @@ This program is a free software released under the [GNU/GPL3](ttps://github.com/ ## Dependencies -Open Stage Control relies on several other free softwares, including [Node.js](https://nodejs.org/), [Electron](http://electron.atom.io/), [osc.js](https://github.com/colinbdclark/osc.js), [socket.io](http://socket.io), [babel](http://babeljs.io/), [browserify](http://browserify.org), [exorcist](https://github.com/thlorenz/exorcist), [express](http://expressjs.com), [node-sass](https://github.com/sass/node-sass), [jQuery](http://jquery.com/), [jQuery-UI](http://jqueryui.com/), [Font Awesome](http://fontawesome.io/), [stacktracejs](stacktracejs.com), [mathjs](http://mathjs.org/). A comprehensive and up-to-date list can be found by reading these [two](https://github.com/jean-emmanuel/open-stage-control/blob/master/package.json) [files](https://github.com/jean-emmanuel/open-stage-control/blob/master/app/package.json). +Open Stage Control relies on several other free softwares, including [Node.js](https://nodejs.org/), [Electron](http://electron.atom.io/), [osc.js](https://github.com/colinbdclark/osc.js), [babel](http://babeljs.io/), [browserify](http://browserify.org), [exorcist](https://github.com/thlorenz/exorcist), [express](http://expressjs.com), [node-sass](https://github.com/sass/node-sass), [jQuery](http://jquery.com/), [jQuery-UI](http://jqueryui.com/), [Font Awesome](http://fontawesome.io/), [stacktracejs](stacktracejs.com), [mathjs](http://mathjs.org/). A comprehensive and up-to-date list can be found by reading these [two](https://github.com/jean-emmanuel/open-stage-control/blob/master/package.json) [files](https://github.com/jean-emmanuel/open-stage-control/blob/master/app/package.json). ## Contributors diff --git a/src/browser/js/app/ipc/index.js b/src/browser/js/app/ipc/index.js index 9c57eb3b..6248edf7 100644 --- a/src/browser/js/app/ipc/index.js +++ b/src/browser/js/app/ipc/index.js @@ -1,25 +1,33 @@ -var io = require('socket.io-client/dist/socket.io.slim.js') +var EventEmitter = require('wolfy87-eventemitter'), + uuid = require('shortid').generate() +var reconnectInterval = 5000, + hearbeatInterval = 25000, + hearbeatTimeout = 5000 -var Ipc = class Ipc { +class Ipc extends EventEmitter { - constructor() { + constructor() { - this.socket = io.connect('/') + super() - } + this.socket = null + this.connected = false + this.queue = [] - send(event, data) { + this.reconnect = undefined - this.socket.compress(false).emit(event, data) + this.hearbeat = undefined + this.hearbeatTimeout = undefined + this.on('pong', ()=>{ + this.hearbeatTimeout = clearTimeout(this.hearbeatTimeout) + }) + this.on('ping', ()=>{ + this.socket.send(`["pong"]`) + }) - } - - on(name, fn) { - - - this.socket.on(name, fn) + this.open() } @@ -35,8 +43,101 @@ var Ipc = class Ipc { } } -} + close() { + + this.socket = null + + this.connected = false + + this.hearbeat = clearInterval(this.hearbeat) + this.hearbeatTimeout = clearTimeout(this.hearbeatTimeout) + + if (!this.reconnect) { + this.reconnect = setInterval(()=>{ + this.open() + }, reconnectInterval) + } + + } + + open() { + + this.socket = new WebSocket('ws://' + window.location.host + '/' + uuid) + + this.socket.onopen = (e)=>{ + + this.connected = true + + this.reconnect = clearInterval(this.reconnect) + + this.hearbeat = setInterval(()=>{ + this.socket.send(`["ping"]`) + this.hearbeatTimeout = setTimeout(()=>{ + this.socket.close() + }, hearbeatTimeout) + }, hearbeatInterval) + + this.trigger('connect') + this.flush() + + } + + this.socket.onmessage = (e)=>{ + this.receive(e.data) + } + + this.socket.onclose = (e)=>{ + this.close() + } + + this.socket.onerror = (e)=>{ + this.close() + } + + } + + receive(message) { + + if (typeof message == 'string') { + + var packet = JSON.parse(message); + + if (Array.isArray(packet) && typeof packet[0] == "string") { + + this.emit(packet[0], packet[1]) + + } + + } + + } + + send(event, data) { + + var packet = JSON.stringify([event, data]) + + if (this.connected) { + this.socket.send(packet) + } else { + this.queue.push(packet) + } + + } + + flush(){ + + for (var i in this.queue) { + + this.socket.send(this.queue[i]) + + } + + this.queue = [] + + } + +} var ipc = new Ipc() diff --git a/src/browser/js/app/managers/widgets.js b/src/browser/js/app/managers/widgets.js index fb332556..23f9bb62 100644 --- a/src/browser/js/app/managers/widgets.js +++ b/src/browser/js/app/managers/widgets.js @@ -19,9 +19,9 @@ var WidgetManager = class WidgetManager extends EventEmitter { this.on('change', this.onChange, this) - ipc.on('reconnect', ()=>{ + ipc.on('connect', ()=>{ for (var hash in this.widgets) { - this.registerWidget(this.widget[hash]) + this.registerWidget(this.widgets[hash]) } })