-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.html
165 lines (134 loc) · 4.11 KB
/
chat.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<html>
<head>
<title> IO CHAT</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="http://code.jquery.com/jquery-latest.min.js"> </script>
<script src="/socket.io/socket.io.js"> </script>
<style>
body{
margin-top:30px;
}
#auth{
visibility: visible;
}
#chatroom{
visibility: hidden;
}
</style>
</head>
<body>
<div class = "container">
<div class = "row" id="chatroom">
<div class = "col-md-4">
<div class = "well">
<h3 id = "h33" style='color:red'> Online Users</h3>
<ul class = "list-group" id ="users"></ul>
</div>
</div>
<div class = "col-md-8">
<div class = "chat" id = "chat"> </div>
<form id = "messageForm">
<div class = "form-group">
<label> Enter Message</label>
<textarea class = "form-control" id = "message"> </textarea>
<br />
<input type = "submit" class = "btn btn-primary" value = "Send Message" />
</div>
</form>
</div>
</div>
<div class = "row" id="auth">
<div class = "well">
<form class="form-horizontal" id = "authForm">
<div class="form-group">
<label for="inputUsr" class="col-sm-3 control-label">Username :</label>
<div class="col-sm-3">
<input type="text" class="form-control" id="inputUsr" placeholder="User - Name">
</div>
</div>
<div class="form-group">
<label for="inputPSWD" class="col-sm-3 control-label">User ID :</label>
<div class="col-sm-3">
<input type="password" class="form-control" id="inputPSWD" placeholder="Provided ID">
</div>
</div>
<button type="submit" class="btn btn-default" style="margin-left:20%;width:20%;">Login To Room</button>
</form>
</div>
</div>
</div>
</form>
</div>
<script>
var room;
$(function(){
var socket = io.connect('/');
var $messageForm = $('#messageForm');
var $message = $('#message');
var $chat = $('#chat');
var $authForm = $('#authForm');
var $inputUsr = $('#inputUsr');
var $inputPSWD = $('#inputPSWD');
var $chatroom = $('#chatroom');
var $authentication =$('#auth')
$authForm.submit(function(e){
e.preventDefault();
var authDetails=
{
name:$inputUsr.val(),
hash:$inputPSWD.val()
};
socket.emit('verify user', authDetails);
$message.val('');
});
$messageForm.submit(function(e){
e.preventDefault();
socket.emit('send message', {msg:$message.val(),r:room} );
console.log();
$message.val('');
});
socket.on('new message', function(data){
if($inputUsr.val() === data.user)
$chat.append('<div class = "well well-sm">'+data.msg+ '</div>');
else
$chat.append('<div class = "well well-sm"> '+'<b>'+data.user+':'+'</b>' +data.msg+ '</div>');
});
socket.on('admin welcome', function(data){
$chat.append('<div class = "well well-sm"> <b><span style="color:red"> Admin:</span>' +data.msg+ '</b> </div>');
});
socket.on('Authenticated', function(data){
if(data.user === 'authenticated')
{
$authentication.hide();
room = data.room;
//same method was inactive in JQuery
document.getElementById("chatroom").style.visibility='visible';
}
else
{
alert('Wrong Input!');
$inputUsr.val('');
$inputPSWD.val('');
}
});
socket.on('User Disconnected', function(data){
$chat.append('<div class = "well well-bm"> <b><span style="color:red"> Admin:</span> User Disconnected: ' +data.usrname+ '</b> </div>');
});
socket.on('User Connect', function(data){
if($inputUsr.val() === data.usrname)
;
else
$chat.append('<div class = "well well-sm"> <b><span style="color:red"> Admin:</span> User Connected: ' +data.usrname+ '</b> </div>');
})
socket.on('get online_users', function(users){
$('#h33').html("Room: ["+room+"] Users");
$('#users').empty();
for(i=0;i<users.length;i++){
if(users[i].room === room)
$('#users').append('<li class="list-group-item">'+users[i].usr+'</li>');
}
});
});
</script>
</body>
</html>