From b243d25399e9538a0dc563e3fd7dd58c864937c1 Mon Sep 17 00:00:00 2001 From: Michael Teeuw Date: Wed, 30 Mar 2016 16:25:24 +0200 Subject: [PATCH] Improved socket connection for node_helper. --- .gitignore | 1 + README.md | 2 - js/electron.js | 24 +++++++--- js/server.js | 51 +--------------------- js/socketclient.js | 31 +------------ modules/node_modules/node_helper/index.js | 53 ++++++++++++++--------- 6 files changed, 55 insertions(+), 107 deletions(-) diff --git a/.gitignore b/.gitignore index ba02955070..fbe259c422 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ !/modules/node_helper/** /modules/* +!/modules/calendar !/modules/clock !/modules/compliments !/modules/currentweather diff --git a/README.md b/README.md index a15eb07426..e72ca3e5df 100644 --- a/README.md +++ b/README.md @@ -17,8 +17,6 @@ Things that still have to be implemented or changed. ####Loader - Loading of module uses `eval()`. We might want to look into a better solution. [loader.js#L112](https://github.com/MichMich/MagicMirror/blob/v2-beta/js/loader.js#L112). -####NodeHelper -- The node_helper superclass creates a seperate socket connection for each module. It's preferred to use the overall socket connection of the server. ##Modules diff --git a/js/electron.js b/js/electron.js index c83164a489..a736ea50bf 100755 --- a/js/electron.js +++ b/js/electron.js @@ -15,6 +15,8 @@ const app = electron.app; // Module to create native browser window. const BrowserWindow = electron.BrowserWindow; +var nodeHelpers = []; + // Keep a global reference of the window object, if you don't, the window will // be closed automatically when the JavaScript object is garbage collected. let mainWindow; @@ -68,7 +70,7 @@ function loadModule(moduleName) { var Module = require(helperPath); var m = new Module(); m.setName(moduleName); - m.start(); + nodeHelpers.push(m); } } @@ -95,16 +97,26 @@ loadConfig(function(c) { } loadModules(modules); + + var server = new Server(config, function(io) { + console.log('Server started ...'); + + for (var h in nodeHelpers) { + var nodeHelper = nodeHelpers[h]; + nodeHelper.setSocketIO(io); + nodeHelper.start(); + } + + console.log('Sockets connected & modules started ...'); + + }); }); // This method will be called when Electron has finished // initialization and is ready to create browser windows. app.on('ready', function() { - var server = new Server(config, function() { - setTimeout(function() { - createWindow(); - }, 1000); - }); + console.log('Launching application.'); + createWindow(); }); // Quit when all windows are closed. diff --git a/js/server.js b/js/server.js index 32670a468a..4440e1aa37 100644 --- a/js/server.js +++ b/js/server.js @@ -12,53 +12,6 @@ var io = require('socket.io')(server); var path = require('path'); var Server = function(config, callback) { - - /* createNamespace(namespace) - * Creates a namespace with a wildcard event. - * - * argument namespace string - The name of the namespace. - */ - var createNamespace = function(namespace) { - console.log('Creating socket namespace: ' + namespace); - - io.of(namespace).on('connection', function (socket) { - console.log("New socket connection on namespace: " + namespace); - - // add a catch all event. - var onevent = socket.onevent; - socket.onevent = function (packet) { - var args = packet.data || []; - onevent.call (this, packet); // original call - packet.data = ["*"].concat(args); - onevent.call(this, packet); // additional call to catch-all - }; - - // register catch all. - socket.on('*', function (event, data) { - io.of(namespace).emit(event, data); - }); - }); - }; - - /* createNamespaces() - * Creates a namespace for all modules in the config. - */ - var createNamespaces = function() { - var modules = []; - var m; - - for (m in config.modules) { - var module = config.modules[m]; - if (modules.indexOf(module.module) === -1) { - modules.push(module.module); - } - } - - for (m in modules) { - createNamespace(modules[m]); - } - }; - console.log("Starting server op port " + config.port + " ... "); server.listen(config.port); @@ -73,10 +26,8 @@ var Server = function(config, callback) { res.sendFile(path.resolve(__dirname + '/../index.html')); }); - createNamespaces(); - if (typeof callback === 'function') { - callback(); + callback(io); } }; diff --git a/js/socketclient.js b/js/socketclient.js index a3cb57bdf0..81341627d0 100644 --- a/js/socketclient.js +++ b/js/socketclient.js @@ -1,27 +1,3 @@ -if (typeof window === 'undefined') { - // Only perfom this part if is isn't running in the browser. - - // Load socket client - var io = require('socket.io-client'); - - // Load config - var fs = require('fs'); - - var config = {}; - - var defaults = require(__dirname + '/defaults.js'); - var configFilename = __dirname + '/../config/config.js'; - - try { - fs.accessSync(configFilename, fs.R_OK); - var c = require(configFilename); - config = Object.assign(defaults, c); - } catch (e) { - config = defaults; - } -} - - var MMSocket = function(moduleName) { var self = this; @@ -35,6 +11,7 @@ var MMSocket = function(moduleName) { // Private Methods var socketBase = (typeof window === 'undefined') ? 'http://localhost:'+config.port : ''; socket = io(socketBase + '/' + self.moduleName); + console.log(socketBase + '/' + self.moduleName); var notificationCallback = function() {}; @@ -76,8 +53,4 @@ var MMSocket = function(moduleName) { } sendNotification(notification, payload); }; -}; - -if (typeof module !== 'undefined') { - module.exports = MMSocket; -} \ No newline at end of file +}; \ No newline at end of file diff --git a/modules/node_modules/node_helper/index.js b/modules/node_modules/node_helper/index.js index 3ccead1713..7d6b18343c 100644 --- a/modules/node_modules/node_helper/index.js +++ b/modules/node_modules/node_helper/index.js @@ -6,7 +6,6 @@ */ var Class = require('../../../js/class.js'); -var MMSocket = require('../../../js/socketclient.js'); NodeHelper = Class.extend({ init: function() { @@ -34,24 +33,6 @@ NodeHelper = Class.extend({ */ setName: function(name) { this.name = name; - this.socket(); - }, - - /* socket() - * Returns a socket object. If it doesn't exsist, it's created. - * It also registers the notification callback. - */ - socket: function() { - if (typeof this._socket === 'undefined') { - this._socket = this._socket = new MMSocket(this.name); - } - - var self = this; - this._socket.setNotificationCallback(function(notification, payload) { - self.socketNotificationReceived(notification, payload); - }); - - return this._socket; }, /* sendSocketNotification(notification, payload) @@ -61,7 +42,39 @@ NodeHelper = Class.extend({ * argument payload mixed - The payload of the notification. */ sendSocketNotification: function(notification, payload) { - this.socket().sendNotification(notification, payload); + this.io.of(this.name).emit(notification, payload); + }, + + /* setSocketIO(io) + * Sets the socket io object for this module. + * Binds message receiver. + * + * argument io Socket.io - The Socket io object. + */ + setSocketIO: function(io) { + var self = this; + self.io = io; + + console.log('Connecting socket for: ' + this.name); + var namespace = this.name; + io.of(namespace).on('connection', function (socket) { + // add a catch all event. + var onevent = socket.onevent; + socket.onevent = function (packet) { + var args = packet.data || []; + onevent.call (this, packet); // original call + packet.data = ["*"].concat(args); + onevent.call(this, packet); // additional call to catch-all + }; + + // register catch all. + socket.on('*', function (notification, payload) { + if (notification !== '*') + console.log('received message in namespace: ' + namespace); + self.socketNotificationReceived(notification, payload); + }); + }); + } });