diff options
Diffstat (limited to 'src/main/resources/static')
-rw-r--r-- | src/main/resources/static/app.js | 78 | ||||
-rw-r--r-- | src/main/resources/static/index.html | 36 |
2 files changed, 114 insertions, 0 deletions
diff --git a/src/main/resources/static/app.js b/src/main/resources/static/app.js new file mode 100644 index 00000000..5a3bc38e --- /dev/null +++ b/src/main/resources/static/app.js @@ -0,0 +1,78 @@ +var stompClient = null; + +function setConnected(connected) { + $("#connect").prop("disabled", connected); + $("#disconnect").prop("disabled", !connected); + if (connected) { + $("#game-list").show(); + } else { + $("#game-list").hide(); + } + $("#greetings").html(""); +} + +function connect() { + var socket = new SockJS('/seven-wonders-websocket'); + stompClient = Stomp.over(socket); + stompClient.connect({}, function (frame) { + setConnected(true); + console.log('Connected: ' + frame); + + stompClient.subscribe('/broadcast/games', function (gameId) { + console.log("Received new game: " + gameId); + addNewGame(gameId.body); + }); + + stompClient.subscribe('/broadcast/players', function (player) { + console.log("Received new player: " + player); + addNewPlayer(JSON.parse(player.body)); + }); + }); +} + +function disconnect() { + if (stompClient != null) { + stompClient.disconnect(); + } + setConnected(false); + console.log("Disconnected"); +} + +function sendCreateGame() { + stompClient.send("/app/lobby/create-game", {}, ""); +} + +function sendJoinGame(gameId) { + stompClient.send("/app/lobby/join-game", {}, + JSON.stringify({'gameId': gameId, 'playerName': $("#player-name-field").val()})); +} + +function addNewGame(gameId) { + console.log(gameId); + $("#game-list-content").append('<tr><td>' + gameId + '</td><td><button id="join-' + gameId + '" type="submit">Join</button></td></tr>'); + $("#join-" + gameId).click(function () { + sendJoinGame(gameId); + }); +} + +function addNewPlayer(player) { + console.log(player); +} + +$(function () { + $("form").on('submit', function (e) { + e.preventDefault(); + }); + $("#connect").click(function () { + connect(); + }); + $("#disconnect").click(function () { + disconnect(); + }); + $("#create-game").click(function () { + sendCreateGame(); + }); + $("#join-game").click(function () { + sendJoinGame(); + }); +});
\ No newline at end of file diff --git a/src/main/resources/static/index.html b/src/main/resources/static/index.html index e238079b..1b959d56 100644 --- a/src/main/resources/static/index.html +++ b/src/main/resources/static/index.html @@ -7,13 +7,49 @@ <script src="/webjars/jquery/jquery.min.js"></script> <script src="/webjars/sockjs-client/sockjs.min.js"></script> <script src="/webjars/stomp-websocket/stomp.min.js"></script> + <script src="app.js"></script> </head> <body> <noscript> <h2 style="color: #ff0000">Seems your browser doesn't support Javascript! Websocket relies on Javascript being enabled. Please enable Javascript and reload this page!</h2> </noscript> + <h1>Seven Wonders</h1> + <p>This is a stub index page for the project, for the sake of vertical completeness. We will soon get to work on it!</p> + +<h2>Connection</h2> + +<form class="form-inline"> + <div class="form-group"> + <label for="connect">WebSocket connection:</label> + <button id="connect" class="btn btn-default" type="submit">Connect</button> + <button id="disconnect" class="btn btn-default" type="submit" disabled="disabled">Disconnect</button> + </div> +</form> + +<h2>Games</h2> + +<form class="form-inline"> + <div class="form-group"> + <label for="player-name-field">Player name</label> + <input id="player-name-field"> + </div> +</form> + +<table id="game-list" class="table table-striped"> + <thead> + <tr> + <th>Id</th> + <th></th> + </tr> + </thead> + <tbody id="game-list-content"> + </tbody> +</table> + +<button id="create-game" class="btn btn-default" type="submit">Create Game</button> + </body> </html>
\ No newline at end of file |