Skip to content

Instantly share code, notes, and snippets.

@jedahan
Created December 1, 2011 21:41
Show Gist options
  • Select an option

  • Save jedahan/1420093 to your computer and use it in GitHub Desktop.

Select an option

Save jedahan/1420093 to your computer and use it in GitHub Desktop.
<head>
<title> Tutorial Application </title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="http://cdn.buglabs.net/swarm/swarm-v0.3.1.js"></script>
<script src="./tutorial_application.js"></script>
</head>
<body>
latest communication: <span id='communication'></span>
<br>
players: <ul id='players'></ul>
</body>
var handlePresence, handleConnect, handleError, handleMessage;
handleConnect = function() {
console.log('connected');
};
handleError = function(response) {
console.log('Error:');
console.log(response);
};
handlePresence = function(response) {
presence = JSON.parse(response).presence;
// Filter out swarm presence messages, which do not contain the 'type' field
if(presence.type) {
who = presence.from.resource;
if (presence.type === "available") {
$('#players').append("<li id='" + who + "'>" + who + '</li>');
} else {
$('#' + who).remove();
}
}
};
// We expect the payload to be a json object of the format:
// { "communication": "string to display" }
handleMessage = function(response) {
payload = JSON.parse(response).message.payload;
$('#message').html(payload.communication);
};
// Join the swarm
// All callbacks must be defined before calling SWARM.connect for it to work
// The apikey, swarm and resource was setup beforehand. Replace with your own!
SWARM.connect({ apikey: '1572691d88f9a0da2c0d0665dfffca59a54090d8'
, swarms: '9771cf59c557853937da112f0c2d101b57e22e78'
, resource: 'aca699198534fd9203de4b5a5c8f19866efbfdf7'
, onmessage: handleMessage
, onpresence: handlePresence
, onconnect: handleConnect
, onerror: handleError
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment