File size: 8,821 Bytes
056d2ad
9aa0995
cd1dd35
 
0955a87
49704c5
5759005
8e9e7f5
7ce5d63
8e9e7f5
 
 
7ce5d63
 
 
8e9e7f5
49704c5
 
 
 
8e9e7f5
 
 
 
7ce5d63
8e9e7f5
7ce5d63
 
6d71d03
8e9e7f5
 
 
 
7ce5d63
8e9e7f5
 
 
 
 
5759005
8e9e7f5
 
 
 
7ce5d63
49704c5
5759005
49704c5
7ce5d63
 
 
49704c5
5759005
49704c5
 
8e9e7f5
7ce5d63
8e9e7f5
 
 
 
7ce5d63
8e9e7f5
 
 
 
7ce5d63
8e9e7f5
 
7ce5d63
8e9e7f5
7ce5d63
8e9e7f5
 
 
 
7ce5d63
8e9e7f5
 
 
 
 
 
 
7ce5d63
5759005
 
 
 
 
8e9e7f5
7ce5d63
 
 
 
 
 
8e9e7f5
49704c5
8e9e7f5
 
7ce5d63
8e9e7f5
7ce5d63
 
8e9e7f5
7ce5d63
8e9e7f5
 
 
49704c5
 
8e9e7f5
 
 
7ce5d63
8e9e7f5
 
 
5759005
8e9e7f5
7ce5d63
8e9e7f5
 
7ce5d63
 
8e9e7f5
7ce5d63
8e9e7f5
49704c5
8e9e7f5
49704c5
8e9e7f5
7ce5d63
8e9e7f5
49704c5
8e9e7f5
49704c5
8e9e7f5
7ce5d63
8e9e7f5
 
7ce5d63
 
8e9e7f5
7ce5d63
 
 
8e9e7f5
7ce5d63
8e9e7f5
 
 
49704c5
8e9e7f5
 
7ce5d63
8e9e7f5
 
7ce5d63
8e9e7f5
 
 
7ce5d63
49704c5
8e9e7f5
7ce5d63
8e9e7f5
 
 
 
 
 
 
 
 
 
 
7ce5d63
 
8e9e7f5
7ce5d63
8e9e7f5
49704c5
5759005
 
49704c5
7ce5d63
8e9e7f5
 
 
 
7ce5d63
8e9e7f5
 
7ce5d63
49704c5
7ce5d63
 
8e9e7f5
7ce5d63
 
8e9e7f5
 
49704c5
7ce5d63
 
 
6d71d03
7ce5d63
 
58acc0d
49704c5
 
7ce5d63
49704c5
7ce5d63
 
49704c5
7ce5d63
49704c5
7ce5d63
49704c5
7ce5d63
 
 
49704c5
5759005
7ce5d63
6d71d03
7ce5d63
 
5759005
 
7ce5d63
 
5759005
 
7ce5d63
5759005
49704c5
cd1dd35
9aa0995
e2e7588
0955a87
 
 
 
 
 
 
 
 
 
 
08cec51
 
 
 
 
 
0955a87
 
 
cd1dd35
9aa0995
0955a87
e2e7588
dfc2073
3f9c14e
 
 
 
0955a87
 
 
 
cd1dd35
e2e7588
 
 
 
 
 
 
 
 
 
0955a87
e2e7588
 
 
 
 
cd1dd35
0955a87
cd1dd35
 
e2e7588
cd1dd35
0955a87
 
 
08cec51
0955a87
cd1dd35
e2e7588
0955a87
 
cd1dd35
0955a87
 
 
 
 
 
 
e2e7588
 
 
 
 
 
0955a87
3f9c14e
e2e7588
 
0955a87
3f9c14e
 
 
 
 
 
 
 
0955a87
 
 
 
e2e7588
0955a87
3f9c14e
0955a87
 
e2e7588
 
 
 
 
 
0955a87
e2e7588
0955a87
e2e7588
0955a87
 
 
 
 
e2e7588
0955a87
 
 
 
 
 
3f9c14e
 
cd1dd35
9aa0995
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Stranger Chat</title>
<style>
  /*
  ================================================================================
  V3: ADAPTIVE CSS FOR MOBILE & DESKTOP
  ================================================================================
*/

/* --- 1. Mobile-First Base Styles (Default) --- */
/* These styles apply to all screens, optimized for mobile. */

