-
Notifications
You must be signed in to change notification settings - Fork 1
/
chat.js
293 lines (247 loc) · 9.48 KB
/
chat.js
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
* (C) 2018, All rights reserved. This software constitutes the trade secrets and confidential and proprietary information
* It is intended solely for use by Sandip Salunke. This code may not be copied or redistributed to third parties without
* prior written authorization from Sandip Salunke
*/
var socket = io();
var allChatMessages = [];
var chatNotificationCount = [];
var myUser = {};
var myFriend = {};
// Document Ready function called automatically on page load
$(document).ready(function () {
// Function call to initilize file uploader
initializeFileUploader();
// Function will be called when file is selected
fileIsSelected();
// Logic to set window height
var windowHeight = $(window).height();
$('.onlineUsersContainer').css('min-height', windowHeight).css('max-height', windowHeight);
$('.chatContainer').css('min-height', windowHeight);
// Function call to login user with custom name
loginMe();
});
// Function to initialize file uploader
function initializeFileUploader() {
$("#imgAttachment").click(function () {
$("#fileAttachment").click();
});
}
// Function to be called when file is selected in file uploader
function fileIsSelected() {
$('#fileAttachment').on('change', function () {
var file = this.files[0];
if (file.size > 5 * 1024 * 1024) {
alert('max upload size is 5 MB');
} else {
var reader = new FileReader();
reader.addEventListener("load", function () {
ajaxFileUpload(file.name, reader.result.split(',')[1]);
}, false);
if (file) {
reader.readAsDataURL(file);
}
}
});
}
// Function to hadle ajax file upload
function ajaxFileUpload(fileName, fileData) {
var message = {};
var formData = new FormData();
formData.append('fileName', fileName);
formData.append("fileToUpload", fileData);
$.ajax({
url: "/file-upload",
type: "POST",
data: formData,
contentType: false,
processData: false,
success: function (response) {
if (response.filePath) {
message.type = 'file';
message.text = response.filePath;
message.sender = myUser.id;
message.receiver = myFriend.id;
// Function call to send attached file to sender/seceiver in chatbox
appendSendersChatboxMessage(message);
} else {
throw 'File upload failed';
}
},
error: function (jqXHR, textStatus, errorMessage) {
alert('Error in sending attachment: ' + errorMessage); // Optional
}
});
}
// Function to ask user to supply his/her name before entering a chatbox
function loginMe() {
var person = prompt("Please enter your name:", "Sandip Salunke");
if (/([^\s])/.test(person) && person != null && person != "") {
//$('#user').val(person);
socket.emit('newUser', person);
document.title = person;
} else {
location.reload();
}
}
// Function to be called when sent a message from chatbox
function submitfunction() {
var message = {};
text = $('#txtChatMessage').val();
if (text != '') {
message.type = 'text';
message.text = text;
message.sender = myUser.id;
message.receiver = myFriend.id;
// Function call to send attached file to sender/seceiver in chatbox
appendSendersChatboxMessage(message);
}
$('#txtChatMessage').val('').focus();
}
// function to emit an even to notify friend that I am typing a message
function notifyTyping() {
socket.emit('notifyTyping', myUser, myFriend);
}
// Load all messages for the selected user
function loadChatBox(messages) {
$('#form').show();
$('#messages').html('').show();
messages.forEach(function (message) {
var cssClass = (message.sender == myUser.id) ? 'chatMessageRight' : 'chatMessageLeft';
// Function call to append receiver's chat message to chatBox
appendMessage(message, cssClass);
});
chatboxScrollBottom();
}
// Function to append chat application to senders/receivers chatBox
function appendMessage(message, cssClass) {
var htmlMessage = '';
var fileTypeRE = /\.(jpe?g|png|gif|bmp)$/i;
var messageTimestamp = new Date().toLocaleString('en-US', { hour: 'numeric', minute: 'numeric', hour12: true });
var chatMessageTimestamp = '<label class="chatMessageTimestamp">' + messageTimestamp + '</label>';
if (message.type === 'text') {
htmlMessage = '<li class="' + cssClass + '">' + message.text + chatMessageTimestamp + '</li>';
} else if (message.type === 'file') {
if (fileTypeRE.test(message.text)) {
htmlMessage = '<li class="' + cssClass + '"><img class="chatImage" src="' + message.text + '" alt="" />' + chatMessageTimestamp + '</li>';
} else {
htmlMessage = '<li class="' + cssClass + '"><a href="' + message.text + '"><img class="chatImage" src="assets/images/if_Download_1031520.png" alt="Download File" /></a>' + chatMessageTimestamp + '</li>';
}
}
$('#messages').append(htmlMessage);
chatboxScrollBottom();
}
// Function to append a chat message to the senders chatBox
function appendSendersChatboxMessage(message) {
// Function call to append senders chat message to chatBox
appendMessage(message, 'chatMessageRight');
if (allChatMessages[myFriend.id] != undefined) {
allChatMessages[myFriend.id].push(message);
} else {
allChatMessages[myFriend.id] = new Array(message);
}
socket.emit('chatMessage', message);
}
// Append a single chant message to the receivers chatbox
function appendReceiversChatMessage(message) {
if (message.receiver == myUser.id && message.sender == myFriend.id) {
var cssClass = (message.sender == myUser.id) ? 'chatMessageRight' : 'chatMessageLeft';
// Function call to append receivers chat message to chatBox
appendMessage(message, cssClass);
// Function call to play notification sound
playNewMessageAudio();
} else {
playNewMessageNotificationAudio();
updateChatNotificationCount(message.sender);
}
if (allChatMessages[message.sender] != undefined) {
allChatMessages[message.sender].push(message);
} else {
allChatMessages[message.sender] = new Array(message);
}
}
// Function to play a audio when new message arrives on selected chatbox
function playNewMessageAudio() {
(new Audio('https://notificationsounds.com/soundfiles/8b16ebc056e613024c057be590b542eb/file-sounds-1113-unconvinced.mp3')).play();
}
// Function to play a audio when new message arrives on selected chatbox
function playNewMessageNotificationAudio() {
(new Audio('https://notificationsounds.com/soundfiles/dd458505749b2941217ddd59394240e8/file-sounds-1111-to-the-point.mp3')).play();
}
// Function to update chat notifocation count
function updateChatNotificationCount(userId) {
var count = (chatNotificationCount[userId] == undefined) ? 1 : chatNotificationCount[userId] + 1;
chatNotificationCount[userId] = count;
$('#' + userId + ' label.chatNotificationCount').html(count);
$('#' + userId + ' label.chatNotificationCount').show();
}
// Function to clear chat notifocation count to 0
function clearChatNotificationCount(userId) {
chatNotificationCount[userId] = 0;
$('#' + userId + ' label.chatNotificationCount').hide();
}
// Function to be called when a friend is selected from the list of online users
function selectUerChatBox(element, userId, userName) {
myFriend.id = userId;
myFriend.name = userName;
$('#form').show();
$('#messages').show();
$('#onlineUsers li').removeClass('active');
$(element).addClass('active');
$('#notifyTyping').text('');
$('#txtChatMessage').val('').focus();
// Reset chat message count to 0
clearChatNotificationCount(userId);
// load all chat message for selected user
if (allChatMessages[userId] != undefined) {
loadChatBox(allChatMessages[userId]);
} else {
$('#messages').html('');
}
}
function chatboxScrollBottom() {
$('#messages').animate({ scrollTop: $('#messages').prop('scrollHeight') });
}
// ############# Event listeners and emitters ###############
// Listen to newUser even to set client with the current user information
socket.on('newUser', function (newUser) {
myUser = newUser;
$('#myName').html(myUser.name);
});
// Listen to notifyTyping event to notify that the friend id typying a message
socket.on('notifyTyping', function (sender, recipient) {
if (myFriend.id == sender.id) {
$('#notifyTyping').text(sender.name + ' is typing ...');
}
setTimeout(function () { $('#notifyTyping').text(''); }, 5000);
});
// Listen to onlineUsers event to update the list of online users
socket.on('onlineUsers', function (onlineUsers) {
var usersList = '';
onlineUsers.forEach(function (user) {
if (user.id != myUser.id) {
if (onlineUsers.length == 2) {
myFriend.id = user.id;
myFriend.name = user.name;
$('#form').show();
$('#messages').show();
}
var activeClass = (user.id == myFriend.id) ? 'active' : '';
usersList += '<li id="' + user.id + '" class="' + activeClass + '" onclick="selectUerChatBox(this, \'' + user.id + '\', \'' + user.name + '\')"><a href="javascript:void(0)">' + user.name + '</a><label class="chatNotificationCount"></label></li>';
}
});
$('#onlineUsers').html(usersList);
});
// Listen to chantMessage event to receive a message sent by my friend
socket.on('chatMessage', function (message) {
// Function call to append a single chant message to the receivers chatbox
appendReceiversChatMessage(message);
});
// Listen to userIsDisconnected event to remove its chat history from chatbox
socket.on('userIsDisconnected', function (userId) {
delete allChatMessages[userId];
if (userId == myFriend.id) {
$('#form').hide();
$('#messages').html('').hide();
}
});