2020-03-08 14:51:43 +05:30
|
|
|
$(function(){
|
|
|
|
$(document).ready(function(){
|
Refactors logic for chat user list and scroll
- Refactors server side code to use instance variable instead of
background thread to generate a list of connected users
- Send this user list anytime any change is made to the list. It can
be: join, update username, disconnect
- In js, render the entire user list everytime it is received.
- Scroll to the bottom of the chat, everytime the current user
sends a message
- Else, if already at the bottom of the chat, scroll to the bottom
after appending incoming status or chat message. But if the user
is scrolled up in the chat window, then do not scroll to the bottom
- When refreshed or close tab is clicked, default browser warning is
shown.
- On receiving disconnect, the browser removes user from room.
- If refreshed, it is shown as if the user left and joined again.
2020-05-04 03:47:13 +05:30
|
|
|
var socket = io.connect('http://' + document.domain + ':' + location.port + '/chat');
|
|
|
|
|
|
|
|
// Store current username received from app context
|
2020-03-12 14:54:48 +05:30
|
|
|
var current_username = $('#username').val();
|
Refactors logic for chat user list and scroll
- Refactors server side code to use instance variable instead of
background thread to generate a list of connected users
- Send this user list anytime any change is made to the list. It can
be: join, update username, disconnect
- In js, render the entire user list everytime it is received.
- Scroll to the bottom of the chat, everytime the current user
sends a message
- Else, if already at the bottom of the chat, scroll to the bottom
after appending incoming status or chat message. But if the user
is scrolled up in the chat window, then do not scroll to the bottom
- When refreshed or close tab is clicked, default browser warning is
shown.
- On receiving disconnect, the browser removes user from room.
- If refreshed, it is shown as if the user left and joined again.
2020-05-04 03:47:13 +05:30
|
|
|
|
|
|
|
// On browser connect, emit a socket event to be added to
|
|
|
|
// room and assigned random username
|
2020-03-08 14:51:43 +05:30
|
|
|
socket.on('connect', function() {
|
|
|
|
socket.emit('joined', {});
|
|
|
|
});
|
Refactors logic for chat user list and scroll
- Refactors server side code to use instance variable instead of
background thread to generate a list of connected users
- Send this user list anytime any change is made to the list. It can
be: join, update username, disconnect
- In js, render the entire user list everytime it is received.
- Scroll to the bottom of the chat, everytime the current user
sends a message
- Else, if already at the bottom of the chat, scroll to the bottom
after appending incoming status or chat message. But if the user
is scrolled up in the chat window, then do not scroll to the bottom
- When refreshed or close tab is clicked, default browser warning is
shown.
- On receiving disconnect, the browser removes user from room.
- If refreshed, it is shown as if the user left and joined again.
2020-05-04 03:47:13 +05:30
|
|
|
|
|
|
|
// Triggered on any status change by any user, such as some
|
|
|
|
// user joined, or changed username, or left, etc.
|
2020-03-08 14:51:43 +05:30
|
|
|
socket.on('status', function(data) {
|
Refactors logic for chat user list and scroll
- Refactors server side code to use instance variable instead of
background thread to generate a list of connected users
- Send this user list anytime any change is made to the list. It can
be: join, update username, disconnect
- In js, render the entire user list everytime it is received.
- Scroll to the bottom of the chat, everytime the current user
sends a message
- Else, if already at the bottom of the chat, scroll to the bottom
after appending incoming status or chat message. But if the user
is scrolled up in the chat window, then do not scroll to the bottom
- When refreshed or close tab is clicked, default browser warning is
shown.
- On receiving disconnect, the browser removes user from room.
- If refreshed, it is shown as if the user left and joined again.
2020-05-04 03:47:13 +05:30
|
|
|
addMessageToRoom(data, current_username, 'status');
|
2020-03-08 14:51:43 +05:30
|
|
|
});
|
Refactors logic for chat user list and scroll
- Refactors server side code to use instance variable instead of
background thread to generate a list of connected users
- Send this user list anytime any change is made to the list. It can
be: join, update username, disconnect
- In js, render the entire user list everytime it is received.
- Scroll to the bottom of the chat, everytime the current user
sends a message
- Else, if already at the bottom of the chat, scroll to the bottom
after appending incoming status or chat message. But if the user
is scrolled up in the chat window, then do not scroll to the bottom
- When refreshed or close tab is clicked, default browser warning is
shown.
- On receiving disconnect, the browser removes user from room.
- If refreshed, it is shown as if the user left and joined again.
2020-05-04 03:47:13 +05:30
|
|
|
|
|
|
|
// Triggered when message is received from a user. Even when sent
|
|
|
|
// by self, it get triggered after the server sends back the emit.
|
2020-03-08 14:51:43 +05:30
|
|
|
socket.on('message', function(data) {
|
Refactors logic for chat user list and scroll
- Refactors server side code to use instance variable instead of
background thread to generate a list of connected users
- Send this user list anytime any change is made to the list. It can
be: join, update username, disconnect
- In js, render the entire user list everytime it is received.
- Scroll to the bottom of the chat, everytime the current user
sends a message
- Else, if already at the bottom of the chat, scroll to the bottom
after appending incoming status or chat message. But if the user
is scrolled up in the chat window, then do not scroll to the bottom
- When refreshed or close tab is clicked, default browser warning is
shown.
- On receiving disconnect, the browser removes user from room.
- If refreshed, it is shown as if the user left and joined again.
2020-05-04 03:47:13 +05:30
|
|
|
addMessageToRoom(data, current_username, 'chat');
|
2020-03-08 14:51:43 +05:30
|
|
|
});
|
Refactors logic for chat user list and scroll
- Refactors server side code to use instance variable instead of
background thread to generate a list of connected users
- Send this user list anytime any change is made to the list. It can
be: join, update username, disconnect
- In js, render the entire user list everytime it is received.
- Scroll to the bottom of the chat, everytime the current user
sends a message
- Else, if already at the bottom of the chat, scroll to the bottom
after appending incoming status or chat message. But if the user
is scrolled up in the chat window, then do not scroll to the bottom
- When refreshed or close tab is clicked, default browser warning is
shown.
- On receiving disconnect, the browser removes user from room.
- If refreshed, it is shown as if the user left and joined again.
2020-05-04 03:47:13 +05:30
|
|
|
|
|
|
|
// Trigger new message on enter or click of send message button.
|
2020-03-08 14:51:43 +05:30
|
|
|
$('#new-message').on('keypress', function(e) {
|
|
|
|
var code = e.keyCode || e.which;
|
|
|
|
if (code == 13) {
|
|
|
|
emitMessage(socket);
|
|
|
|
}
|
|
|
|
});
|
Refactors logic for chat user list and scroll
- Refactors server side code to use instance variable instead of
background thread to generate a list of connected users
- Send this user list anytime any change is made to the list. It can
be: join, update username, disconnect
- In js, render the entire user list everytime it is received.
- Scroll to the bottom of the chat, everytime the current user
sends a message
- Else, if already at the bottom of the chat, scroll to the bottom
after appending incoming status or chat message. But if the user
is scrolled up in the chat window, then do not scroll to the bottom
- When refreshed or close tab is clicked, default browser warning is
shown.
- On receiving disconnect, the browser removes user from room.
- If refreshed, it is shown as if the user left and joined again.
2020-05-04 03:47:13 +05:30
|
|
|
$('#send-button').on('click', function(e) {
|
|
|
|
emitMessage(socket);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Update username
|
2020-03-12 14:54:48 +05:30
|
|
|
$('#update-username').on('click', function() {
|
|
|
|
var username = $('#username').val();
|
Refactors logic for chat user list and scroll
- Refactors server side code to use instance variable instead of
background thread to generate a list of connected users
- Send this user list anytime any change is made to the list. It can
be: join, update username, disconnect
- In js, render the entire user list everytime it is received.
- Scroll to the bottom of the chat, everytime the current user
sends a message
- Else, if already at the bottom of the chat, scroll to the bottom
after appending incoming status or chat message. But if the user
is scrolled up in the chat window, then do not scroll to the bottom
- When refreshed or close tab is clicked, default browser warning is
shown.
- On receiving disconnect, the browser removes user from room.
- If refreshed, it is shown as if the user left and joined again.
2020-05-04 03:47:13 +05:30
|
|
|
current_username = username;
|
2020-03-12 14:54:48 +05:30
|
|
|
socket.emit('update_username', {username: username});
|
|
|
|
});
|
Refactors logic for chat user list and scroll
- Refactors server side code to use instance variable instead of
background thread to generate a list of connected users
- Send this user list anytime any change is made to the list. It can
be: join, update username, disconnect
- In js, render the entire user list everytime it is received.
- Scroll to the bottom of the chat, everytime the current user
sends a message
- Else, if already at the bottom of the chat, scroll to the bottom
after appending incoming status or chat message. But if the user
is scrolled up in the chat window, then do not scroll to the bottom
- When refreshed or close tab is clicked, default browser warning is
shown.
- On receiving disconnect, the browser removes user from room.
- If refreshed, it is shown as if the user left and joined again.
2020-05-04 03:47:13 +05:30
|
|
|
|
|
|
|
// Show warning of losing data
|
|
|
|
$(window).on('beforeunload', function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.returnValue = '';
|
|
|
|
return '';
|
|
|
|
});
|
2020-03-08 14:51:43 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
|
Refactors logic for chat user list and scroll
- Refactors server side code to use instance variable instead of
background thread to generate a list of connected users
- Send this user list anytime any change is made to the list. It can
be: join, update username, disconnect
- In js, render the entire user list everytime it is received.
- Scroll to the bottom of the chat, everytime the current user
sends a message
- Else, if already at the bottom of the chat, scroll to the bottom
after appending incoming status or chat message. But if the user
is scrolled up in the chat window, then do not scroll to the bottom
- When refreshed or close tab is clicked, default browser warning is
shown.
- On receiving disconnect, the browser removes user from room.
- If refreshed, it is shown as if the user left and joined again.
2020-05-04 03:47:13 +05:30
|
|
|
var addMessageToRoom = function(data, current_username, messageType) {
|
|
|
|
var scrollDiff = getScrollDiffBefore();
|
|
|
|
if (messageType === 'status') {
|
|
|
|
addStatusMessage(data.msg);
|
|
|
|
if (data.connected_users) {
|
|
|
|
addUserList(data.connected_users, current_username);
|
|
|
|
}
|
|
|
|
} else if (messageType === 'chat') {
|
|
|
|
addChatMessage(data.msg)
|
|
|
|
}
|
|
|
|
scrollBottomMaybe(scrollDiff);
|
|
|
|
}
|
|
|
|
|
2020-03-11 18:10:08 +05:30
|
|
|
var emitMessage = function(socket) {
|
2020-03-12 14:54:48 +05:30
|
|
|
var text = $('#new-message').val();
|
2020-03-08 14:51:43 +05:30
|
|
|
$('#new-message').val('');
|
Refactors logic for chat user list and scroll
- Refactors server side code to use instance variable instead of
background thread to generate a list of connected users
- Send this user list anytime any change is made to the list. It can
be: join, update username, disconnect
- In js, render the entire user list everytime it is received.
- Scroll to the bottom of the chat, everytime the current user
sends a message
- Else, if already at the bottom of the chat, scroll to the bottom
after appending incoming status or chat message. But if the user
is scrolled up in the chat window, then do not scroll to the bottom
- When refreshed or close tab is clicked, default browser warning is
shown.
- On receiving disconnect, the browser removes user from room.
- If refreshed, it is shown as if the user left and joined again.
2020-05-04 03:47:13 +05:30
|
|
|
$('#chat').scrollTop($('#chat')[0].scrollHeight);
|
2020-03-08 14:51:43 +05:30
|
|
|
socket.emit('text', {msg: text});
|
|
|
|
}
|
2020-03-11 18:10:08 +05:30
|
|
|
|
Refactors logic for chat user list and scroll
- Refactors server side code to use instance variable instead of
background thread to generate a list of connected users
- Send this user list anytime any change is made to the list. It can
be: join, update username, disconnect
- In js, render the entire user list everytime it is received.
- Scroll to the bottom of the chat, everytime the current user
sends a message
- Else, if already at the bottom of the chat, scroll to the bottom
after appending incoming status or chat message. But if the user
is scrolled up in the chat window, then do not scroll to the bottom
- When refreshed or close tab is clicked, default browser warning is
shown.
- On receiving disconnect, the browser removes user from room.
- If refreshed, it is shown as if the user left and joined again.
2020-05-04 03:47:13 +05:30
|
|
|
/************************************/
|
|
|
|
/********* Util Functions ***********/
|
|
|
|
/************************************/
|
|
|
|
|
|
|
|
var createUserListHTML = function(connected_users, current_user) {
|
|
|
|
var userListHTML = '';
|
|
|
|
connected_users.sort();
|
|
|
|
connected_users.forEach(function(username) {
|
|
|
|
if (username !== current_user) {
|
|
|
|
userListHTML += `<li>${sanitizeHTML(username)}</li>`;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return userListHTML;
|
|
|
|
}
|
|
|
|
|
|
|
|
var getScrollDiffBefore = function() {
|
|
|
|
return $('#chat').scrollTop() - ($('#chat')[0].scrollHeight - $('#chat')[0].offsetHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
var scrollBottomMaybe = function(scrollDiff) {
|
|
|
|
// Scrolls to bottom if the user is scrolled at bottom
|
|
|
|
// if the user has scrolled upp, it wont scroll at bottom.
|
|
|
|
// Note: when a user themselves send a message, it will still
|
|
|
|
// scroll to the bottom even if they had scrolled up before.
|
|
|
|
if (scrollDiff > 0) {
|
|
|
|
$('#chat').scrollTop($('#chat')[0].scrollHeight);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var addStatusMessage = function(message) {
|
|
|
|
$('#chat').append(
|
|
|
|
`<p><small><i>${sanitizeHTML(message)}</i></small></p>`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
var addChatMessage = function(message) {
|
|
|
|
$('#chat').append(`<p>${sanitizeHTML(message)}</p>`);
|
|
|
|
}
|
|
|
|
|
|
|
|
var addUserList = function(connected_users, current_username) {
|
|
|
|
$('#user-list').html(
|
|
|
|
createUserListHTML(
|
|
|
|
connected_users,
|
|
|
|
current_username
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-03-11 18:10:08 +05:30
|
|
|
var sanitizeHTML = function(str) {
|
|
|
|
var temp = document.createElement('span');
|
|
|
|
temp.textContent = str;
|
|
|
|
return temp.innerHTML;
|
|
|
|
};
|