Spaces:
Running
Running
Update static/chat.js
Browse files- static/chat.js +36 -51
static/chat.js
CHANGED
@@ -1,63 +1,48 @@
|
|
1 |
(() => {
|
2 |
const params = new URLSearchParams(window.location.search);
|
3 |
const room = params.get("room");
|
4 |
-
const socket = io();
|
5 |
-
let myId = sessionStorage.getItem("myId") || "me";
|
6 |
-
let currentRoom = sessionStorage.getItem("roomId") || null;
|
7 |
-
let partnerId = null;
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
const leaveBtn = document.getElementById("leave");
|
13 |
-
const title = document.getElementById("chatroom-title");
|
14 |
-
|
15 |
-
// join room
|
16 |
-
socket.emit("subscribe", { room: currentRoom });
|
17 |
-
socket.emit("chat", { room: currentRoom, msg: `__joined__:${myId}` });
|
18 |
|
19 |
-
|
20 |
-
const
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
};
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
if (id !== myId) {
|
38 |
-
partnerId = id;
|
39 |
-
title.textContent = `Chatroom of ${myId} and ${partnerId}`;
|
40 |
}
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
appendMessage(`${d.sender}: ${d.msg}`, sender);
|
46 |
-
});
|
47 |
-
|
48 |
-
socket.on("left", () => {
|
49 |
-
appendMessage("Partner left the chat.", "status");
|
50 |
-
setTimeout(() => window.location.href = "/", 1500);
|
51 |
-
});
|
52 |
|
53 |
-
function appendMessage(text, cls = "") {
|
54 |
-
const line = document.createElement("div");
|
55 |
-
line.textContent = text;
|
56 |
-
line.classList.add("chat-line");
|
57 |
-
if (cls) line.classList.add(cls);
|
58 |
-
chat.append(line);
|
59 |
-
chat.scrollTop = chat.scrollHeight;
|
60 |
-
}
|
61 |
socket.on("matched", data => {
|
62 |
// If you ever get matched here, you can handle it, but in this flow
|
63 |
// matching happens before navigation.
|
|
|
1 |
(() => {
|
2 |
const params = new URLSearchParams(window.location.search);
|
3 |
const room = params.get("room");
|
4 |
+
const socket = io();
|
|
|
|
|
|
|
5 |
|
6 |
+
socket.on("connect", () => {
|
7 |
+
socket.emit("subscribe", { room });
|
8 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
+
const chatDiv = document.getElementById("chat");
|
11 |
+
const input = document.getElementById("msg");
|
12 |
+
const sendBtn = document.getElementById("send");
|
13 |
+
const leaveBtn= document.getElementById("leave");
|
14 |
+
|
15 |
+
sendBtn.onclick = () => {
|
16 |
+
const text = input.value.trim();
|
17 |
+
if (!text) return;
|
18 |
+
socket.emit("chat", { room, msg: text });
|
19 |
+
input.value = "";
|
20 |
+
};
|
21 |
+
|
22 |
+
socket.on("chat", data => {
|
23 |
+
const b = document.createElement("div");
|
24 |
+
b.textContent = data.msg;
|
25 |
+
chatDiv.append(b);
|
26 |
+
chatDiv.scrollTop = chatDiv.scrollHeight;
|
27 |
+
});
|
28 |
|
29 |
+
socket.on("status", data => {
|
30 |
+
const s = document.createElement("div");
|
31 |
+
s.style.fontStyle = "italic";
|
32 |
+
s.textContent = data.msg;
|
33 |
+
chatDiv.append(s);
|
34 |
+
chatDiv.scrollTop = chatDiv.scrollHeight;
|
35 |
+
});
|
36 |
|
37 |
+
leaveBtn.onclick = () => {
|
38 |
+
if (confirm("Leave chat?")) {
|
39 |
+
socket.emit("leave", { room });
|
|
|
|
|
|
|
40 |
}
|
41 |
+
};
|
42 |
+
socket.on("left", () => {
|
43 |
+
window.location = "/";
|
44 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
socket.on("matched", data => {
|
47 |
// If you ever get matched here, you can handle it, but in this flow
|
48 |
// matching happens before navigation.
|