diff --git a/chat.html b/chat.html index 4d60578..4dca2d3 100644 --- a/chat.html +++ b/chat.html @@ -3,14 +3,21 @@ WebSocket Chat exercise - +
+ + + diff --git a/chat.js b/chat.js new file mode 100644 index 0000000..e69d407 --- /dev/null +++ b/chat.js @@ -0,0 +1,47 @@ +$(document).ready(function() { + var socket; + var logElement = document.getElementById('chatlog'); + var sendButton = document.getElementById('sendButton'); + var closeButton = document.getElementById('closeButton'); + + function logMessage(message) { + logElement.value += message + '\n'; + } + + function connect() { + socket = new WebSocket("ws://webex.sdi.hevs.ch/chat"); + socket.onopen = function() { + logMessage("Connected."); + sendButton.removeAttribute("disabled"); + closeButton.textContent = "End"; + }; + socket.onmessage = function(msg) { + logMessage(msg.data); + }; + socket.onclose = function() { + logMessage("Disconnected."); + sendButton.setAttribute("disabled", "true"); + closeButton.textContent = "Reconnect"; + }; + socket.onerror = function() { + logMessage("Error."); + }; + + } + + + connect(); + + document.getElementById("sendButton").onclick = function() { + var msg = document.getElementById("msg").value; + socket.send(msg); + document.getElementById("msg").value = ""; + }; + document.getElementById("closeButton").onclick = function() { + if (socket.readyState === WebSocket.OPEN) { + socket.close(); + } else { + connect(); + } + }; +}); diff --git a/tempbat.html b/tempbat.html index 7b75791..ad4ffc3 100644 --- a/tempbat.html +++ b/tempbat.html @@ -3,16 +3,11 @@ Websocket Server EX - - @@ -22,5 +17,9 @@ + + + + diff --git a/tempbat.js b/tempbat.js new file mode 100644 index 0000000..87cd2d7 --- /dev/null +++ b/tempbat.js @@ -0,0 +1,53 @@ +$(document).ready(function() { + var socket; + var logElement = document.getElementById('log'); + var tempButton = document.getElementById('temp'); + var batButton = document.getElementById('bat'); + var disconButton = document.getElementById('discon'); + + function logMessage(message) { + logElement.value += message + '\n'; + } + + function connect() { + socket = new WebSocket("ws://webex.sdi.hevs.ch/temp"); + + socket.onopen = function() { + logMessage('Connection open'); + tempButton.removeAttribute('disabled'); + batButton.removeAttribute('disabled'); + disconButton.textContent = 'Disconnect'; + }; + socket.onclose = function() { + logMessage('Connection close'); + tempButton.setAttribute('disabled', 'true'); + batButton.setAttribute('disabled', 'true'); + disconButton.textContent = 'Reconnect'; + }; + + socket.onerror = function(error) { + logMessage('Error: ' + error.message); + }; + socket.onmessage = function(msg) { + logMessage(msg.data); + }; + } + + connect(); + + document.getElementById('temp').addEventListener('click', function() { + socket.send("temp"); + }); + + document.getElementById('bat').addEventListener('click', function() { + socket.send("bat"); + }); + + document.getElementById('discon').addEventListener('click', function() { + if (socket.readyState === WebSocket.OPEN) { + socket.close(); + } else { + connect(); + } + }); +}); \ No newline at end of file