1
0
mirror of https://github.com/actix/examples synced 2025-02-21 08:44:48 +01:00

remove jquery from broker chat client side

This commit is contained in:
Rob Ede 2020-04-09 03:20:18 +01:00
parent a892a062e1
commit 56443d9e9c
No known key found for this signature in database
GPG Key ID: C2A3B36E841A91E6

View File

@ -1,90 +1,204 @@
<!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"> <meta charset="utf-8" />
</script> <title>Websocket Chat Broker</title>
<script language="javascript" type="text/javascript">
$(function() { <style>
var conn = null; :root {
function log(msg) { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto,
var control = $('#log'); Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
control.html(control.html() + msg + '<br/>'); font-size: 18px;
control.scrollTop(control.scrollTop() + 1000);
} }
function connect() {
disconnect(); input[type='text'] {
var wsUri = (window.location.protocol=='https:'&&'wss://'||'ws://')+window.location.host + '/ws/'; font-size: inherit;
conn = new WebSocket(wsUri);
log('Connecting...');
conn.onopen = function() {
log('Connected.');
update_ui();
};
conn.onmessage = function(e) {
log('Received: ' + e.data);
};
conn.onclose = function() {
log('Disconnected.');
conn = null;
update_ui();
};
} }
function disconnect() {
if (conn != null) { #log {
log('Disconnecting...'); width: 30em;
conn.close(); height: 20em;
conn = null; overflow: auto;
update_ui(); margin: 0.5em 0;
border: 1px solid black;
} }
#status {
padding: 0 0.2em;
} }
function update_ui() {
var msg = ''; #text {
if (conn == null) { width: 17em;
$('#status').text('disconnected'); padding: 0.5em;
$('#connect').html('Connect');
} else {
$('#status').text('connected (' + conn.protocol + ')');
$('#connect').html('Disconnect');
} }
.msg {
margin: 0;
padding: 0.25em 0.5em;
} }
$('#connect').click(function() {
if (conn == null) { .msg--status {
connect(); /* a light yellow */
} else { background-color: #ffffc9;
disconnect();
} }
update_ui();
return false; .msg--message {
}); /* a light blue */
$('#send').click(function() { background-color: #d2f4ff;
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;
} }
});
}); .msg--error {
</script> background-color: pink;
}
</style>
</head> </head>
<body> <body>
<h3>Chat!</h3> <h1>Chat!</h1>
<div> <div>
<button id="connect">Connect</button>&nbsp;|&nbsp;Status: <button id="connect">Connect</button>
<span>Status:</span>
<span id="status">disconnected</span> <span id="status">disconnected</span>
</div> </div>
<div id="log"
style="width:20em;height:15em;overflow:auto;border:1px solid black"> <div id="log"></div>
</div>
<form id="chatform" onsubmit="return false;"> <form id="chatform">
<input id="text" type="text" /> <input type="text" id="text" />
<input id="send" type="button" value="Send" /> <input type="submit" id="send" />
</form> </form>
<hr />
<section>
<h2>Commands</h2>
<table style="border-spacing: 0.5em;">
<tr>
<td>
<code>/list</code>
</td>
<td>
list all available rooms
</td>
</tr>
<tr>
<td>
<code>/join name</code>
</td>
<td>
join room, if room does not exist, create new one
</td>
</tr>
<tr>
<td>
<code>/name name</code>
</td>
<td>
set session name
</td>
</tr>
<tr>
<td>
<code>some message</code>
</td>
<td>
just string, send message to all peers in same room
</td>
</tr>
</table>
</section>
<script>
const $status = document.querySelector('#status')
const $connectButton = document.querySelector('#connect')
const $log = document.querySelector('#log')
const $form = document.querySelector('#chatform')
const $input = document.querySelector('#text')
/** @type {WebSocket | null} */
var socket = null
function log(msg, type = 'status') {
$log.innerHTML += `<p class="msg msg--${type}">${msg}</p>`
$log.scrollTop += 1000
}
function connect() {
disconnect()
const { location } = window
const proto = location.protocol.startsWith('https') ? 'wss' : 'ws'
const wsUri = `${proto}://${location.host}/ws/`
log('Connecting...')
socket = new WebSocket(wsUri)
socket.onopen = () => {
log('Connected')
updateConnectionStatus()
}
socket.onmessage = (ev) => {
log('Received: ' + ev.data, 'message')
}
socket.onclose = () => {
log('Disconnected')
socket = null
updateConnectionStatus()
}
}
function disconnect() {
if (socket) {
log('Disconnecting...')
socket.close()
socket = null
updateConnectionStatus()
}
}
function updateConnectionStatus() {
if (socket) {
$status.style.backgroundColor = 'transparent'
$status.style.color = 'green'
$status.textContent = `connected`
$connectButton.innerHTML = 'Disconnect'
$input.focus()
} else {
$status.style.backgroundColor = 'red'
$status.style.color = 'white'
$status.textContent = 'disconnected'
$connectButton.textContent = 'Connect'
}
}
$connectButton.addEventListener('click', () => {
if (socket) {
disconnect()
} else {
connect()
}
updateConnectionStatus()
})
$form.addEventListener('submit', (ev) => {
ev.preventDefault()
const text = $input.value
log('Sending: ' + text)
socket.send(text)
$input.value = ''
$input.focus()
})
updateConnectionStatus()
</script>
</body> </body>
</html> </html>