NihalGazi commited on
Commit
3474c3f
·
verified ·
1 Parent(s): 9188057

Update static/chat.js

Browse files
Files changed (1) hide show
  1. 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
- const chat = document.getElementById("chat");
10
- const input = document.getElementById("msg");
11
- const sendBtn = document.getElementById("send");
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
- sendBtn.onclick = () => {
20
- const txt = input.value.trim();
21
- if (!txt || !currentRoom) return;
22
- socket.emit("chat", { room: currentRoom, msg: txt, sender: myId });
23
- input.value = "";
24
- };
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
- leaveBtn.onclick = () => {
27
- if (confirm("Leave?")) {
28
- socket.emit("leave", { room: currentRoom });
29
- sessionStorage.clear();
30
- window.location.href = "/";
31
- }
32
- };
33
 
34
- socket.on("chat", d => {
35
- if (d.msg.startsWith("__joined__:")) {
36
- const id = d.msg.split(":")[1];
37
- if (id !== myId) {
38
- partnerId = id;
39
- title.textContent = `Chatroom of ${myId} and ${partnerId}`;
40
  }
41
- return;
42
- }
43
-
44
- const sender = d.sender === myId ? "me" : "other";
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.