:root {
  --header-bg: #005e54;
  --chat-bg: #e5ddd5;
  --bubble-me-bg: #dcf8c6;
  --bubble-other-bg: #ffffff;
  --input-bg: #f0f0f0;
  --primary-text: #075e54;
  --secondary-text: #667781;
  --font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

html {
  /* Set a larger base font size for mobile readability */
  font-size: 1.0em;
}

body {
  font-family: var(--font-family);
  background: #e9eaed; /* A neutral desktop background */
  margin: 0;
  padding: 0;
  color: #333;
}

/* Hide chat by default */
#chat {
  display: none;
}

/* --- Mobile Containers: Full Screen --- */
#lobby, #chat {
  height: 100vh;
  width: 100%;
  margin: 0;
  border-radius: 0;
  box-shadow: none;
  box-sizing: border-box;
  flex-direction: column;
}

#lobby {
  background: white;
  justify-content: center;
  padding: 1.5rem;
}

/* --- Mobile Forms & Inputs: Larger for Touch --- */
#lobby h2 {
  color: var(--primary-text);
  text-align: center;
}

#lobby input, #lobby select, #lobby button {
  width: 100%;
  padding: 1.1rem; /* More padding for easier tapping */
  margin: 0.5rem 0;
  box-sizing: border-box;
  border: 1px solid #ccc;
  border-radius: 8px;
  font-size: 1rem;
}

#lobby button {
  background-color: var(--header-bg);
  color: white;
  font-weight: bold;
  border: none;
  cursor: pointer;
}

#lobbyStatus {
  text-align: center;
  margin-top: 1rem;
  color: var(--secondary-text);
}

/* --- Chat UI Styling (Mobile) --- */
#chat {
  background: var(--chat-bg);
  overflow: hidden;
}

#chatHeader {
  flex-shrink: 0;
  background: var(--header-bg);
  color: white;
  font-weight: 600;
  font-size: 1.1rem;
  padding: 1rem;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
  z-index: 10;
  text-align: center;
}

#messages {
  flex-grow: 1;
  overflow-y: auto;
  padding: 1rem 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}

#messages div {
  padding: 0.6rem 0.9rem;
  border-radius: 12px;
  max-width: 85%;
  word-wrap: break-word;
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.08);
  line-height: 1.4;
  font-size: 1rem; /* Base this on the root html font-size */
}

.me {
  align-self: flex-end;
  background-color: var(--bubble-me-bg);
  border-bottom-right-radius: 2px;
}

.other {
  align-self: flex-start;
  background-color: var(--bubble-other-bg);
  border-bottom-left-radius: 2px;
}

.status {
  align-self: center;
  background-color: rgba(0, 0, 0, 0.1);
  color: white;
  font-size: 0.8rem;
  font-style: normal;
  font-weight: 500;
  padding: 0.4rem 0.8rem;
  box-shadow: none;
  border-radius: 20px;
}

#input-area {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  padding: 0.6rem 0.75rem;
  background-color: var(--input-bg);
}

#msg {
  flex-grow: 1;
  border: none;
  padding: 0.9rem 1.1rem;
  border-radius: 2rem;
  font-size: 1rem;
  margin-right: 0.6rem;
  background: white;
}
#msg:focus {
  outline: none;
}

button {
  border: none;
  cursor: pointer;
  background: transparent;
  color: white;
  font-size: 1rem;
  font-weight: bold;
}

#send {
  background-color: var(--header-bg);
  min-width: 50px;
  min-height: 50px;
  border-radius: 50%;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

#leave {
  width: 100%;
  padding: 0.5rem;
  color: var(--secondary-text);
  background-color: var(--input-bg);
  text-align: center;
  font-size: 0.9rem;
  font-weight: normal;
  flex-shrink: 0;
}


/* --- 2. Desktop Overrides --- */
/* These styles apply ONLY when screen width is 768px or more. */

@media (orientation: landscape) {
  html {
    /* Reset to a standard desktop font size */
    font-size: 1em;
  }

  /* Create a centered, windowed look for containers */
  #lobby, #chat {
    height: auto;
    margin: 2rem auto;
    border-radius: 12px;
    box-shadow: 0 5px 20px rgba(0, 0, 0, 0.15);
  }
  
  #lobby {
    max-width: 450px; /* Constrain lobby width */
    min-height: 0;
    padding: 2rem;
  }

  #chat {
    max-width: 80%;
    height: calc(100vh - 4rem); /* Fill most of the screen but not all */
    max-height: 900px; /* Set a max-height */
  }

  #leave {
    background-color: white; /* Match background on desktop */
  }
    }
  
