diff options
author | Matt Kohls <mattkohls13@gmail.com> | 2019-11-16 00:16:05 -0500 |
---|---|---|
committer | Matt Kohls <mattkohls13@gmail.com> | 2019-11-16 00:16:05 -0500 |
commit | 28cf66631f269f9970ee09aaba2d5ef03d49cc25 (patch) | |
tree | 74c6b64bea74611387b59c7f1be200f435e2959c /public/js | |
parent | 6f1e29c7989781a72a168b554b60ad7e2af4482d (diff) | |
download | rogue.js-28cf66631f269f9970ee09aaba2d5ef03d49cc25.tar.gz rogue.js-28cf66631f269f9970ee09aaba2d5ef03d49cc25.tar.bz2 rogue.js-28cf66631f269f9970ee09aaba2d5ef03d49cc25.zip |
Adding in start of websocket stuff
Diffstat (limited to 'public/js')
-rw-r--r-- | public/js/rogue.js | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/public/js/rogue.js b/public/js/rogue.js index e3c046f..1580593 100644 --- a/public/js/rogue.js +++ b/public/js/rogue.js @@ -8,6 +8,7 @@ */ var gamemap = {}; +var player = {}; function Sprite(x, y, width, height) { this.x = x; @@ -101,6 +102,58 @@ var space = new Sprite(1, 59, 8, 16); /** Client Logic **/ +const messages = document.querySelector('#messages'); +const joinGame = document.querySelector('#joingame'); +const leaveGame = document.querySelector('#leavegame'); +const wsButton = document.querySelector('#wsButton'); +const wsSendButton = document.querySelector('#wsSendButton'); +let websocket; + +/** + * Show messages from the server + */ +function showMessage(message) { + messages.textContent += `\n${message}`; + messages.scrollTop = messages.scrollHeight; +} + +joinGame.onclick = function() { + // TODO if we need this +}; + +leaveGame.onclick = function() { + // TODO if we need this +}; + +wsButton.onclick = function() { + if (websocket) { + websocket.onerror = websocket.onopen = websocket.onclose = 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() { + if (!websocket) { + showMessage('No WebSocket connection'); + return; + } + + websocket.send('Hello World!'); + showMessage('Sent "Hello World!"'); +}; + const canvas = document.getElementById('gameboard'); const context = canvas.getContext('2d'); const spritemap = new Image(320, 1264); |