1
0
mirror of https://github.com/actix/examples synced 2025-02-12 21:45:32 +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 =
conn.onopen = function() { (window.location.protocol === 'https:' ? 'wss://' : 'ws://') +
log('Connected.'); window.location.host +
update_ui(); '/ws/'
};
conn.onmessage = function(e) { conn = new WebSocket(wsUri)
log('Received: ' + e.data); log('Connecting...')
};
conn.onclose = function() { conn.onopen = function () {
log('Disconnected.'); log('Connected.')
conn = null; update_ui()
update_ui();
};
} }
function disconnect() {
if (conn != null) { conn.onmessage = function (e) {
log('Disconnecting...'); log('Received: ' + e.data)
conn.close(); }
conn = null;
update_ui(); conn.onclose = function () {
log('Disconnected.')
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();
return false; update_ui()
});
$('#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;
} }
});
}); btn_send.onclick = () => {
</script> if (!conn) return
</head>
<body> const text = input_text.value
<h3>Chat!</h3> log('Sending: ' + text)
<div> conn.send(text)
<button id="connect">Connect</button>&nbsp;|&nbsp;Status:
<span id="status">disconnected</span> input_text.value = ''
</div> input_text.focus()
<div id="log" }
style="width:20em;height:15em;overflow:auto;border:1px solid black">
</div> input_text.onkeyup = (e) => {
<form id="chatform" onsubmit="return false;"> if (e.key === 'Enter') {
<input id="text" type="text" /> btn_send.click()
<input id="send" type="button" value="Send" /> }
</form> }
</body> }
</script>
</head>
<body>
<h3>Chat!</h3>
<div>
<button id="btn_connect">Connect</button>
Status: <span id="span_status">disconnected</span>
</div>
<div
id="div_log"
style="width: 20em; height: 15em; overflow: auto; border: 1px solid black"
></div>
<input id="input_text" type="text" />
<input id="btn_send" type="button" value="Send" />
</body>
</html> </html>