</style>
  
</head>
<body>

  <div id="lobby">
    <h2>Create Profile & Join</h2>
    <form id="form">
      ID: <input name="id" required><br>
      Gender:
      <select name="gender"><option>Male</option><option>Female</option></select><br>
      Age: <input name="age" type="number" required><br>
      Interests:<br>
      <label><input type="checkbox" name="interests" value="science">Science</label><br>
      <label><input type="checkbox" name="interests" value="arts">Arts</label><br>
      <label><input type="checkbox" name="interests" value="life">Life</label><br>
      Looking for:
      <select name="looking">
        <option value="any">Any</option>
        <option value="male">Male</option>
        <option value="female">Female</option>
      </select><br>
      <button>Enter Lobby</button>
    </form>
    <div id="lobbyStatus"></div>
  </div>

  <div id="chat">
    <div id="chatHeader">Chat Room</div>
    <div id="messages"></div>
    <div id="input-area">
      <input id="msg" autocomplete="off" placeholder="Type a message...">
      <button id="send">Send</button>
    </div>
    <button id="leave">Leave</button>
  </div>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.8.1/socket.io.js"></script>
  <script>
    const socket       = io();
    const form         = document.getElementById("form");
    const lobby        = document.getElementById("lobby");
    const lobbyStatus  = document.getElementById("lobbyStatus");
    const chatDiv      = document.getElementById("chat");
    const messages     = document.getElementById("messages");
    const input        = document.getElementById("msg");
    const sendBtn      = document.getElementById("send");
    const leaveBtn     = document.getElementById("leave");
    const chatHeader   = document.getElementById("chatHeader");

    let currentRoom = null;
    let myId = null;
    let mySid = null;
    let partnerId = null;
    let partnerSid = null;

    form.addEventListener("submit", e => {
      e.preventDefault();
      const data = new FormData(form);
      myId = data.get("id");
      const profile = {
        id: data.get("id"),
        gender: data.get("gender"),
        age: data.get("age"),
        looking: data.get("looking"),
        interests: data.getAll("interests")
      };

      lobbyStatus.textContent = "Finding partner...";
      socket.emit("find", profile);
    });

    socket.on("status", d => {
      lobbyStatus.textContent = d.msg;
      appendMessage(d.msg, "status");
    });

    socket.on("matched", d => {
      currentRoom  = d.room;
      mySid        = socket.id;
      myId         = d.my_id;
      partnerId    = d.partner_id;
      partnerSid   = d.partner_sid;

      lobby.style.display = "none";
      chatDiv.style.display = "flex"; // Changed from 'block' to 'flex'
      chatHeader.textContent = `Chatroom of ${myId} and ${partnerId}`;
      appendMessage("Matched! Start chatting.", "status");
    });
    
    // Allow sending message with Enter key
    input.addEventListener("keypress", (e) => {
        if (e.key === "Enter") {
            e.preventDefault(); // Prevents new line in input
            sendBtn.onclick();
        }
    });

    sendBtn.onclick = () => {
      const txt = input.value.trim();
      if (!txt || !currentRoom) return;
      socket.emit("chat", { room: currentRoom, msg: txt, sid: mySid });
      input.value = "";
      input.focus(); // Keep focus on the input after sending
    };

    socket.on("chat", d => {
      const who = d.sid === mySid ? "You" : partnerId || "Partner";
      const cls = d.sid === mySid ? "me" : "other";
      appendMessage(`${who}: ${d.msg}`, cls);
    });

    leaveBtn.onclick = () => {
      if (confirm("Leave?")) socket.emit("leave", { room: currentRoom });
    };

    socket.on("left", () => {
      appendMessage("You left the chat.", "status");
      setTimeout(() => location.reload(), 1000);
    });

    function appendMessage(text, cls = "chat") {
      const d = document.createElement("div");
      d.textContent = text;
      if (cls) d.classList.add(cls);
      messages.append(d);
      messages.scrollTop = messages.scrollHeight;
    }
    

  </script>
</body>
</html>