-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
44 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,44 @@ | ||
var MMSocket = function(moduleName) { | ||
var self = this; | ||
|
||
if (typeof moduleName !== "string") { | ||
throw new Error("Please set the module name for the MMSocket."); | ||
} | ||
|
||
self.moduleName = moduleName; | ||
|
||
// Private Methods | ||
self.socket = io("/" + self.moduleName, { | ||
path: window.location.pathname + "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 | ||
packet.data = ["*"].concat(args); | ||
onevent.call(this, packet); // additional call to catch-all | ||
}; | ||
|
||
// register catch all. | ||
self.socket.on("*", function(notification, payload) { | ||
if (notification !== "*") { | ||
notificationCallback(notification, payload); | ||
} | ||
}); | ||
|
||
// Public Methods | ||
this.setNotificationCallback = function(callback) { | ||
notificationCallback = callback; | ||
}; | ||
|
||
this.sendNotification = function(notification, payload) { | ||
if (typeof payload === "undefined") { | ||
payload = {}; | ||
} | ||
self.socket.emit(notification, payload); | ||
}; | ||
}; | ||
var MMSocket = function (moduleName) { | ||
var self = this | ||
|
||
if (typeof moduleName !== 'string') { | ||
throw new Error('Please set the module name for the MMSocket.') | ||
} | ||
|
||
self.moduleName = moduleName | ||
|
||
// Private Methods | ||
// self.socket = io("/" + self.moduleName, { | ||
// path: window.location.pathname + "socket.io" | ||
// }); | ||
self.socket = io('/' + self.moduleName) | ||
|
||
var notificationCallback = function () {} | ||
|
||
var onevent = self.socket.onevent | ||
self.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. | ||
self.socket.on('*', function (notification, payload) { | ||
if (notification !== '*') { | ||
notificationCallback(notification, payload) | ||
} | ||
}) | ||
|
||
// Public Methods | ||
this.setNotificationCallback = function (callback) { | ||
notificationCallback = callback | ||
} | ||
|
||
this.sendNotification = function (notification, payload) { | ||
if (typeof payload === 'undefined') { | ||
payload = {} | ||
} | ||
self.socket.emit(notification, payload) | ||
} | ||
} |