1
0
mirror of https://github.com/actix/examples synced 2025-02-13 05:52:20 +01:00

js without jQuery (#342)

Co-authored-by: Rob Ede <robjtede@icloud.com>
This commit is contained in:
Taisuke Fukuno 2020-11-01 00:55:23 +09:00 committed by GitHub
parent 7148fd3d0e
commit 542e6dbcdd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,90 +1,112 @@
<!DOCTYPE html> <!DOCTYPE html>
<meta charset="utf-8" />
<html> <html>
<head> <head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"> <title>Chat!</title>
</script>
<script language="javascript" type="text/javascript"> <meta charset="utf-8" />
$(function() {
var conn = null; <script>
function log(msg) { 'use strict'
var control = $('#log');
control.html(control.html() + msg + '<br/>'); window.onload = () => {
control.scrollTop(control.scrollTop() + 1000); let conn = null
const log = (msg) => {
div_log.innerHTML += msg + '<br>'
div_log.scroll(0, div_log.scrollTop + 1000)
} }
function connect() {
disconnect(); const connect = () => {
var wsUri = (window.location.protocol=='https:'&&'wss://'||'ws://')+window.location.host + '/ws/'; disconnect()
conn = new WebSocket(wsUri);
log('Connecting...'); const wsUri =
(window.location.protocol === 'https:' ? 'wss://' : 'ws://') +
window.location.host +
'/ws/'
conn = new WebSocket(wsUri)
log('Connecting...')
conn.onopen = function () { conn.onopen = function () {
log('Connected.'); log('Connected.')
update_ui(); update_ui()
}; }
conn.onmessage = function (e) { conn.onmessage = function (e) {
log('Received: ' + e.data); log('Received: ' + e.data)
}; }
conn.onclose = function () { conn.onclose = function () {
log('Disconnected.'); log('Disconnected.')
conn = null; conn = null
update_ui();
}; update_ui()
}
function disconnect() {
if (conn != null) {
log('Disconnecting...');
conn.close();
conn = null;
update_ui();
} }
} }
function update_ui() {
var msg = ''; const disconnect = () => {
if (conn == null) { if (conn) {
$('#status').text('disconnected'); log('Disconnecting...')
$('#connect').html('Connect'); conn.close()
conn = null
update_ui()
}
}
const update_ui = () => {
if (!conn) {
span_status.textContent = 'disconnected'
btn_connect.textContent = 'Connect'
} else { } else {
$('#status').text('connected (' + conn.protocol + ')'); span_status.textContent = `connected (${conn.protocol})`
$('#connect').html('Disconnect'); btn_connect.textContent = 'Disconnect'
} }
} }
$('#connect').click(function() {
if (conn == null) { btn_connect.onclick = () => {
connect(); if (!conn) {
connect()
} else { } else {
disconnect(); disconnect()
}
update_ui()
}
btn_send.onclick = () => {
if (!conn) return
const text = input_text.value
log('Sending: ' + text)
conn.send(text)
input_text.value = ''
input_text.focus()
}
input_text.onkeyup = (e) => {
if (e.key === 'Enter') {
btn_send.click()
}
} }
update_ui();
return false;
});
$('#send').click(function() {
var text = $('#text').val();
log('Sending: ' + text);
conn.send(text);
$('#text').val('').focus();
return false;
});
$('#text').keyup(function(e) {
if (e.keyCode === 13) {
$('#send').click();
return false;
} }
});
});
</script> </script>
</head> </head>
<body> <body>
<h3>Chat!</h3> <h3>Chat!</h3>
<div> <div>
<button id="connect">Connect</button>&nbsp;|&nbsp;Status: <button id="btn_connect">Connect</button>
<span id="status">disconnected</span> Status: <span id="span_status">disconnected</span>
</div> </div>
<div id="log"
style="width:20em;height:15em;overflow:auto;border:1px solid black"> <div
</div> id="div_log"
<form id="chatform" onsubmit="return false;"> style="width: 20em; height: 15em; overflow: auto; border: 1px solid black"
<input id="text" type="text" /> ></div>
<input id="send" type="button" value="Send" />
</form> <input id="input_text" type="text" />
<input id="btn_send" type="button" value="Send" />
</body> </body>
</html> </html>