Created
May 3, 2022 12:13
-
-
Save sudocode1/df34878b2b03c83da01f4b6d287fe090 to your computer and use it in GitHub Desktop.
textarea stuffs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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