-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat.js
210 lines (179 loc) · 6.87 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
/*
* (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
*/
//import index from "./index";
var socket = io();
var allChatMessages = [];
var chatNotificationCount = [];
var myUser = {};
var myFriend = {};
//var index = require('./index');
//var person = index.name;
//var login = require('./index');
//var login = index.
// firebase.auth().onAuthStateChanged(function(user) {
// if (user) {
// // User is signed in.
// } else {
// // No user is signed in.
// }
// });
// Document Ready function called automatically on page load
$(document).ready(function(){
loginMe();
});
// Function to ask user to supply his/her name before entering a chatbox
function loginMe() {
var person = prompt("Please enter a username:", "");//user;
var name = person;
if (/([^\s])/.test(person) && person != null && person != "") {
$('#user').val(name);
socket.emit('newUser', name);
document.title = name;
} else {
location.reload();
}
// // $('#user').val(person);
// // socket.emit('newUser', person);
// // document.title = user;
//socket.emit('newUser', myUser.id)
// login();
// // var user = index.name;
// // var name = user.displayName;
// console.log(name);
// $('#user').val(name);
// socket.emit('newUser', name);
// document.title = name;
}
// Function to be called when sent a message from chatbox
function submitfunction() {
var message = {};
text = $('#m').val();
if(text != '') {
message.text = text;
message.sender = myUser.id;
message.receiver = myFriend.id;
$('#messages').append('<li class="chatMessageRight">' + message.text + '</li>');
if(allChatMessages[myFriend.id] != undefined) {
allChatMessages[myFriend.id].push(message);
} else {
allChatMessages[myFriend.id] = new Array(message);
}
socket.emit('chatMessage', message);
}
$('#m').val('').focus();
return false;
}
// 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) {
$('#messages').html('');
messages.forEach(function(message){
var cssClass = (message.sender == myUser.id) ? 'chatMessageRight' : 'chatMessageLeft';
$('#messages').append('<li class="' + cssClass + '">' + message.text + '</li>');
});
}
// Append a single chant message to the chatbox
function appendChatMessage(message) {
if(message.receiver == myUser.id && message.sender == myFriend.id) {
playNewMessageAudio();
var cssClass = (message.sender == myUser.id) ? 'chatMessageRight' : 'chatMessageLeft';
$('#messages').append('<li class="' + cssClass + '">' + message.text + '</li>');
} 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('');
$('#m').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('');
}
}
// ############# 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 = '';
if(onlineUsers.length == 2) {
onlineUsers.forEach(function(user){
if(myUser.id != user.id){
myFriend.id = user.id;
myFriend.name = user.name;
$('#form').show();
$('#messages').show();
}
});
}
onlineUsers.forEach(function(user){
if(user.id != myUser.id) {
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){
appendChatMessage(message);
});
// Listen to userIsDisconnected event to remove its chat history from chatbox
socket.on('userIsDisconnected', function(userId){
delete allChatMessages[userId];
$('#form').hide();
$('#messages').hide();
});