Created
July 22, 2010 00:52
-
-
Save bcoe/485420 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* HackWars 2 | |
* | |
* The main JavaScript entry-point. | |
*/ | |
var HackWars = Class.extend({ | |
/** | |
* | |
*/ | |
init: function() { | |
var self = this; | |
// Attach an event to the login button. | |
$('#login_button').click(function() { | |
$.ajax({ | |
type: 'POST', | |
url: '/endpoint/login', | |
data: { | |
username: $('#username').val(), | |
password: $('#password').val() | |
}, | |
dataType: 'json', | |
success: function(response) { | |
if (response.error) { | |
alert(response.error); | |
} else { | |
self.access_token = response.access_token; | |
self.ip = response.ip; | |
self.init_comet(); | |
} | |
} | |
}); | |
}); | |
// Attach a message publisher to the chat. | |
$('#chat_button').click(function() { | |
var payload = { | |
message: $('#chat_message').val(), | |
access_token: self.access_token, | |
ip: self.ip | |
} | |
self.publish_message(payload); | |
}) | |
}, | |
/** | |
* | |
*/ | |
publish_message: function(payload) { | |
var self = this | |
$.ajax({ | |
type: 'POST', | |
url: '/endpoint/publish', | |
data: { | |
ip: self.ip, | |
access_token: self.access_token, | |
payload: JSON.stringify(payload), | |
}, | |
dataType: 'json', | |
success: function(response) { | |
console.log(response) | |
} | |
}); | |
}, | |
/** | |
* | |
*/ | |
init_comet: function() { | |
var self = this; | |
var socket = new Orbited.TCPSocket(); // Orbited.settings.port defaults to 8000 | |
socket.onerror = function() { | |
alert('error'); | |
} | |
socket.onopen = function() { | |
alert('open'); | |
socket.send(self.access_token); | |
}; | |
socket.onclose = function() { | |
alert('close'); | |
} | |
socket.onread = function(s) { | |
alert("Received string through comet -- " + s); | |
} | |
socket.open("localhost", 9000); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Messages are retrieved and dispatched from RabbitMQ using a JavaScript library I'm building.