/* Magic Mirror * Node Helper Superclass * * By Michael Teeuw http://michaelteeuw.nl * MIT Licensed. */ var Class = require('../../../js/class.js'); NodeHelper = Class.extend({ init: function() { console.log('Initializing new module helper ...'); }, start: function() { console.log('Staring module helper: ' + this.name); }, /* socketNotificationReceived(notification, payload) * This method is called when a socket notification arrives. * * argument notification string - The identifier of the noitication. * argument payload mixed - The payload of the notification. */ socketNotificationReceived: function(notification, payload) { console.log(this.name + ' received a socket notification: ' + notification + ' - Payload: ' + payload); }, /* setName(data) * Set the module name. * * argument name string - Module name. */ setName: function(name) { this.name = name; }, /* sendSocketNotification(notification, payload) * Send a socket notification to the node helper. * * argument notification string - The identifier of the noitication. * argument payload mixed - The payload of the notification. */ sendSocketNotification: function(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); }); }); } }); NodeHelper.create = function(moduleDefinition) { return NodeHelper.extend(moduleDefinition); }; module.exports = NodeHelper;