Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.51 KB | None | 0 0
  1. var webSocket;
  2. var messages = document.getElementById("messages");
  3. var buttons = document.getElementById("buttons");
  4. var invisable = document.getElementById("invisable-container");
  5.  
  6.  
  7.  
  8.  
  9. function openSocket(){
  10. // Ensures only one connection is open at a time
  11. if(webSocket !== undefined && webSocket.readyState !== WebSocket.CLOSED){
  12. writeResponse("WebSocket is already opened.");
  13. return;
  14. }
  15. // Create a new instance of the websocket
  16. var host = location.origin.replace(/^http/, 'ws')
  17.  
  18. // Create a new instance of the websocket
  19. webSocket = new WebSocket(host);
  20.  
  21. /**
  22. * Binds functions to the listeners for the websocket.
  23. */
  24. webSocket.onopen = function(event){
  25. // For reasons I can't determine, onopen gets called twice
  26. // and the first time event.data is undefined.
  27. // Leave a comment if you know the answer.
  28. if(event.data === undefined)
  29. return;
  30.  
  31. //writeResponse(event.data);
  32. };
  33.  
  34. webSocket.onmessage = function(event){
  35. //writeResponse(event.data);
  36. checkText(event.data);
  37. };
  38.  
  39. webSocket.onclose = function(event){
  40. //writeResponse("Connection closed");
  41. };
  42. }
  43.  
  44. /**
  45. * Sends the value of the text input to the server
  46. */
  47. function send(arg1){
  48. var text = arg1;
  49. console.log("text:"+text);
  50. webSocket.send(text);
  51. }
  52.  
  53. function closeSocket(){
  54. webSocket.close();
  55. }
  56.  
  57. //function writeResponse(text){
  58. // messages.innerHTML += "<br/>" + text;
  59. //}
  60. function checkText(text)
  61. {
  62. //console.log(text);
  63. if (text === "game:confirm")
  64. {
  65.  
  66. }
  67. else if (text === "game:askrole")
  68. {
  69. while (buttons.firstChild) {
  70. buttons.removeChild(buttons.firstChild);
  71. }
  72. var button = document.createElement("button");
  73. var button2 = document.createElement("button");
  74. button.textContent = "THIS IS A BUTTON";
  75. button.setAttribute( "onClick", "javascript: send('game:host');" );
  76. button2.setAttribute( "onClick", "javascript: send('game:client');" );
  77. button2.textContent = "THIS IS A BUTTON TOO";
  78. button.className += " joinButton btn btn-success";
  79. button2.className += " joinButton btn btn-success";
  80. buttons.appendChild(button);
  81. buttons.appendChild(button2);
  82.  
  83. }
  84. else if (text === "game:loadboard")
  85. {
  86. //LOAD GAME BOARD IN
  87.  
  88.  
  89. /*while (buttons.firstChild) {
  90. buttons.removeChild(buttons.firstChild);
  91. }
  92. var pg = document.createElement("p");
  93. pg.textContent = "WAITING FOR OTHER PLAYERS TO JOIN....";
  94. pg.className += " par";
  95. buttons.appendChild(pg); */
  96. }
  97. else if (text.substring(0, 22) === 'game:clientsconnected-'){
  98. while (buttons.firstChild) {
  99. buttons.removeChild(buttons.firstChild);
  100. }
  101. var pg = document.createElement("p");
  102. if(parseInt(text.substring(22))===1)
  103. pg.textContent = "Currently, there is " + text.substring(22) + " client connected.";
  104. else
  105. pg.textContent = "Currently, there are " + text.substring(22) + " clients connected.";
  106. if(parseInt(text.substring(22))>2){
  107. send("game:checkhost");
  108. }
  109. pg.className += " par";
  110. buttons.appendChild(pg);
  111. }
  112.  
  113. else if(text === "game:hashost")
  114. {
  115. var button = document.createElement("button");
  116. button.textContent = "Start Game";
  117. buttons.appendChild(button);
  118. }
  119. else if (text.substring(0,12) === "game:showclue")
  120. {
  121. document.getElementById("invis-container").className = "";
  122. var pg = document.createElement("p");
  123. pg.textContent = text.substring(13);
  124. pg.className += "par";
  125. buttons.appendChild(pg);
  126. }
  127. }
  128. function startGame(){
  129.  
  130. }
  131.  
  132. function buzz(arg1)
  133. {
  134. console.log("text:"+ arg1);
  135. webSocket.send("game:buzz");
  136. webSocket.send(arg1+"");
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement