Improved socket connection for node_helper.

This commit is contained in:
Michael Teeuw
2016-03-30 16:25:24 +02:00
parent 59b339aba5
commit b243d25399
6 changed files with 55 additions and 107 deletions

View File

@@ -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.

View File

@@ -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);
}
};

View File

@@ -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;
}
};