Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * 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);
- }
- });
Add Comment
Please, Sign In to add comment