clean up node helper

This commit is contained in:
Felix Wiedenbach
2021-01-05 18:44:36 +01:00
parent ac141a4316
commit 5d60534dc9

View File

@@ -8,18 +8,18 @@ const Class = require("./class.js");
const Log = require("./logger.js"); const Log = require("./logger.js");
const express = require("express"); const express = require("express");
var NodeHelper = Class.extend({ const NodeHelper = Class.extend({
init: function () { init() {
Log.log("Initializing new module helper ..."); Log.log("Initializing new module helper ...");
}, },
loaded: function (callback) { loaded(callback) {
Log.log("Module helper loaded: " + this.name); Log.log(`Module helper loaded: ${this.name}`);
callback(); callback();
}, },
start: function () { start() {
Log.log("Starting module helper: " + this.name); Log.log(`Starting module helper: ${this.name}`);
}, },
/* stop() /* stop()
@@ -28,8 +28,8 @@ var NodeHelper = Class.extend({
* gracefully exit the module. * gracefully exit the module.
* *
*/ */
stop: function () { stop() {
Log.log("Stopping module helper: " + this.name); Log.log(`Stopping module helper: ${this.name}`);
}, },
/* socketNotificationReceived(notification, payload) /* socketNotificationReceived(notification, payload)
@@ -38,8 +38,8 @@ var NodeHelper = Class.extend({
* argument notification string - The identifier of the notification. * argument notification string - The identifier of the notification.
* argument payload mixed - The payload of the notification. * argument payload mixed - The payload of the notification.
*/ */
socketNotificationReceived: function (notification, payload) { socketNotificationReceived(notification, payload) {
Log.log(this.name + " received a socket notification: " + notification + " - Payload: " + payload); Log.log(`${this.name} received a socket notification: ${notification} - Payload: ${payload}`);
}, },
/* setName(name) /* setName(name)
@@ -47,7 +47,7 @@ var NodeHelper = Class.extend({
* *
* argument name string - Module name. * argument name string - Module name.
*/ */
setName: function (name) { setName(name) {
this.name = name; this.name = name;
}, },
@@ -56,7 +56,7 @@ var NodeHelper = Class.extend({
* *
* argument path string - Module path. * argument path string - Module path.
*/ */
setPath: function (path) { setPath(path) {
this.path = path; this.path = path;
}, },
@@ -66,7 +66,7 @@ var NodeHelper = Class.extend({
* argument notification string - The identifier of the notification. * argument notification string - The identifier of the notification.
* argument payload mixed - The payload of the notification. * argument payload mixed - The payload of the notification.
*/ */
sendSocketNotification: function (notification, payload) { sendSocketNotification(notification, payload) {
this.io.of(this.name).emit(notification, payload); this.io.of(this.name).emit(notification, payload);
}, },
@@ -76,11 +76,10 @@ var NodeHelper = Class.extend({
* *
* argument app Express app - The Express app object. * argument app Express app - The Express app object.
*/ */
setExpressApp: function (app) { setExpressApp(app) {
this.expressApp = app; this.expressApp = app;
var publicPath = this.path + "/public"; app.use(`/${this.name}`, express.static(`${this.path}/public`));
app.use("/" + this.name, express.static(publicPath));
}, },
/* setSocketIO(io) /* setSocketIO(io)
@@ -89,27 +88,25 @@ var NodeHelper = Class.extend({
* *
* argument io Socket.io - The Socket io object. * argument io Socket.io - The Socket io object.
*/ */
setSocketIO: function (io) { setSocketIO(io) {
var self = this; this.io = io;
self.io = io;
Log.log("Connecting socket for: " + this.name); Log.log(`Connecting socket for: ${this.name}`);
var namespace = this.name;
io.of(namespace).on("connection", function (socket) { io.of(this.name).on("connection", (socket) => {
// add a catch all event. // add a catch all event.
var onevent = socket.onevent; const onevent = socket.onevent;
socket.onevent = function (packet) { socket.onevent = function (packet) {
var args = packet.data || []; const args = packet.data || [];
onevent.call(this, packet); // original call onevent.call(this, packet); // original call
packet.data = ["*"].concat(args); packet.data = ["*"].concat(args);
onevent.call(this, packet); // additional call to catch-all onevent.call(this, packet); // additional call to catch-all
}; };
// register catch all. // register catch all.
socket.on("*", function (notification, payload) { socket.on("*", (notification, payload) => {
if (notification !== "*") { if (notification !== "*") {
//Log.log('received message in namespace: ' + namespace); this.socketNotificationReceived(notification, payload);
self.socketNotificationReceived(notification, payload);
} }
}); });
}); });
@@ -120,7 +117,4 @@ NodeHelper.create = function (moduleDefinition) {
return NodeHelper.extend(moduleDefinition); return NodeHelper.extend(moduleDefinition);
}; };
/*************** DO NOT EDIT THE LINE BELOW ***************/
if (typeof module !== "undefined") {
module.exports = NodeHelper; module.exports = NodeHelper;
}