/
index.html
146 lines (126 loc) · 3.54 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Chat App socket.io + express</title>
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=Noto+Sans+Thai:wght@300&display=swap"
rel="stylesheet"
/>
<style>
* {
font-family: 'Noto Sans Thai', sans-serif;
}
.page-title {
text-align: center;
font-size: 2rem;
}
.chat-container {
display: flex;
flex-direction: column;
justify-content: flex-end;
height: 100vh;
width: 640px;
margin: 0 auto;
}
.chat-messages {
flex: 1;
overflow-y: scroll;
}
.message {
background-color: #f1f0f0;
border-radius: 5px;
padding: 10px;
margin-bottom: 10px;
}
.message .meta {
font-size: 0.8rem;
color: #777;
}
.message .text {
font-size: 1rem;
}
.chat-form {
display: flex;
margin-top: 10px;
}
.chat-form #name {
padding: 0.5rem;
margin-right: 0.25rem;
}
.chat-form #message {
flex: 1;
padding: 0.5rem;
border-radius: 0.25rem;
border: 1px solid #4e4bfc;
margin-right: 0.5rem;
}
.chat-form button {
background-color: #4e4bfc;
color: #fff;
border: none;
border-radius: 5px;
padding: 10px;
cursor: pointer;
}
</style>
</head>
<body>
<div class="chat-container">
<h2 class="page-title">Chat App with socket.io + Express</h2>
<div class="chat-messages">
<div class="message">
<p class="meta">User 1 <span>9:12pm</span></p>
<p class="text">สวัสดีครับ!</p>
</div>
<div class="message">
<p class="meta">User 2 <span>9:15pm</span></p>
<p class="text">ดีจ้า</p>
</div>
</div>
<form class="chat-form" id="form">
<input type="text" id="name" />
<input type="text" id="message" />
<button type="submit">Send</button>
</form>
</div>
<script src="https://cdn.socket.io/4.5.4/socket.io.min.js"></script>
<script>
const socket = io()
const form = document.getElementById('form')
const message = document.getElementById('message')
const name = document.getElementById('name')
// initial name
name.value = getName()
form.addEventListener('submit', (e) => {
e.preventDefault()
if (message.value) {
const payload = {
username: name.value,
message: message.value,
time: new Date().toLocaleTimeString(),
}
socket.emit('chat:message', payload)
message.value = ''
}
})
socket.on('chat:message', (data) => {
const div = document.createElement('div')
div.classList.add('message')
div.innerHTML = `
<p class="meta">${data.username} <span>${data.time}</span></p>
<p class="text">${data.message}</p>
`
document.querySelector('.chat-messages').appendChild(div)
})
function getName() {
// get name from date timestamp
const date = new Date()
return 'User-' + date.getTime()
}
</script>
</body>
</html>