-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.html
executable file
·243 lines (192 loc) · 7.3 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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
<meta name="description" content="" />
<meta name="author" content="" />
<!--[if IE]>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<![endif]-->
<title>REALTiME CHAT RooMs</title>
<!-- BOOTSTRAP CORE STYLE CSS -->
<link href="css/bootstrap.css" rel="stylesheet" />
<script type="text/javascript" src="js/jquery.js"></script>
<style>
.board{
height: 32em;
overflow: auto;
}
.servernote{
padding-top: 5px;
padding-bottom: 5px;
}
</style>
</head>
<body>
<div class="navbar-wrapper">
<div class="container">
<nav class="navbar navbar-default navbar-static-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="">Instant Chat</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="#">Room Manarola</a></li>
<li><a href="#">Room Amalfi</a></li>
<li><a href="#">Room Garda</a></li>
</ul>
</div>
</div>
</nav>
</div>
</div>
<div class="container">
<div class="col-md-3">
<div class="panel panel-primary">
<div class="panel-heading">
My Chat Name
</div>
<div class="panel-body">
<div class="col-lg-12">
<div class="input-group">
<input id="myName" value="anonymous" type="text" class="form-control">
<span class="input-group-btn">
<button id="confirmName" class="btn btn-default" type="button"><span class="glyphicon glyphicon-ok"></span></button>
</span>
</div><!-- /input-group -->
</div><!-- /.col-lg-6 -->
</div>
</div>
<div class="panel panel-primary">
<div class="panel-heading">
ONLINE USERS
</div>
<div class="panel-body">
<ul class="media-list connected-list">
<li>
<span class="glyphicon glyphicon-user"> </span> Alex Deo
<small class="text-muted">joined at ...</small>
</li>
</ul>
<hr />
</div>
</div>
</div>
<div class="col-md-8">
<div class="panel panel-info">
<div class="panel-heading">
INSTANT MESSAGE BOARD
</div>
<div class="panel-body board-wrapper">
<ul class="media-list board">
<li class="maxtes">
<div>
<span class="glyphicon glyphicon-user"> </span>
<span class="text-muted">Admin | Announcement</span>
</div>
<div class="well" >
Welcome to the istant message chat room, this web app is implemented by Node.js, Socket.io, Express, Bootstrap, jQuery and PHP.
</div>
</li>
</ul>
</div>
<div class="panel-footer">
<form class="input-group">
<input id="m" type="text" class="form-control" placeholder="Enter Message" />
<span class="input-group-btn">
<input type="submit" class="btn btn-info send" type="button">SEND</input>
</span>
</form>
</div>
</div>
</div>
</div>
<script type="text/javascript" src="js/script.js"></script>
<script src="node_modules/socket.io/node_modules/socket.io-client/socket.io.js"></script>
<script>
var socket = io.connect( 'http://localhost:8080' );
var cache = {"Room Manarola":[],"Room Amalfi":[],"Room Garda":[]};
var currentRoom = 'Room Manarola';
// socket.emit('chat message', 'new user connected ... ');
//send message
$('form').submit(function(){
socket.emit('chat message', $('#m').val());
$('#m').val('');
// Ajax call for saving datas
// $.ajax({
// url: "./ajax/insertNewMessage.php",
// type: "POST",
// data: { name: nameVal, message: msg },
// success: function(data) {
// }
// });
return false;
});
// send change name
$('#confirmName').click(function(){
var name = $("#myName").val();
socket.emit('username', name);
})
// send switch room
$(".nav li").click(function(){
// nav bar item
$(".nav li").removeClass("active");
$(this).addClass("active");
var roomName = $(this).find("a").text();
socket.emit('room', roomName);
currentRoom =roomName;
//display old messages
$('.board li').remove();
var oldmsg = cache[currentRoom];
for (var i = 0; i < oldmsg.length; i++) {
var msg = oldmsg[i];
var msghtm = "<li><div><span class='glyphicon glyphicon-user'></span> <span class='text-muted'>"+
""+msg.name+" | "+msg.time+"</span></div>"+
"<div class='well'>"+msg.message+"</div></li>";
$('.board').append(msghtm);
};
})
// Receives Chat Message
socket.on('chat message', function(msg){
//display msg
var msghtm = "<li><div><span class='glyphicon glyphicon-user'></span> <span class='text-muted'>"+
""+msg.name+" | "+msg.time+"</span></div>"+
"<div class='well'>"+msg.message+"</div></li>";
$('.board').append(msghtm);
// keep in windon buttom
$(".board").animate({scrollTop:400000},1);
// update cache
cache[currentRoom].push(msg);
});
// Receives SERVER FEED
socket.on('SERVER FEED', function(msg){
// var msghtm = "<li><div><span class='glyphicon glyphicon-user'></span> <span class='text-muted'>"+
// "SERVER </span></div>"+
// "<div class='well'>"+msg+"</div></li>";
var msghtm = "<li><div class='servernote'><span class='glyphicon glyphicon-bell'></span> <span class='text-muted'>"+
"SERVER: "+msg+"</span></div></li>";
$('.board').append(msghtm);
$(".board").animate({scrollTop:400000},1);
});
// Receives User list
socket.on('CONNECTED USERS', function(arr){
$(".connected-list li").remove();
for (var i = 0; i < arr.length ; i++) {
var user = arr[i];
var name = user['name'];
var time = user['time'];
var tmp = "<li> <span class='glyphicon glyphicon-user'> </span> "+name+" <small class='text-muted'>joined at "+time+"</small> </li>";
$(".connected-list").append(tmp);
};
});
</script>
</body>
</html>