diff options
| author | Matt Kohls <mattkohls13@gmail.com> | 2019-12-04 20:07:23 -0500 | 
|---|---|---|
| committer | Matt Kohls <mattkohls13@gmail.com> | 2019-12-04 20:07:23 -0500 | 
| commit | 418b89ccebdddd5bac995393a0d9945debaf0d7f (patch) | |
| tree | f538a4c115d5ee4716ca0d59ba6c4f8589c2ac36 /public | |
| parent | 484d7613dbc7d8064e524e8c751d92f06e481616 (diff) | |
| download | rogue.js-418b89ccebdddd5bac995393a0d9945debaf0d7f.tar.gz rogue.js-418b89ccebdddd5bac995393a0d9945debaf0d7f.tar.bz2 rogue.js-418b89ccebdddd5bac995393a0d9945debaf0d7f.zip | |
More bits, client now gets response from server
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() { | 
