commit 3ff3a89f8ecd4ab79779d471ac47947de0bcc1a7 Author: usernames122 <88596366+usernames122@users.noreply.github.com> Date: Thu Aug 7 23:58:45 2025 +0200 first commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..1794b5b --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# Old webchat client + +Nothing much to say? \ No newline at end of file diff --git a/assets/css/style.css b/assets/css/style.css new file mode 100644 index 0000000..95f02b4 --- /dev/null +++ b/assets/css/style.css @@ -0,0 +1,49 @@ +header { + display: flex; + flex-direction: row; + justify-content: space-between; +} + +#username { + font-weight: 500; +} + +.chatbox{ + height: 88vh; + width:100%; + background-color: black; + overflow-y: scroll; + overflow-x: hidden; + font-family: 'Consolas', 'Arial', sans-serif; + color: white; +} + +.container { + background-color: black; +} + +.chattext { + color: white; +} +.consolas { + font-family: 'Consolas', 'Arial', sans-serif; +} + +body { + overflow: hidden; +} + +.chatstuff { + display: flex; + background-color: rgb(50,50,50); +} + +.chatstuff button { + align-self: flex-end; +} + +#message { + flex: 1; + resize: none; +} + diff --git a/assets/js/chat.js b/assets/js/chat.js new file mode 100644 index 0000000..4514ef0 --- /dev/null +++ b/assets/js/chat.js @@ -0,0 +1,127 @@ +class Client extends WebSocket { + #connected = false + username = "Guest".concat(Math.round(Math.random() * 9999).toString()); + + constructor(url, username) { + super(url); + if (username != null || username != "") { + this.username = username; + } + super.onopen = this.onopen; + super.onmessage = this.onmessage; + super.onclose = this.onclose; + console.log("Trying to connect to server {} with username {}.".format(url,username)) + } + + onopen() { + console.log("Connecting..."); + this.connected = true; + this.send({username: this.username, clientdata: {}}); + } + + onmessage(event) { + var chatbox = document.getElementById("chat-box"); + var follow = chatbox.scrollHeight - chatbox.scrollTop - chatbox.clientHeight == 0; + try { + const data = JSON.parse(event.data); + switch (data.type) { + case "message": + console.log(data.content.replaceAll("\n", " ")); + data.content.split("\n").forEach(line => { + chatbox.innerHTML += "

" + line + "

"; + }) + if (data.content.slice(0, data.content.search(":")) != this.username) { + document.getElementById('notify').play(); + } + break; + case "join": + console.log(data.username + " joined the chat"); + chatbox.innerHTML += "

" + data.username + " joined the chat

"; + if (data.username != this.username) { + document.getElementById('join').play(); + } + break; + case "leave": + console.log(data.username + " left the chat"); + chatbox.innerHTML += "

" + data.username + " left the chat

"; + break; + default: + console.log("Unhandled packet: " + JSON.stringify(data)); + this.send({type: "unhandled", packet: JSON.stringify(data)}); + break; + } + } catch { + console.log(event.data) + chatbox.innerHTML += "

" + event.data + "

"; + if (event.data.slice(0, event.data.search(":")) != this.username) { + document.getElementById('audioElement').play(); + } + } + if (follow) { + chatbox.scrollTop = chatbox.scrollHeight; + } + } + + onclose() { + this.connected = false; + console.log("Disconnected!"); + document.getElementById("chat-box").innerHTML += "

Disconnected

"; + } + + send(data) { + super.send(JSON.stringify(data)); + } +}; + +// JS Patch for formatting +// STACKOVERFLOW!!! +String.prototype.format = function () { + var i = 0, args = arguments; + return this.replace(/{}/g, function () { + return typeof args[i] != 'undefined' ? args[i++] : ''; + }); +}; + +let socket = new Client("ws://" + (location.host.search(":") == -1 ? location.host : location.host.slice(0, location.host.search(":"))) + ":8765", getUsername()); + +function getUsername() { + var username = localStorage.getItem("username") + if (username == null) { + username = prompt("Enter your username. Press cancel or just type in nothing to get a anonymous username:"); + if (username == null || username == ""){ + username = "Guest".concat(Math.round(Math.random() * 9999).toString()); + } + localStorage.setItem("username", username); + } + return username; +} + +function setUsername() { + username = prompt("Enter your username. Press cancel or just type in nothing to get a anonymous username:"); + if (username == null || username == ""){ + username = "Guest".concat(Math.round(Math.random() * 9999).toString()); + } + localStorage.setItem("username", username); + location.reload(); +} + +function onload() { + document.getElementById("username").innerText = "Chatting as " + getUsername(); + document.getElementById("message").addEventListener("keyup", event => { + if (event.key == "Enter" && !event.shiftKey) { + document.getElementById("sendbutton").click(); + } + event.preventDefault(); + }); +} + +function sendMessage() { + var element = document.getElementById("message"); + var message = element.value; + element.value = message.split + socket.send({ + type: "message", + message: message + }); + element.value = ""; +} \ No newline at end of file diff --git a/assets/mp3/notify.mp3 b/assets/mp3/notify.mp3 new file mode 100644 index 0000000..27d6bbf Binary files /dev/null and b/assets/mp3/notify.mp3 differ diff --git a/assets/wav/join.wav b/assets/wav/join.wav new file mode 100644 index 0000000..414d7f4 Binary files /dev/null and b/assets/wav/join.wav differ diff --git a/favicon.ico b/favicon.ico new file mode 100644 index 0000000..9310190 Binary files /dev/null and b/favicon.ico differ diff --git a/index.html b/index.html new file mode 100644 index 0000000..ff7f849 --- /dev/null +++ b/index.html @@ -0,0 +1,27 @@ + + + + WebChat + + + + +
+

WebChat

+
+

+

+
+ + +
+
+

Connecting to server...

+
+
+ + +
+ +