0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-14 12:20:24 +00:00

Remove jQuery, cleanup Websocket example a bit

This commit is contained in:
Loïc Hoguin 2020-05-23 11:00:17 +02:00
parent 8d5628c5f1
commit 3eec447960
No known key found for this signature in database
GPG key ID: 8A9DF795F6FED764
2 changed files with 101 additions and 106 deletions

View file

@ -1,29 +1,51 @@
<html> <!DOCTYPE html>
<html lang="en">
<head> <head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <meta charset="utf-8"/>
<title>Websocket client</title> <title>Websocket client</title>
<script src="/static/jquery.min.js"></script> </head>
<body>
<header>
<h1>Websocket client</h1>
<div id="status"></div>
</header>
<nav>
<div id="connecting">
<input type='text' id="server" value=""></input>
<button type="button" onclick="toggle_connection()">connection</button>
</div>
<div id="connected">
<input type='text' id="message" value=""></input>
<button type="button" onclick="sendTxt();">send</button>
</div>
</nav>
<main id="content">
<button id="clear" onclick="clearScreen()" >Clear text</button>
<div id="output"></div>
</main>
<script type="text/javascript"> <script type="text/javascript">
var websocket; var websocket;
$(document).ready(init); var server = document.getElementById("server");
var message = document.getElementById("message");
var connecting = document.getElementById("connecting");
var connected = document.getElementById("connected");
var content = document.getElementById("content");
var output = document.getElementById("output");
function init() { server.value = "ws://" + window.location.host + "/websocket";
$('#server').val("ws://" + window.location.host + "/websocket"); connected.style.display = "none";
if(!("WebSocket" in window)){ content.style.display = "none";
$('#status').append('<p><span style="color: red;">websockets are not supported </span></p>');
$("#navigation").hide();
} else {
$('#status').append('<p><span style="color: green;">websockets are supported </span></p>');
connect();
};
$("#connected").hide();
$("#content").hide();
};
function connect() function connect()
{ {
wsHost = $("#server").val() wsHost = server.value;
websocket = new WebSocket(wsHost); websocket = new WebSocket(wsHost);
showScreen('<b>Connecting to: ' + wsHost + '</b>'); showScreen('<b>Connecting to: ' + wsHost + '</b>');
websocket.onopen = function(evt) { onOpen(evt) }; websocket.onopen = function(evt) { onOpen(evt) };
@ -37,7 +59,7 @@
}; };
function toggle_connection(){ function toggle_connection(){
if(websocket.readyState == websocket.OPEN){ if (websocket && websocket.readyState == websocket.OPEN) {
disconnect(); disconnect();
} else { } else {
connect(); connect();
@ -46,9 +68,9 @@
function sendTxt() { function sendTxt() {
if (websocket.readyState == websocket.OPEN) { if (websocket.readyState == websocket.OPEN) {
txt = $("#send_txt").val(); var msg = message.value;
websocket.send(txt); websocket.send(msg);
showScreen('sending: ' + txt); showScreen('sending: ' + msg);
} else { } else {
showScreen('websocket is not connected'); showScreen('websocket is not connected');
}; };
@ -56,8 +78,9 @@
function onOpen(evt) { function onOpen(evt) {
showScreen('<span style="color: green;">CONNECTED </span>'); showScreen('<span style="color: green;">CONNECTED </span>');
$("#connected").fadeIn('slow'); connecting.style.display = "none";
$("#content").fadeIn('slow'); connected.style.display = "";
content.style.display = "";
}; };
function onClose(evt) { function onClose(evt) {
@ -72,42 +95,16 @@
showScreen('<span style="color: red;">ERROR: ' + evt.data + '</span>'); showScreen('<span style="color: red;">ERROR: ' + evt.data + '</span>');
}; };
function showScreen(txt) { function showScreen(html) {
$('#output').prepend('<p>' + txt + '</p>'); var el = document.createElement("p");
el.innerHTML = html;
output.insertBefore(el, output.firstChild);
}; };
function clearScreen() function clearScreen() {
{ output.innerHTML = "";
$('#output').html("");
}; };
</script> </script>
</head>
<body>
<div id="header">
<h1>Websocket client</h1>
<div id="status"></div>
</div>
<div id="navigation">
<p id="connecting">
<input type='text' id="server" value=""></input>
<button type="button" onclick="toggle_connection()">connection</button>
</p>
<div id="connected">
<p>
<input type='text' id="send_txt" value=></input>
<button type="button" onclick="sendTxt();">send</button>
</p>
</div>
<div id="content">
<button id="clear" onclick="clearScreen()" >Clear text</button>
<div id="output"></div>
</div>
</div>
</body> </body>
</html> </html>

File diff suppressed because one or more lines are too long