mirror of
https://github.com/MichMich/MagicMirror.git
synced 2025-06-27 11:50:00 +00:00
Improved socket connection for node_helper.
This commit is contained in:
parent
59b339aba5
commit
b243d25399
1
.gitignore
vendored
1
.gitignore
vendored
@ -4,6 +4,7 @@
|
|||||||
!/modules/node_helper/**
|
!/modules/node_helper/**
|
||||||
|
|
||||||
/modules/*
|
/modules/*
|
||||||
|
!/modules/calendar
|
||||||
!/modules/clock
|
!/modules/clock
|
||||||
!/modules/compliments
|
!/modules/compliments
|
||||||
!/modules/currentweather
|
!/modules/currentweather
|
||||||
|
@ -17,8 +17,6 @@ Things that still have to be implemented or changed.
|
|||||||
####Loader
|
####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).
|
- 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
|
##Modules
|
||||||
|
|
||||||
|
@ -15,6 +15,8 @@ const app = electron.app;
|
|||||||
// Module to create native browser window.
|
// Module to create native browser window.
|
||||||
const BrowserWindow = electron.BrowserWindow;
|
const BrowserWindow = electron.BrowserWindow;
|
||||||
|
|
||||||
|
var nodeHelpers = [];
|
||||||
|
|
||||||
// Keep a global reference of the window object, if you don't, the window will
|
// 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.
|
// be closed automatically when the JavaScript object is garbage collected.
|
||||||
let mainWindow;
|
let mainWindow;
|
||||||
@ -68,7 +70,7 @@ function loadModule(moduleName) {
|
|||||||
var Module = require(helperPath);
|
var Module = require(helperPath);
|
||||||
var m = new Module();
|
var m = new Module();
|
||||||
m.setName(moduleName);
|
m.setName(moduleName);
|
||||||
m.start();
|
nodeHelpers.push(m);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -95,16 +97,26 @@ loadConfig(function(c) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
loadModules(modules);
|
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
|
// This method will be called when Electron has finished
|
||||||
// initialization and is ready to create browser windows.
|
// initialization and is ready to create browser windows.
|
||||||
app.on('ready', function() {
|
app.on('ready', function() {
|
||||||
var server = new Server(config, function() {
|
console.log('Launching application.');
|
||||||
setTimeout(function() {
|
createWindow();
|
||||||
createWindow();
|
|
||||||
}, 1000);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Quit when all windows are closed.
|
// Quit when all windows are closed.
|
||||||
|
51
js/server.js
51
js/server.js
@ -12,53 +12,6 @@ var io = require('socket.io')(server);
|
|||||||
var path = require('path');
|
var path = require('path');
|
||||||
|
|
||||||
var Server = function(config, callback) {
|
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 + " ... ");
|
console.log("Starting server op port " + config.port + " ... ");
|
||||||
|
|
||||||
server.listen(config.port);
|
server.listen(config.port);
|
||||||
@ -73,10 +26,8 @@ var Server = function(config, callback) {
|
|||||||
res.sendFile(path.resolve(__dirname + '/../index.html'));
|
res.sendFile(path.resolve(__dirname + '/../index.html'));
|
||||||
});
|
});
|
||||||
|
|
||||||
createNamespaces();
|
|
||||||
|
|
||||||
if (typeof callback === 'function') {
|
if (typeof callback === 'function') {
|
||||||
callback();
|
callback(io);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -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 MMSocket = function(moduleName) {
|
||||||
|
|
||||||
var self = this;
|
var self = this;
|
||||||
@ -35,6 +11,7 @@ var MMSocket = function(moduleName) {
|
|||||||
// Private Methods
|
// Private Methods
|
||||||
var socketBase = (typeof window === 'undefined') ? 'http://localhost:'+config.port : '';
|
var socketBase = (typeof window === 'undefined') ? 'http://localhost:'+config.port : '';
|
||||||
socket = io(socketBase + '/' + self.moduleName);
|
socket = io(socketBase + '/' + self.moduleName);
|
||||||
|
console.log(socketBase + '/' + self.moduleName);
|
||||||
|
|
||||||
var notificationCallback = function() {};
|
var notificationCallback = function() {};
|
||||||
|
|
||||||
@ -77,7 +54,3 @@ var MMSocket = function(moduleName) {
|
|||||||
sendNotification(notification, payload);
|
sendNotification(notification, payload);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
if (typeof module !== 'undefined') {
|
|
||||||
module.exports = MMSocket;
|
|
||||||
}
|
|
53
modules/node_modules/node_helper/index.js
generated
vendored
53
modules/node_modules/node_helper/index.js
generated
vendored
@ -6,7 +6,6 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
var Class = require('../../../js/class.js');
|
var Class = require('../../../js/class.js');
|
||||||
var MMSocket = require('../../../js/socketclient.js');
|
|
||||||
|
|
||||||
NodeHelper = Class.extend({
|
NodeHelper = Class.extend({
|
||||||
init: function() {
|
init: function() {
|
||||||
@ -34,24 +33,6 @@ NodeHelper = Class.extend({
|
|||||||
*/
|
*/
|
||||||
setName: function(name) {
|
setName: function(name) {
|
||||||
this.name = 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)
|
/* sendSocketNotification(notification, payload)
|
||||||
@ -61,7 +42,39 @@ NodeHelper = Class.extend({
|
|||||||
* argument payload mixed - The payload of the notification.
|
* argument payload mixed - The payload of the notification.
|
||||||
*/
|
*/
|
||||||
sendSocketNotification: function(notification, payload) {
|
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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user