Guest User

Untitled

a guest
May 23rd, 2018
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. /**
  2. * HackWars 2
  3. *
  4. * The main JavaScript entry-point.
  5. */
  6. var HackWars = Class.extend({
  7. /**
  8. *
  9. */
  10. init: function() {
  11. var self = this;
  12.  
  13. // Attach an event to the login button.
  14. $('#login_button').click(function() {
  15. $.ajax({
  16. type: 'POST',
  17. url: '/endpoint/login',
  18. data: {
  19. username: $('#username').val(),
  20. password: $('#password').val()
  21. },
  22. dataType: 'json',
  23. success: function(response) {
  24. if (response.error) {
  25. alert(response.error);
  26. } else {
  27. self.access_token = response.access_token;
  28. self.ip = response.ip;
  29. self.init_comet();
  30. }
  31. }
  32. });
  33. });
  34.  
  35. // Attach a message publisher to the chat.
  36. $('#chat_button').click(function() {
  37. var payload = {
  38. message: $('#chat_message').val(),
  39. access_token: self.access_token,
  40. ip: self.ip
  41. }
  42. self.publish_message(payload);
  43. })
  44. },
  45.  
  46. /**
  47. *
  48. */
  49. publish_message: function(payload) {
  50. var self = this
  51.  
  52. $.ajax({
  53. type: 'POST',
  54. url: '/endpoint/publish',
  55. data: {
  56. ip: self.ip,
  57. access_token: self.access_token,
  58. payload: JSON.stringify(payload),
  59. },
  60. dataType: 'json',
  61. success: function(response) {
  62. console.log(response)
  63. }
  64. });
  65. },
  66.  
  67. /**
  68. *
  69. */
  70. init_comet: function() {
  71. var self = this;
  72. var socket = new Orbited.TCPSocket(); // Orbited.settings.port defaults to 8000
  73.  
  74. socket.onerror = function() {
  75. alert('error');
  76. }
  77.  
  78. socket.onopen = function() {
  79. alert('open');
  80. socket.send(self.access_token);
  81. };
  82.  
  83. socket.onclose = function() {
  84. alert('close');
  85. }
  86.  
  87. socket.onread = function(s) {
  88. alert("Received string through comet -- " + s);
  89. }
  90.  
  91. socket.open("localhost", 9000);
  92. }
  93. });
Add Comment
Please, Sign In to add comment