From 418b89ccebdddd5bac995393a0d9945debaf0d7f Mon Sep 17 00:00:00 2001 From: Matt Kohls Date: Wed, 4 Dec 2019 20:07:23 -0500 Subject: More bits, client now gets response from server --- public/index.html | 3 --- public/js/rogue.js | 41 +++++++++++++++++++++-------------------- 2 files changed, 21 insertions(+), 23 deletions(-) (limited to 'public') 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 @@ - 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() { -- cgit v1.2.3