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:
Michael Teeuw
2016-03-30 12:20:46 +02:00
parent 15856574d7
commit 899d05bc32
12 changed files with 531 additions and 152 deletions

83
js/server.js Normal file
View File

@@ -0,0 +1,83 @@
/* Magic Mirror
* Server
*
* By Michael Teeuw http://michaelteeuw.nl
* MIT Licensed.
*/
var express = require('express');
var app = require('express')();
var server = require('http').Server(app);
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);
app.use('/js', express.static(__dirname));
app.use('/config', express.static(path.resolve(__dirname + '/../config')));
app.use('/css', express.static(path.resolve(__dirname + '/../css')));
app.use('/fonts', express.static(path.resolve(__dirname + '/../fonts')));
app.use('/modules', express.static(path.resolve(__dirname + '/../modules')));
app.use('/vendor', express.static(path.resolve(__dirname + '/../vendor')));
app.get('/', function (req, res) {
res.sendFile(path.resolve(__dirname + '/../index.html'));
});
createNamespaces();
if (typeof callback === 'function') {
callback();
}
};
module.exports = Server;