$(function () {
var showChatName = new Boolean(true);
var previousUsername = '';
var selectedUsername = 'Admin';
var chatUsername = window.prompt("Enter Username:", "");
//
var usernameList = $('#showUsernames');
var chatTitleName = $('#chat-title-name');
var txtMsg = $('#txtmsg');
var showMessage = $('#messages');
//
if (selectedUsername != chatUsername) {
}
txtMsg.focus();
chatUsername = chatUsername.toLowerCase().replace(/\b[a-z]/g, function (letter) {
return letter.toUpperCase();
});
var chat = $.connection.chat; //calling the created class "Chat" (Works, if and only if it implements Hub class)
//for removing the username who is disconnected from the server
chat.client.leave = function () {
// console.log(user + date);
//calling the server side method "GetConnectedUsers"
//The return value of the method is stored in a variable "users"
chat.server.getConnectedUsers().done(function (users) {
//clearing the div, to fill it again (over writing)
usernameList.empty();
console.log(users.length);
//calling each username stored in a static dictionary on server side using for loop
for (var i = 0; i < users.length; ++i) {
usernameList.append('<li>' + users[i].Name + '</li>');
}
});
};
//On key press "Enter"
txtMsg.keypress(function (e) {
console.log("keypress");
if (e.which == 13) {
//prevent default operations of the "Enter" Key
e.preventDefault();
//Comparing if the last message entered username is same as the current username
//if the previous username and current username are same then dont show the username again.
if ((!previousUsername.length == 0) && (previousUsername == chatUsername)) {
showChatName = false;
}
else { showChatName = true; }
var message = txtMsg.val();
if (message.replace(/\s/g, "").length !== 0) {
//calls the server side method if and only if there is some valid info to send
chat.server.send(chatUsername, selectedUsername, txtMsg.val(), showChatName);
}
//emptying the textbox once the message is sent to server
txtMsg.val('');
}
});
// Declare a function on the chat hub so the server can invoke it
chat.client.addMessage = function (chatUsername, message, showChatName) {
if (showChatName) {
//To show Username with the typed message
showMessage.append('<b>' + chatUsername + '</b>' + ': ' + message + '</br>');
}
else {
//To show only typed message, not the username
showMessage.append(' ' + message + '</br>');
}
previousUsername = chatUsername;
//To keep scroll always bottom
showMessage.scrollTop(showMessage[0].scrollHeight);
};
//Invoking the hub class
$.connection.hub.start().done(function () {
//calling the server side method just after invoking the hub class
chat.server.joined(chatUsername);
});
//Method to show the usernames of the connected users in a div of id -- showUsernames
chat.client.joins = function () {
//calling the server side method "GetConnectedUsers"
//The return value of the method is stored in a variable "users"
chat.server.getConnectedUsers().done(function (users) {
//clearing the div, to fill it again (over writing)
usernameList.empty();
//calling each username stored in a static dictionary on server side using for loop
for (var i = 0; i < users.length; ++i) {
usernameList.append('<li>' + users[i].Name + '</li>');
}
});
};
$('ul#showUsernames li').on('click', function () {
selectedUsername = $(this).text();
if (selectedUsername != chatUsername) {
chatTitleName.text("Chat between " + chatUsername + " and " + selectedUsername);
}
});
});