Skip to content

Commit

Permalink
Improved socket connection for node_helper.
Browse files Browse the repository at this point in the history
  • Loading branch information
MichMich committed Mar 30, 2016
1 parent 59b339a commit b243d25
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 107 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
!/modules/node_helper/**

/modules/*
!/modules/calendar
!/modules/clock
!/modules/compliments
!/modules/currentweather
Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
24 changes: 18 additions & 6 deletions js/electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -68,7 +70,7 @@ function loadModule(moduleName) {
var Module = require(helperPath);
var m = new Module();
m.setName(moduleName);
m.start();
nodeHelpers.push(m);
}
}

Expand All @@ -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.
Expand Down
51 changes: 1 addition & 50 deletions js/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -73,10 +26,8 @@ var Server = function(config, callback) {
res.sendFile(path.resolve(__dirname + '/../index.html'));
});

createNamespaces();

if (typeof callback === 'function') {
callback();
callback(io);
}
};

Expand Down
31 changes: 2 additions & 29 deletions js/socketclient.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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() {};

Expand Down Expand Up @@ -76,8 +53,4 @@ var MMSocket = function(moduleName) {
}
sendNotification(notification, payload);
};
};

if (typeof module !== 'undefined') {
module.exports = MMSocket;
}
};
53 changes: 33 additions & 20 deletions modules/node_modules/node_helper/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b243d25

Please sign in to comment.