-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
107 lines (96 loc) · 3.27 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
<!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" />
<link rel="stylesheet" href="/style.css" />
<title>Socket.io Chat App</title>
</head>
<body>
<div class="login-container-bg">
<div class="login-container">
<form id="login-form" action="">
<label for="display-name">Display Name: </label>
<input
type="text"
name="display-name"
id="display-name"
placeholder="John23"
/>
<button>Start Chatting</button>
</form>
<p id="invalid-text" style="color: red; font-size: 15px">
Invalid name
</p>
</div>
</div>
<ul id="messages"></ul>
<form id="form" action="">
<input id="input" autocomplete="off" /><button>Send</button>
</form>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io();
var messages = document.getElementById("messages");
var form = document.getElementById("form");
var input = document.getElementById("input");
var loginForm = document.getElementById("login-form");
var nameInput = document.getElementById("display-name");
var loginContainer =
document.getElementsByClassName("login-container-bg")[0];
var invalidText = document.getElementById("invalid-text");
var user = {
displayName: null,
};
// Login event listener
loginForm.addEventListener("submit", (e) => {
e.preventDefault();
console.log("login button pressed");
let name = nameInput.value;
if (name) {
socket.emit("validate name", name);
// loginContainer.style.display = "none";
}
});
// send chat message to server
form.addEventListener("submit", (e) => {
e.preventDefault();
if (input.value) {
socket.emit("chat message", input.value, user.displayName);
input.value = "";
}
});
socket.on("chat message", (msg) => {
var item = document.createElement("li");
item.textContent = msg;
messages.appendChild(item);
window.scrollTo(0, document.body.scrollHeight);
});
socket.on("join chat message", (msg) => {
var item = document.createElement("li");
item.textContent = msg;
item.style.color = "#57CC99";
messages.appendChild(item);
window.scrollTo(0, document.body.scrollHeight);
});
socket.on("leave chat message", (msg) => {
var item = document.createElement("li");
item.textContent = msg;
item.style.color = "#FF4848";
messages.appendChild(item);
window.scrollTo(0, document.body.scrollHeight);
});
// Name valid event from server
socket.on("validation success", (msg) => {
loginContainer.style.display = "none";
user.displayName = msg;
invalidText.style.visibility = "hidden";
});
socket.on("validation failure", (msg) => {
// TODO: Do stuff like tell the user that their name is taken
invalidText.style.visibility = "visible";
});
</script>
</body>
</html>