-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.php
90 lines (75 loc) · 2.07 KB
/
index.php
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Chat Interface</title>
<!-- Include Tailwind CSS -->
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/tailwindcss/2.2.7/tailwind.min.css"
/>
</head>
<body>
<div class="h-screen flex flex-col">
<div class="flex-1 bg-gray-100 p-6 flex flex-col justify-end">
<div class="flex flex-col gap-2" id="chats">
<!-- Add more messages here as needed -->
</div>
</div>
<form onsubmit="sendMsg();return false;">
<div class="bg-white p-4 flex">
<input
type="text"
id="msg"
placeholder="Type your message here..."
class="flex-1 border rounded-lg py-2 px-4 mr-4"
/>
<button type="submit" class="bg-blue-500 hover:bg-blue-600 text-white rounded-lg py-2 px-4">
Send
</button>
</div>
</form>
</div>
</body>
<script>
var username = '';
if(!username)
{
username = prompt("Hey there, good looking stranger! What's your name?", "");
}
function sendMsg(){
if(!username)
{
username = prompt("Hey there, good looking stranger! What's your name?", "");
if(!username)
{
return;
}
}
var msg = document.getElementById("msg").value;
if(!msg)
{
return;
}
document.querySelector("#msg").value='';
fetch('?msg='+username+ " : " +msg);
}
var source = new EventSource('?server=yes');
var old = '';
source.onmessage = function(e)
{
if(old!=e.data){
const [un, text] = e.data.split(' : ');
const mtd = `<span class="font-bold">${un}</span> - ${text}`;
if(e.data.includes(username+" :")){
document.getElementById("chats").innerHTML+='<div class="bg-gray-300 text-gray-700 rounded-lg p-2 max-w-max self-end">'+mtd+'</div>';
}else{
document.getElementById("chats").innerHTML+='<div class="bg-blue-500 text-white rounded-lg p-2 max-w-max">'+mtd+'</div>';
}
window.scrollTo(0, document.body.scrollHeight);
old = e.data;
}
};
</script>
</html>