-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
135 lines (121 loc) · 3.87 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
<html>
<head>
<title>IO chat</title>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://cdn.socket.io/socket.io-1.4.5.js"></script>
<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<style>
body{
margin-top: 30px;
}
#messaageArea{
display: none;
}
</style>
</head>
<body>
<div class="container">
<div class="row" id="userFormArea">
<div class="col-md-12">
<form id="userForm">
<div class="form-group">
<label>Enter username</label>
<textarea class="form-control" id="username" /></textarea>
<br />
<input type="submit" class="btn btn-primary" value="login" />
</div>
</form>
</div>
</div>
<div id="messaageArea"class="row">
<div class="col-md-4">
<div class="well">
<h2>Online users</h2>
<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>
<script>
$(document).ready(function() {
var socket = io.connect();
var $messageForm = $('#messageForm')
var $message = $('#message');
var $chat = $('#chat');
var $messaageArea = $('#messaageArea');
var $userFormArea = $('#userFormArea');
var $userForm = $('#userForm');
var $users = $('#users');
var $username = $('#username');
$messageForm.submit(function(e) {
e.preventDefault();
socket.emit('send message',$message.val());
$message.val(' ');
});
socket.on('new message', function(data){
$chat.append('<div class="well"><strong>'+ data.user+' </strong>: '+data.msg+'</div>');
// request permission on page load
if (Notification.permission !== "granted")
Notification.requestPermission();
var notification = new Notification('New message from '+data.user+'', {
//icon: SERVER + '/images/so_icon.png',
body:data.msg,
});
});
$userForm.submit(function(e) {
e.preventDefault();
socket.emit('new user',$username.val(),function(data){
if(data){
$userFormArea.hide();
$messaageArea.show();
}
});
$username.val(' ');
});
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);
});
// function notifyMe(data) {
// if (!Notification) {
// alert('Desktop notifications not available in your browser. Try Chromium.');
// return;
// }
// if (Notification.permission !== "granted")
// Notification.requestPermission();
// else {
// var notification = new Notification('New message', {
// //icon: SERVER + '/images/so_icon.png',
// //body: data.message,
// body:"hiii",
// });
// // Open and active current chat window
// // notification.onclick = function () {
// // chatWindow.focus();
// // };
// }
// }
});
</script>
</body>
</html>