mirror of
https://github.com/actix/examples
synced 2025-02-08 20:06:07 +01:00
parent
7148fd3d0e
commit
542e6dbcdd
@ -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
|
||||||
}
|
|
||||||
function connect() {
|
const log = (msg) => {
|
||||||
disconnect();
|
div_log.innerHTML += msg + '<br>'
|
||||||
var wsUri = (window.location.protocol=='https:'&&'wss://'||'ws://')+window.location.host + '/ws/';
|
div_log.scroll(0, div_log.scrollTop + 1000)
|
||||||
conn = new WebSocket(wsUri);
|
}
|
||||||
log('Connecting...');
|
|
||||||
conn.onopen = function() {
|
const connect = () => {
|
||||||
log('Connected.');
|
disconnect()
|
||||||
update_ui();
|
|
||||||
};
|
const wsUri =
|
||||||
conn.onmessage = function(e) {
|
(window.location.protocol === 'https:' ? 'wss://' : 'ws://') +
|
||||||
log('Received: ' + e.data);
|
window.location.host +
|
||||||
};
|
'/ws/'
|
||||||
conn.onclose = function() {
|
|
||||||
log('Disconnected.');
|
conn = new WebSocket(wsUri)
|
||||||
conn = null;
|
log('Connecting...')
|
||||||
update_ui();
|
|
||||||
};
|
conn.onopen = function () {
|
||||||
}
|
log('Connected.')
|
||||||
function disconnect() {
|
update_ui()
|
||||||
if (conn != null) {
|
}
|
||||||
log('Disconnecting...');
|
|
||||||
conn.close();
|
conn.onmessage = function (e) {
|
||||||
conn = null;
|
log('Received: ' + e.data)
|
||||||
update_ui();
|
}
|
||||||
|
|
||||||
|
conn.onclose = function () {
|
||||||
|
log('Disconnected.')
|
||||||
|
conn = null
|
||||||
|
|
||||||
|
update_ui()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const disconnect = () => {
|
||||||
|
if (conn) {
|
||||||
|
log('Disconnecting...')
|
||||||
|
conn.close()
|
||||||
|
conn = null
|
||||||
|
|
||||||
|
update_ui()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const update_ui = () => {
|
||||||
|
if (!conn) {
|
||||||
|
span_status.textContent = 'disconnected'
|
||||||
|
btn_connect.textContent = 'Connect'
|
||||||
|
} else {
|
||||||
|
span_status.textContent = `connected (${conn.protocol})`
|
||||||
|
btn_connect.textContent = 'Disconnect'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
btn_connect.onclick = () => {
|
||||||
|
if (!conn) {
|
||||||
|
connect()
|
||||||
|
} else {
|
||||||
|
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()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
function update_ui() {
|
</script>
|
||||||
var msg = '';
|
</head>
|
||||||
if (conn == null) {
|
|
||||||
$('#status').text('disconnected');
|
<body>
|
||||||
$('#connect').html('Connect');
|
<h3>Chat!</h3>
|
||||||
} else {
|
<div>
|
||||||
$('#status').text('connected (' + conn.protocol + ')');
|
<button id="btn_connect">Connect</button>
|
||||||
$('#connect').html('Disconnect');
|
Status: <span id="span_status">disconnected</span>
|
||||||
}
|
</div>
|
||||||
}
|
|
||||||
$('#connect').click(function() {
|
<div
|
||||||
if (conn == null) {
|
id="div_log"
|
||||||
connect();
|
style="width: 20em; height: 15em; overflow: auto; border: 1px solid black"
|
||||||
} else {
|
></div>
|
||||||
disconnect();
|
|
||||||
}
|
<input id="input_text" type="text" />
|
||||||
update_ui();
|
<input id="btn_send" type="button" value="Send" />
|
||||||
return false;
|
</body>
|
||||||
});
|
|
||||||
$('#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>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<h3>Chat!</h3>
|
|
||||||
<div>
|
|
||||||
<button id="connect">Connect</button> | Status:
|
|
||||||
<span id="status">disconnected</span>
|
|
||||||
</div>
|
|
||||||
<div id="log"
|
|
||||||
style="width:20em;height:15em;overflow:auto;border:1px solid black">
|
|
||||||
</div>
|
|
||||||
<form id="chatform" onsubmit="return false;">
|
|
||||||
<input id="text" type="text" />
|
|
||||||
<input id="send" type="button" value="Send" />
|
|
||||||
</form>
|
|
||||||
</body>
|
|
||||||
</html>
|
</html>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user