/
index.html
121 lines (118 loc) · 3.08 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>WebSockets Chat</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto",
"Oxygen", "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans",
"Helvetica Neue", sans-serif;
}
.container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
border: 0.5rem solid rebeccapurple;
}
.messages {
flex: 1;
overflow: auto;
width: 100%;
}
.input {
display: flex;
align-items: center;
width: 100%;
border-top: 0.5rem solid rebeccapurple;
}
.input input {
font-size: 20px;
flex: 1;
padding: 1rem;
border: none;
outline: none;
}
.input button {
background-color: green;
color: white;
border: none;
cursor: pointer;
padding: 10px;
height: 100%;
}
.other-message {
display: flex;
justify-content: flex-start;
width: 100%;
}
.other-message .text {
background-color: #ddd;
padding: 10px;
margin: 10px;
border-radius: 10px;
}
.my-message {
display: flex;
justify-content: flex-end;
width: 100%;
}
.my-message .text {
background-color: rebeccapurple;
color: white;
padding: 10px;
margin: 10px;
border-radius: 10px;
align-self: flex-end;
}
</style>
</head>
<body>
<div class="container">
<div class="messages"></div>
<div class="input">
<input
type="text"
class="message"
placeholder="Type a message..."
onenter
/>
<button class="send" onclick="send()">Send</button>
</div>
</div>
<script>
const socket = new WebSocket("ws://localhost:3000");
const messages = document.querySelector(".messages");
const input = document.querySelector(".message");
socket.addEventListener("message", (event) => {
const div = document.createElement("div");
div.classList.add("other-message");
div.innerHTML = `<span class="text">${event.data}</span>`;
messages.appendChild(div);
});
function send() {
socket.send(input.value);
const div = document.createElement("div");
div.classList.add("my-message");
div.innerHTML = `<span class="text">${input.value}</span>`;
messages.appendChild(div);
input.value = "";
}
input.addEventListener("keypress", function (event) {
if (event.key === "Enter") {
event.preventDefault();
send();
}
});
</script>
</body>
</html>