diff options
Diffstat (limited to 'public')
-rw-r--r-- | public/index.html | 3 | ||||
-rw-r--r-- | public/js/rogue.js | 41 |
2 files changed, 21 insertions, 23 deletions
diff --git a/public/index.html b/public/index.html index bbac0a6..39f2afc 100644 --- a/public/index.html +++ b/public/index.html @@ -15,9 +15,6 @@ <canvas id="gameboard" class="center" width="512" height="416"></canvas> <button id="joingame" type="button" title="Join Game">Join Game</button> <button id="leavegame" type="button" title="Leave Game">Leave Game</button> - <button id="wsButton" type="button" title="Open WebSocket connection"> - Open WebSocket connection - </button> <button id="wsSendButton" type="button" title="Send WebSocket message"> Send WebSocket message </button> diff --git a/public/js/rogue.js b/public/js/rogue.js index a5a640a..c8ca8b9 100644 --- a/public/js/rogue.js +++ b/public/js/rogue.js @@ -120,30 +120,31 @@ function showMessage(message) { } joinGame.onclick = function() { - // TODO if we need this + if (!websocket) { + websocket = new WebSocket(`ws://localhost:8080`); + websocket.onerror = function() { + showMessage('Communication error'); + }; + websocket.onopen = function() { + showMessage('Game joined'); + }; + websocket.onclose = function() { + showMessage('Disconnected from game'); + websocket = null; + }; + websocket.onmessage = function(event) { + showMessage(event.data); + }; + } }; leaveGame.onclick = function() { - // TODO if we need this -}; - -wsButton.onclick = function() { - if (websocket) { - websocket.onerror = websocket.onopen = websocket.onclose = null; + if(websocket) { + showMessage('Disconnecting from game'); + websocket.onerror = websocket.onopen = websocket.onclose = websocket.onmessage = null; websocket.close(); - } - - websocket = new WebSocket(`ws://localhost:8080`); - websocket.onerror = function() { - showMessage('WebSocket error'); - }; - websocket.onopen = function() { - showMessage('WebSocket connection established'); - }; - websocket.onclose = function() { - showMessage('WebSocket connection closed'); - websocket = null; - }; + } + }; wsSendButton.onclick = function() { |