Skip to content

Commit

Permalink
Use es6 notation in socketclient
Browse files Browse the repository at this point in the history
  • Loading branch information
rejas committed Apr 18, 2021
1 parent d736dd9 commit 7bc7102
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions js/socketclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,48 @@
* By Michael Teeuw https://michaelteeuw.nl
* MIT Licensed.
*/
var MMSocket = function (moduleName) {
var self = this;

const MMSocket = function (moduleName) {
if (typeof moduleName !== "string") {
throw new Error("Please set the module name for the MMSocket.");
}

self.moduleName = moduleName;
this.moduleName = moduleName;

// Private Methods
var base = "/";
let base = "/";
if (typeof config !== "undefined" && typeof config.basePath !== "undefined") {
base = config.basePath;
}
self.socket = io("/" + self.moduleName, {
this.socket = io("/" + this.moduleName, {
path: base + "socket.io"
});
var notificationCallback = function () {};

var onevent = self.socket.onevent;
self.socket.onevent = function (packet) {
var args = packet.data || [];
onevent.call(this, packet); // original call
let notificationCallback = function () {};

const onevent = this.socket.onevent;
this.socket.onevent = (packet) => {
const args = packet.data || [];
onevent.call(this.socket, packet); // original call
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.
self.socket.on("*", function (notification, payload) {
this.socket.on("*", (notification, payload) => {
if (notification !== "*") {
notificationCallback(notification, payload);
}
});

// Public Methods
this.setNotificationCallback = function (callback) {
this.setNotificationCallback = (callback) => {
notificationCallback = callback;
};

this.sendNotification = function (notification, payload) {
this.sendNotification = (notification, payload) => {
if (typeof payload === "undefined") {
payload = {};
}
self.socket.emit(notification, payload);
this.socket.emit(notification, payload);
};
};

0 comments on commit 7bc7102

Please sign in to comment.