harry code

harry code

  • NA
  • 124
  • 21.5k

Chat Application using SignalR not working

Jan 31 2019 8:24 AM
Hi,
 
I am trying to develop a simple chat application in ASP.Net
 
i am using signalR.
 
the messages are not getting printed on the screen
 
Below is the .aspx code
  1. <head id="Head1" runat="server">  
  2. <title></title>  
  3. </head>  
  4. <body>  
  5. <form id="frmHome" runat="server">  
  6. <div>  
  7. <ul id="ulInbox">  
  8. <li><span id="lblName">Name:</span> <span id="lblMessage">Message</span></li>  
  9. </ul>  
  10. <ul>  
  11. <li>  
  12. <input type="text" id="txtMessage" value="" />  
  13. <input type="button" id="btnSend" value="Send" />  
  14. <input type="hidden" id="txtName" />  
  15. </li>  
  16. </ul>  
  17. </div>  
  18. </form>  
  19. <script src="http://code.jquery.com/jquery-1.8.2.min.js" type="text/javascript"></script>  
  20. <script src="Scripts/jquery.signalR-1.0.1.min.js" type="text/javascript"></script>  
  21. <script src="signalr/hubs" type="text/javascript"></script>  
  22. <script type="text/javascript">  
  23. $(function () {  
  24. // Proxy created on the fly  
  25. var chat = $.connection.chatHub;  
  26. // Get the user name and store it to prepend to messages.  
  27. $('#txtName').val(prompt('Enter your name:'''));  
  28. // Declare a function on the chat hub so the server can invoke it  
  29. chat.client.sendMessage = function (name, message) {  
  30. var username = $('<div />').text(name).html();  
  31. var chatMessage = $('<div />').text(message).html();  
  32. $('#ulInbox').append('<li>' + username + ':  ' + chatMessage + '</li>');  
  33. };  
  34. // Start the connection  
  35. $.connection.hub.start().done(function ()  
  36. {  
  37. $('#btnSend').click(function ()  
  38. {  
  39. // Call the chat method on the server  
  40. chat.server.send($('#txtName').val(), $('#txtMessage').val());  
  41. $('#txtMessage').val('');  
  42. });  
  43. });  
  44. });  
  45. </script>  
  46. </body>  
  47. </html>  
This method should be called....but is is not getting called
  1. public class ChatHub : Hub  
  2. {  
  3. public void Send(string name, string message)  
  4. {  
  5. Clients.All.sendMessage(name, message);  
  6. }  
  7. }  
  8. public class Global : System.Web.HttpApplication  
  9. {  
  10. void Application_Start(object sender, EventArgs e)  
  11. {  
  12. // Code that runs on application startup  
  13. RouteTable.Routes.MapHubs();  
  14. }  
  15. }  
Thank you

Answers (3)