2020-05-02 10:39:09 +02:00
|
|
|
/* global io */
|
|
|
|
|
2022-01-26 23:09:26 +01:00
|
|
|
/* MagicMirror²
|
2020-05-02 10:39:09 +02:00
|
|
|
* TODO add description
|
|
|
|
*
|
|
|
|
* By Michael Teeuw https://michaelteeuw.nl
|
|
|
|
* MIT Licensed.
|
|
|
|
*/
|
2021-04-10 08:21:27 +02:00
|
|
|
const MMSocket = function (moduleName) {
|
2016-04-05 14:35:11 -04:00
|
|
|
if (typeof moduleName !== "string") {
|
|
|
|
throw new Error("Please set the module name for the MMSocket.");
|
2016-03-30 12:20:46 +02:00
|
|
|
}
|
|
|
|
|
2021-04-10 08:21:27 +02:00
|
|
|
this.moduleName = moduleName;
|
2016-03-30 12:20:46 +02:00
|
|
|
|
|
|
|
// Private Methods
|
2021-04-10 08:21:27 +02:00
|
|
|
let base = "/";
|
2020-05-11 22:22:32 +02:00
|
|
|
if (typeof config !== "undefined" && typeof config.basePath !== "undefined") {
|
2020-04-07 19:35:21 +02:00
|
|
|
base = config.basePath;
|
|
|
|
}
|
2023-03-19 14:32:23 +01:00
|
|
|
this.socket = io(`/${this.moduleName}`, {
|
|
|
|
path: `${base}socket.io`
|
2020-02-18 17:09:25 +01:00
|
|
|
});
|
2016-03-30 12:20:46 +02:00
|
|
|
|
2021-04-10 08:21:27 +02:00
|
|
|
let notificationCallback = function () {};
|
|
|
|
|
|
|
|
const onevent = this.socket.onevent;
|
|
|
|
this.socket.onevent = (packet) => {
|
|
|
|
const args = packet.data || [];
|
|
|
|
onevent.call(this.socket, packet); // original call
|
2016-03-30 17:22:30 +02:00
|
|
|
packet.data = ["*"].concat(args);
|
2021-04-10 08:21:27 +02:00
|
|
|
onevent.call(this.socket, packet); // additional call to catch-all
|
2016-03-30 17:22:30 +02:00
|
|
|
};
|
2016-03-30 12:20:46 +02:00
|
|
|
|
2016-03-30 17:22:30 +02:00
|
|
|
// register catch all.
|
2021-04-10 08:21:27 +02:00
|
|
|
this.socket.on("*", (notification, payload) => {
|
2016-04-05 14:35:11 -04:00
|
|
|
if (notification !== "*") {
|
2016-03-30 17:22:30 +02:00
|
|
|
notificationCallback(notification, payload);
|
|
|
|
}
|
2016-03-30 12:20:46 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// Public Methods
|
2021-04-10 08:21:27 +02:00
|
|
|
this.setNotificationCallback = (callback) => {
|
2016-03-30 12:20:46 +02:00
|
|
|
notificationCallback = callback;
|
|
|
|
};
|
|
|
|
|
2023-04-16 17:38:39 +02:00
|
|
|
this.sendNotification = (notification, payload = {}) => {
|
2021-04-10 08:21:27 +02:00
|
|
|
this.socket.emit(notification, payload);
|
2016-03-30 12:20:46 +02:00
|
|
|
};
|
2016-04-03 19:52:13 +02:00
|
|
|
};
|