fork download
  1. $(function () {
  2. var showChatName = new Boolean(true);
  3. var previousUsername = '';
  4. var selectedUsername = 'Admin';
  5. var chatUsername = window.prompt("Enter Username:", "");
  6. //
  7. var usernameList = $('#showUsernames');
  8. var chatTitleName = $('#chat-title-name');
  9. var txtMsg = $('#txtmsg');
  10. var showMessage = $('#messages');
  11. //
  12. if (selectedUsername != chatUsername) {
  13.  
  14. }
  15.  
  16. txtMsg.focus();
  17. chatUsername = chatUsername.toLowerCase().replace(/\b[a-z]/g, function (letter) {
  18. return letter.toUpperCase();
  19. });
  20.  
  21. var chat = $.connection.chat; //calling the created class "Chat" (Works, if and only if it implements Hub class)
  22. //for removing the username who is disconnected from the server
  23. chat.client.leave = function () {
  24. // console.log(user + date);
  25. //calling the server side method "GetConnectedUsers"
  26. //The return value of the method is stored in a variable "users"
  27. chat.server.getConnectedUsers().done(function (users) {
  28. //clearing the div, to fill it again (over writing)
  29. usernameList.empty();
  30. console.log(users.length);
  31. //calling each username stored in a static dictionary on server side using for loop
  32. for (var i = 0; i < users.length; ++i) {
  33. usernameList.append('<li>' + users[i].Name + '</li>');
  34. }
  35. });
  36. };
  37.  
  38.  
  39.  
  40. //On key press "Enter"
  41. txtMsg.keypress(function (e) {
  42. console.log("keypress");
  43. if (e.which == 13) {
  44. //prevent default operations of the "Enter" Key
  45. e.preventDefault();
  46. //Comparing if the last message entered username is same as the current username
  47. //if the previous username and current username are same then dont show the username again.
  48. if ((!previousUsername.length == 0) && (previousUsername == chatUsername)) {
  49. showChatName = false;
  50. }
  51. else { showChatName = true; }
  52.  
  53. var message = txtMsg.val();
  54. if (message.replace(/\s/g, "").length !== 0) {
  55. //calls the server side method if and only if there is some valid info to send
  56. chat.server.send(chatUsername, selectedUsername, txtMsg.val(), showChatName);
  57. }
  58. //emptying the textbox once the message is sent to server
  59. txtMsg.val('');
  60.  
  61. }
  62. });
  63.  
  64. // Declare a function on the chat hub so the server can invoke it
  65. chat.client.addMessage = function (chatUsername, message, showChatName) {
  66. if (showChatName) {
  67. //To show Username with the typed message
  68. showMessage.append('<b>' + chatUsername + '</b>' + ':&nbsp;' + message + '</br>');
  69. }
  70. else {
  71. //To show only typed message, not the username
  72. showMessage.append('&nbsp;&nbsp;' + message + '</br>');
  73. }
  74. previousUsername = chatUsername;
  75. //To keep scroll always bottom
  76. showMessage.scrollTop(showMessage[0].scrollHeight);
  77. };
  78.  
  79. //Invoking the hub class
  80. $.connection.hub.start().done(function () {
  81. //calling the server side method just after invoking the hub class
  82. chat.server.joined(chatUsername);
  83. });
  84.  
  85. //Method to show the usernames of the connected users in a div of id -- showUsernames
  86. chat.client.joins = function () {
  87. //calling the server side method "GetConnectedUsers"
  88. //The return value of the method is stored in a variable "users"
  89. chat.server.getConnectedUsers().done(function (users) {
  90. //clearing the div, to fill it again (over writing)
  91. usernameList.empty();
  92. //calling each username stored in a static dictionary on server side using for loop
  93. for (var i = 0; i < users.length; ++i) {
  94. usernameList.append('<li>' + users[i].Name + '</li>');
  95. }
  96. });
  97. };
  98.  
  99. $('ul#showUsernames li').on('click', function () {
  100. selectedUsername = $(this).text();
  101. if (selectedUsername != chatUsername) {
  102. chatTitleName.text("Chat between " + chatUsername + " and " + selectedUsername);
  103. }
  104. });
  105. });
  106.  
Runtime error #stdin #stdout 0.35s 213888KB
stdin
Standard input is empty
stdout
Standard output is empty