-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
107 lines (89 loc) · 3.51 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 name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://code.jquery.com/jquery-3.4.1.js"
integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU="
crossorigin="anonymous"></script>
<script type="text/javascript" src="http://localhost:3333/socket.io/socket.io.js"></script> <!-- Leads to a script online -->
<link rel="stylesheet" type="text/css" href="css/index.css">
<title>Home</title>
</head>
<body>
<div id="login-area">
<div id="login-text">
<h1>Login page</h1>
</div>
<div id="login-form-area">
<form id="login-form">
<input id="username" type="text" placeholder="Username" autocomplete="off">
<input id="login-submit" type="submit" value="Login">
</form>
</div>
</div>
<div id="chat-area">
<div id="users-area">
<div id="online-users-text">
<h2>Online users:</h2>
</div>
<div id="online-users">
<ul id="users"></ul>
</div>
</div>
<div id="message-area">
<div id="display-message-area">
<ul id="messages"></ul>
</div>
<div id="message-form-area">
<form id="message-form">
<input id="message" type="text" placeholder="Type your message..." autocomplete="off">
<input id="message-submit" type="submit" value="Send">
</form>
</div>
</div>
</div>
</body>
<script type="text/javascript">
var socket = io();
function updateUsers(list) {
for(var i = 0; i < list.length; i++) {
$("#users").append("<li><b>" + list[i] + "</b></li>");
}
}
$("#login-form").submit(function(event) {
event.preventDefault();
if($("#username").val == "" || $("#username").val() == " ") {
alert("Invalid username!");
} else {
$username = $("#username").val()
$("#login-area").hide();
$("#chat-area").show();
$("#messages").html("");
socket.emit("has connected", $username);
}
});
$("#message-form").submit(function(event) {
event.preventDefault();
if ($("#message").val() == "" || $("#message").val() == " ") {
alert("You must enter a message");
} else {
socket.emit("new message", {username: $username, message: $("#message").val()});
$("#message").val("");
}
});
socket.on("has connected", function(data) {
$("#users").html("");
updateUsers(data.usersList);
$("#messages").append("<li><i><b>" + data.username + "</b> has connected </i></li>")
});
socket.on("has disconnected", function(data) {
$("#users").html("");
updateUsers(data.usersList);
$("#messages").append("<li><i><b>" + data.username + "</b> has disconnected </i></li>")
});
socket.on("new message", function(data) {
$("#messages").append("<li><b>" + data.username + "</b>: " + data.message + "</li>")
});
</script>
</html>