mirror of
https://github.com/MichMich/MagicMirror.git
synced 2025-08-21 12:55:22 +00:00
Improved socket connection for node_helper.
This commit is contained in:
51
js/server.js
51
js/server.js
@@ -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);
|
||||
}
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user