mirror of
https://github.com/MichMich/MagicMirror.git
synced 2025-06-28 17:44:52 -04:00
Add server (web/socket), create socket system, better helper loader.
- The Magic Mirror is now hosted via a express server, allowing you to load it from an external client (for debugging.) - It now includes a socket system to communicate between the node_helper and the client module. - node_helpers are now only loaded if the module is configured in the config.
This commit is contained in:
parent
15856574d7
commit
899d05bc32
12 changed files with 531 additions and 152 deletions
83
js/socketclient.js
Normal file
83
js/socketclient.js
Normal file
|
@ -0,0 +1,83 @@
|
|||
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;
|
||||
|
||||
if (typeof moduleName !== 'string') {
|
||||
throw new Error('Please set the module name for the MMSocket.');
|
||||
}
|
||||
|
||||
self.moduleName = moduleName;
|
||||
|
||||
// Private Methods
|
||||
var socketBase = (typeof window === 'undefined') ? 'http://localhost:'+config.port : '';
|
||||
socket = io(socketBase + '/' + self.moduleName);
|
||||
|
||||
var notificationCallback = function() {};
|
||||
|
||||
socket.on('connect', function(s) {
|
||||
|
||||
// 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 notification: ' + notification +', payload: ' + payload);
|
||||
notificationCallback(notification, payload);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
|
||||
var sendNotification = function(notification, payload) {
|
||||
//console.log('Send notification: ' + notification +', payload: ' + payload);
|
||||
socket.emit(notification, payload);
|
||||
};
|
||||
|
||||
// Public Methods
|
||||
this.setNotificationCallback = function(callback) {
|
||||
notificationCallback = callback;
|
||||
};
|
||||
|
||||
this.sendNotification = function(notification, payload) {
|
||||
if (typeof payload === 'undefined') {
|
||||
payload = {};
|
||||
}
|
||||
sendNotification(notification, payload);
|
||||
};
|
||||
};
|
||||
|
||||
if (typeof module !== 'undefined') {
|
||||
module.exports = MMSocket;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue