-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
101 lines (80 loc) · 2.37 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
<html>
<head>
<title>IO CHAT</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.js"></script>
<script src="/socket.io/socket.io.js"></script>
<style>
body {
margin-top: 30px;
margin-left:20px;
}
</style>
</head>
<body>
<h2>REAL TIME CHAT</h2>
<div id="userFormArea">
<form action="" id="userForm">
<div >
<label>Enter Username</label>
<input class="form-control" id="username" />
<br/>
<input type="submit" class="btn btn-primary" value="Login" />
</div>
</form>
</div>
<div id="messageArea" class="row">
<h3>Online Users</h3>
<ul id="users" class="list-group">
</ul>
</div>
<div class="chat" id="chat">
</div>
<form action="" id="messageForm">
<div>
<label>Enter Message</label>
<br/>
<textarea id="message"></textarea>
<br/>
<input type="submit" class="btn btn-primary" value="Send Message" />
</div>
</form>
<script>
var socket = io.connect();
var $userForm = $("#userForm"),
$userFormArea = $("#userFormArea"),
$messageArea = $("#messageArea"),
$username = $("#username"),
$users = $("#users"),
$messageForm = $("#messageForm"),
$chat = $("#chat"),
$message = $("#message");
$userForm.submit(function (e) {
e.preventDefault();
socket.emit("new user", $username.val(), function (data){
if (data){
$userFormArea.hide();
$messageArea.show();
}
});
$username.val(""); // clear
});
// Send the message to the server
$messageForm.submit(function (e) {
e.preventDefault();
socket.emit("send message", $message.val());
$message.val(""); // clear
});
socket.on("get users", function (data) {
var html = "";
for(var i = 0; i < data.length; i++) {
html += "<li class='list-group-item'>" + data[i] + "</li>";
}
$users.html(html);
});
socket.on("new message", function (data) {
$chat.append("<div class='well'><strong>" + data.user + ":</strong>" + data.msg + "</div>");
});
</script>
</body>
</html>