Use es6 notation in socketclient

This commit is contained in:
rejas 2021-04-10 08:21:27 +02:00
parent d736dd92be
commit 7bc71029de

View File

@ -6,49 +6,48 @@
* By Michael Teeuw https://michaelteeuw.nl * By Michael Teeuw https://michaelteeuw.nl
* MIT Licensed. * MIT Licensed.
*/ */
var MMSocket = function (moduleName) { const MMSocket = function (moduleName) {
var self = this;
if (typeof moduleName !== "string") { if (typeof moduleName !== "string") {
throw new Error("Please set the module name for the MMSocket."); throw new Error("Please set the module name for the MMSocket.");
} }
self.moduleName = moduleName; this.moduleName = moduleName;
// Private Methods // Private Methods
var base = "/"; let base = "/";
if (typeof config !== "undefined" && typeof config.basePath !== "undefined") { if (typeof config !== "undefined" && typeof config.basePath !== "undefined") {
base = config.basePath; base = config.basePath;
} }
self.socket = io("/" + self.moduleName, { this.socket = io("/" + this.moduleName, {
path: base + "socket.io" path: base + "socket.io"
}); });
var notificationCallback = function () {};
var onevent = self.socket.onevent; let notificationCallback = function () {};
self.socket.onevent = function (packet) {
var args = packet.data || []; const onevent = this.socket.onevent;
onevent.call(this, packet); // original call this.socket.onevent = (packet) => {
const args = packet.data || [];
onevent.call(this.socket, packet); // original call
packet.data = ["*"].concat(args); packet.data = ["*"].concat(args);
onevent.call(this, packet); // additional call to catch-all onevent.call(this.socket, packet); // additional call to catch-all
}; };
// register catch all. // register catch all.
self.socket.on("*", function (notification, payload) { this.socket.on("*", (notification, payload) => {
if (notification !== "*") { if (notification !== "*") {
notificationCallback(notification, payload); notificationCallback(notification, payload);
} }
}); });
// Public Methods // Public Methods
this.setNotificationCallback = function (callback) { this.setNotificationCallback = (callback) => {
notificationCallback = callback; notificationCallback = callback;
}; };
this.sendNotification = function (notification, payload) { this.sendNotification = (notification, payload) => {
if (typeof payload === "undefined") { if (typeof payload === "undefined") {
payload = {}; payload = {};
} }
self.socket.emit(notification, payload); this.socket.emit(notification, payload);
}; };
}; };