Skip to content

Instantly share code, notes, and snippets.

@sudocode1
Created May 3, 2022 12:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sudocode1/df34878b2b03c83da01f4b6d287fe090 to your computer and use it in GitHub Desktop.
Save sudocode1/df34878b2b03c83da01f4b6d287fe090 to your computer and use it in GitHub Desktop.
textarea stuffs
<span id="chatBox"></span> <Br><br>
<textarea id="textBox"></textarea>
<script>
let ws = new WebSocket('ws://localhost:PORT');
let messages = [];
ws.onopen = () => {
//...
}
ws.onmessage = s => {
let d = s.data;
//eg username: message
if (messages.length == 5) {
messages.shift();
}
messages.push(s.data);
document.getElementById('chatBox').innerHTML = '';
messages.forEach(message => {
document.getElementById('chatBox').innerHTML += `${message}<br>`
})
}
setInterval(() => {
if (document.getElementById('textBox').value.endsWith('\n')) {
ws.send(document.getElementById('textBox').value);
document.getElementById('textBox').value = "";
}
}, 10)
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment