Create chat.js
Browse files- public/chat.js +23 -0
public/chat.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const socket = io();
|
| 2 |
+
|
| 3 |
+
const urlParams = new URLSearchParams(window.location.search);
|
| 4 |
+
const roomId = urlParams.get("room");
|
| 5 |
+
const name = urlParams.get("name");
|
| 6 |
+
|
| 7 |
+
document.getElementById("roomTitle").innerText = "Room: " + roomId;
|
| 8 |
+
|
| 9 |
+
socket.emit("join-room", { roomId, name });
|
| 10 |
+
|
| 11 |
+
socket.on("message", (data) => {
|
| 12 |
+
const chat = document.getElementById("chat");
|
| 13 |
+
chat.innerHTML += `<p><b>${data.name}:</b> ${data.text}</p>`;
|
| 14 |
+
chat.scrollTop = chat.scrollHeight;
|
| 15 |
+
});
|
| 16 |
+
|
| 17 |
+
function send() {
|
| 18 |
+
const msgInput = document.getElementById("msg");
|
| 19 |
+
const msg = msgInput.value;
|
| 20 |
+
if (!msg) return;
|
| 21 |
+
socket.emit("send-message", msg);
|
| 22 |
+
msgInput.value = "";
|
| 23 |
+
}
|