Skip to content

Instantly share code, notes, and snippets.

@bcoe
Created July 22, 2010 00:52
Show Gist options
  • Save bcoe/485420 to your computer and use it in GitHub Desktop.
Save bcoe/485420 to your computer and use it in GitHub Desktop.
/**
* 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);
}
});
@bcoe
Copy link
Author

bcoe commented Jul 22, 2010

Messages are retrieved and dispatched from RabbitMQ using a JavaScript library I'm building.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment