Skip to content

Commit

Permalink
Added a callback hook for sending messages so you can parse the respo…
Browse files Browse the repository at this point in the history
…nse.
  • Loading branch information
Andrew Hao committed Sep 24, 2011
1 parent aad02a0 commit 522a0b7
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions public/javascripts/mmtss.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,32 @@ function deleteClipsInGroup() {
*/
function Command(sock) {
this.socket = sock;
this.callbacks = {};
}

Command.prototype.init = function() {
this.socket.on('message', function (data) {
console.log('Server message: '+data);
});
var cmdObj = this;
this.socket.on('osc_response', function(data) {
var address = data._address;
var args = data._args;
//console.log(data);

// Fire any associated callbacks.
if (cmdObj.callbacks[address] !== undefined) {
var cbList = cmdObj.callbacks[address];
_.each(cbList, function(cb) {
cb.call(this, data);
});
// Clear the callback entry.
cmdObj.callbacks[address] = [];
}

// Emit events based on OSC messages.
$(document).trigger(EVENT_MAP[address], args);
if (EVENT_MAP[address] !== undefined) {
$(document).trigger(EVENT_MAP[address], args);
}
});
return this;
}
Expand All @@ -117,15 +131,30 @@ Command.prototype.addListener = function(ev, cb) {
* sendMessage('/live/play')
* sendMessage('/live/tempo', 50)
* sendMessage('/live/clip/info', [0, 1])
*
* @param {String} path The command path
* @param {Array} argArr An array of arguments to pass to the command
* @param {Function} cb Callback method to execute when the
* server response returns.
*/
Command.prototype.send = function(path, argArr) {
Command.prototype.send = function(path, argArr, cb) {
if (!argArr instanceof Array) {
argArr = [argArr];
}
var msgObj = {
address: path,
args: argArr
}

if (cb !== undefined) {
// Add callback to method stack.
if (this.callbacks[path] === undefined) {
this.callbacks[path] = [cb];
} else {
this.callbacks[path].push(cb);
}
}

this.socket.emit('osc_command', msgObj);
}

Expand Down

0 comments on commit 522a0b7

Please sign in to comment.