const messages = document.getElementById("messages");
const msg = document.getElementById("msg");
const uri = "ws://localhost:80";
const ws = new WebSocket(uri);
function add_msg(t)
{
var msg = JSON.parse(t);
var d = new Date(msg["tim"]);
messages.innerHTML += "
";
messages.innerHTML += "" + d.toLocaleTimeString() + "";
messages.innerHTML += " " + msg["usr"] + ":";
messages.innerHTML += " " + msg["msg"];
messages.scrollTop = messages.scrollHeight;
}
function send_msg(e)
{
if (e.keyCode == 13) {
const t = msg.value;
ws.send(JSON.stringify({
"usr": t.split(" ")[0],
"msg": t
}));
msg.value = "";
}
}
ws.onopen = function() {
messages.innerHTML = "Connected.";
};
ws.onmessage = function(m) {
add_msg(m.data);
};
ws.onclose = function() {
add_msg("Disconnected.");
};