21 lines
679 B
JavaScript
21 lines
679 B
JavaScript
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
function receivedMessage(msg) {
|
|
document.querySelector('#chat-echo-area')
|
|
.insertAdjacentHTML("beforeend", msg.data);
|
|
}
|
|
|
|
// TODO {{ server-name }}:{{ server-port }}
|
|
const ws = new WebSocket("ws://" + window.location.host + "/chat-server");
|
|
ws.addEventListener('message', receivedMessage);
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
const inputField = document.getElementById("chat-input");
|
|
inputField.addEventListener("keyup", (evt) => {
|
|
if (evt.key === "Enter") {
|
|
ws.send(evt.target.value);
|
|
evt.target.value = "";
|
|
}
|
|
});
|