Created
February 2, 2012 03:07
-
-
Save thedaniel/1721166 to your computer and use it in GitHub Desktop.
capability client.js
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
$(function(){ | |
var Spire = require('./spire.io.js'); | |
var Subscription = require('./spire/api/subscription'); | |
var spire = new Spire(); | |
$.get('/discover', function(data){ | |
var spireSubscription; | |
var spireChannel; | |
spire.subscriptionFromUrlAndCapability(data.subscription, function(err, subscription) { | |
if (err){ | |
console.error(err); | |
} | |
spireSubscription = subscription; | |
spireSubscription.addListener('message', function(message){ | |
$('.emesrever').val(message.content); | |
}); | |
spireSubscription.startListening(); | |
}); | |
spire.channelFromUrlAndCapability(data.channel, function(err, channel){ | |
spireChannel = channel; | |
}); | |
var setUpListener = function(){ | |
if (spireChannel && spireSubscription) { | |
console.log('Setting up listeners...'); | |
$('.loading').hide(); | |
$('.content').show(); | |
$('.submit.button').click(function(e){ | |
e.stopPropagation(); | |
e.preventDefault(); | |
spireChannel.publish($('.reverseme').val(), function(err, msg){ | |
console.log('Sent message: ', msg); | |
}); | |
}); | |
} else { | |
console.log('waiting for discovery...'); | |
setTimeout(setUpListener, 500); | |
} | |
}; | |
setUpListener(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment