Created
April 1, 2011 19:21
-
-
Save c-spencer/898691 to your computer and use it in GitHub Desktop.
Stopgap channel implementation
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
var Channel = (function () { | |
function Channel(name) { | |
this.init(name); | |
} | |
Channel.prototype = new Object(); | |
// Initialise | |
Channel.prototype.init = function (name) { | |
this.name = name; | |
this.clients = {}; | |
this.connectedFuncs = []; | |
this.disconnectedFuncs = []; | |
} | |
// Utility function to iterate over clients in the channel. | |
Channel.prototype.iterate = function (callback) { | |
_(this.clients).each(function (client) { | |
callback.call(client); | |
}); | |
} | |
// Add a callback on client connection to the channel. | |
Channel.prototype.connected = function (callback) { | |
this.connectedFuncs.push(callback); | |
} | |
Channel.prototype.runConnected = function (client) { | |
_(this.connectedFuncs).each(function (callback) { | |
callback.call(client); | |
}); | |
} | |
// Add a callback on client disconnection from the channel. | |
Channel.prototype.disconnected = function (callback) { | |
this.disconnectedFuncs.push(callback); | |
} | |
Channel.prototype.runDisconnected = function (client) { | |
_(this.disconnectedFuncs).each(function (callback) { | |
callback.call(client); | |
}); | |
} | |
// Join a client to the channel and trigger connected callbacks. | |
Channel.prototype.join = function (client) { | |
this.clients[client.user.clientId] = client; | |
this.runConnected(client); | |
} | |
// Remove a client from the channel and trigger disconnected callbacks. | |
Channel.prototype.leave = function (client) { | |
if (this.clients[client.user.clientId]) { | |
delete this.clients[client.user.clientId]; | |
this.runDisconnected(client); | |
} | |
} | |
// Takes a function name and a callback as a function to call for each | |
// client in the channel. | |
Channel.prototype.def = function (funcname, callback) { | |
if (this[funcname] !== undefined) { | |
console.log("Ignored def for " + funcname + " as it was already defined."); | |
} | |
this[funcname] = function () { | |
var args = arguments; | |
_(this.clients).each(function (client) { | |
callback.apply(client, args); | |
}); | |
} | |
} | |
// Takes 1 or more arguments as function names to proxy forward to clients | |
// that are in the channel. | |
Channel.prototype.proxy = function () { | |
for (var i = 0; i < arguments.length; i++) { | |
var funcname = arguments[i]; | |
if (this[funcname] !== undefined) { | |
console.log("Ignored def for " + funcname + " as it was already defined."); | |
} | |
this[funcname] = function () { | |
var args = arguments; | |
_(this.clients).each(function (client) { | |
client.now[funcname].apply(client, args); | |
}); | |
} | |
} | |
} | |
return Channel; | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment