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

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