index.html 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <!DOCTYPE html>
  2. <meta charset="utf-8" />
  3. <html>
  4. <head>
  5. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
  6. <script language="javascript" type="text/javascript">
  7. $(function() {
  8. var connection = null;
  9. $('#join').click(function() {
  10. var game_id = $('#gameId').val();
  11. var nick = $('#nick').val();
  12. var msg = {
  13. join: {
  14. game_id: game_id,
  15. nick: nick,
  16. }
  17. };
  18. connect(msg);
  19. });
  20. $('#create').click(function() {
  21. var game_id = $('#gameId').val();
  22. var nick = $('#nick').val();
  23. var msg = {
  24. create_game: {
  25. game_id: game_id,
  26. nick: nick,
  27. }
  28. };
  29. connect(msg);
  30. });
  31. $('#submit').click(function() {
  32. var word = $('#word').val();
  33. var msg = {
  34. submit_word: {
  35. word: word
  36. }
  37. };
  38. connect(msg);
  39. });
  40. function connect(initial_message) {
  41. if (connection != null) {
  42. disconnect();
  43. }
  44. var uri = 'ws://' + window.location.host + '/ws/';
  45. connection = new WebSocket(uri);
  46. connection.onopen = function() {
  47. connection.send(JSON.stringify(initial_message));
  48. };
  49. connection.onmessage = function(e) {
  50. onReceive(e);
  51. };
  52. connection.onclose = function() {
  53. };
  54. }
  55. function disconnect() {
  56. connection.close();
  57. connection = null;
  58. }
  59. function onReceive(msg) {
  60. var obj = jQuery.parseJSON(msg.data);
  61. if (obj.game_not_found != null) {
  62. $('#status').html("Game \"" + obj.game_not_found.game_id + "\" not found");
  63. }
  64. else if (obj.game_already_exists != null) {
  65. $('#status').html("Game \"" + obj.game_already_exists.game_id + "\" already exists");
  66. }
  67. else if (obj.game_state != null) {
  68. var gs = obj.game_state;
  69. if (gs.state_data.creating != null) {
  70. var players = gs.players;
  71. playerlist = "";
  72. for (var i = 0; i < players.length; i++) {
  73. playerlist += players[i].nick + "(" + players[i].points + ")";
  74. if (i + 1 < players.length)
  75. playerlist += ", ";
  76. }
  77. $('#status').html("Players: " + playerlist);
  78. $('#createform').toggle();
  79. }
  80. }
  81. else {
  82. $('#status').html("Unknown Message recieved");
  83. }
  84. }
  85. });
  86. </script>
  87. </head>
  88. <body>
  89. <form id="loginform">
  90. <label for="gameId">Game ID:</label>
  91. <input id="gameId" type="text" /><br>
  92. <label for="nick">Nickname:</label>
  93. <input id="nick" type="text" /><br>
  94. <input id="join" type="button" value="Join" />
  95. <input id="create" type="button" value="Create Game" />
  96. </form>
  97. <form id="createform" style="display:none">
  98. <label>Create a word with the following letters</label>
  99. <label for="word">Word:</label>
  100. <input id="word" type="text" /><br>
  101. <input id="submit" type="button" value="Submit Word" />
  102. </form>
  103. <div id="status">
  104. </div>
  105. </body>
  106. </html>