-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
177 lines (130 loc) · 4.47 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
<!doctype html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>Chat</title>
<link rel="stylesheet" type="text/css" href="main.css">
</head>
<body>
<div id="users">
<ul id="online-users">
<li><strong>Online Now</strong></li>
</ul>
</div>
<div id="messagebox">
<ul id="messages"></ul>
</div>
<form action="">
<input id="m" autocomplete="off" /><button>send</button>
</form>
<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
<script src="https://code.jquery.com/jquery-1.11.1.js"></script>
<script>
/*
** FUNCTIONS
*/
// Instantiate notifications
function notifyMe() {
// Check if the browser supports notifications
if (!("Notification" in window)) {
alert("This browser does not support desktop notification");
}
// Check whether notification permissions have already been granted
else if (Notification.permission === "granted") {
// If OK, create a notification
var notification = new Notification("Hi there!",{ body: 'You will receive notifications from now on!'});
}
// Otherwise, we need to ask the user for permission
else if (Notification.permission !== 'denied') {
Notification.requestPermission(function (permission) {
// If the user accepts, let's create a notification
if (permission === "granted") {
var notification = new Notification("Hi there!",{ body: 'You will receive notifications from now on!'});
}
});
}
}
var div = $('#messagebox');
// Scroll to bottom of message box
var scrollToBottom = function() {
div.scrollTop( div.get(0).scrollHeight );
}
// Call function on pageload
notifyMe();
var typing = false;
var timeout = undefined;
/*
** SERVER
*/
var server = io.connect('http://localhost:3000');
// On connect ask for name and display connected to chat
server.on('connect', function(data){
var nickname = prompt("what is your nickname?");
$('#messages').append($('<li>').text('Connected to chat as '+nickname+'.'));
server.emit('join', nickname);
scrollToBottom();
});
function timeoutFunction() {
typing = false;
server.emit("typing", false);
}
// Handle typing function
$("#m").keypress(function(){
if (typing === false && $("#m").is(":focus")) {
typing = true;
server.emit("typing", true);
} else {
clearTimeout(timeout);
timeout = setTimeout(timeoutFunction, 3000);
}
});
// Show 'typing...'
server.on("isTyping", function(data) {
if (data.isTyping) {
$(".user-"+data.person).append("<span class='user-"+data.person+"-typing'> (typing)</span>");
} else {
$(".user-"+data.person+"-typing").remove();
}
});
// Handle disconnect
server.on('disconnect', function(data){
server.emit('disconnect',nickname)
});
// Send messages to server and reset input field
$('form').submit(function(){
server.emit('message', $('#m').val());
$('#m').val('');
return false;
clearTimeout(timeout);
timeout = setTimeout(timeoutFunction, 0);
});
// Add message to list
server.on('message', function(msg){
$('#messages').append($('<li>').html('<span class="chat-date">'+msg.time+'</span><span class="chat-user">'+msg.user+'</span><span class="chat-message">'+msg.message+'</span>'));
scrollToBottom();
$(".user-"+msg.user+"-typing").remove();
clearTimeout(timeout);
timeout = setTimeout(timeoutFunction, 0);
});
// Show who joined
server.on('joined', function(name){
$('#messages').append($('<li>').html('<span class="notification">'+name+' joined the chat.</span>'));
scrollToBottom();
});
// Show notifications to group
server.on('notify', function(msg) {
var n = new Notification(msg.user, {icon: 'http://gurucul.com/wp-content/uploads/2015/01/default-user-icon-profile.png', body: msg.message});
setTimeout(n.close.bind(n), 4000);
});
// Show who's online
server.on('isOnline', function(name){
$('#online-users').append($('<li class="user-'+name+'">').text(name));
});
// Show who left
server.on('left', function(name){
$('#messages').append($('<li>').html('<span class="notification">'+name+' left the chat.</span>'));
$( ".user-"+name ).remove();
});
</script>
</body>
</html>