banner



How To Build A Chat App C# Mvc

  • Updated date Apr 17, 2019
  • 35.7k
  • 0

SignalR can be easily integrated with ASP.NET web applications and it supports mobile and JavaScript too.

Introduction

SignalR is a web-based real-time bidirectional communication framework. It's very useful in terms of user-friendliness. While developing a chat application or broadcasting the message like notification popup, before SignalR came into the picture, we used to use the request/response technology where the server had to wait for the client's request. On the other hand, in SignalR, we can directly push the message to the clients.

It has three important transport mediums as mentioned below.

  1. Server-Sent Event.
  2. ForEver Frame.
  3. Long Polling.

Implementation

Step 1

Open Visual Studio and create a new ASP.NET web application.

Step 2

Select MVC and authentication mode as "No Authentication".

Step 3

Open Package Manager Console and type the following command to install the NuGet package. Alternatively, click Add >> New Item >> SingalR and the select SignalR Hub Class (v2) followed by a click on the OK button.

install-package Microsoft.AspNet.SignalR

ASP.NET MVC Chat Application By Using SignalR

ASP.NET MVC Chat Application By Using SignalR

ASP.NET MVC Chat Application By Using SignalR

Step 4

In Myhub1.cs class, add the following method.

  1. public void  Send( string  name, string  message)
  2.   {
  3.       Clients.All.addNewMessageToPage(name, message);
  4.   }

Step 5

In Solution Explorer, right click and add a new class with the name as "Startup.cs" and then, install "Microsoft.Owin" NuGet package.

Step 6

Open the HomeController.cs class and add the chat method.

Step 7

Create a new View called "Chat" under "Home" folder and delete the existing code from View and add the below code.

  1. @{
  2.     ViewBag.Title ="Chat" ;
  3. }
  4. <h2>Chat</h2>
  5. <script src="~/Scripts/jquery.signalR-2.1.0.min.js" ></script>
  6. <divclass = "container" >
  7.     <input type="text"  id= "message"  />
  8.     <input type="button"  id= "sendmessage"  value= "Send"  />
  9.     <input type="hidden"  id= "displayname"  />
  10.     <ul id="discussion" ></ul>
  11. </div>
  12. @section scripts {
  13.     <!--Script references. -->
  14.     <!--The jQuery library is required and is referenced bydefault in  _Layout.cshtml. -->
  15.     <!--Reference the SignalR library. -->
  16.     <script src="~/Scripts/jquery.signalR-2.1.0.min.js" ></script>
  17.     <!--Reference the autogenerated SignalR hub script. -->
  18.     <script src="~/signalr/hubs" ></script>
  19.     <!--SignalR script to update the chat page and send messages.-->
  20.     <script>
  21.         $(function  () {
  22. var  chat = $.connection.myHub1;
  23.             chat.client.addNewMessageToPage =function  (name, message) {
  24.                 $('#discussion' ).append( '<li><strong>'  + htmlEncode(name)
  25.                     +'</strong>: '  + htmlEncode(message) + '</li>' );
  26.             };
  27.             $('#displayname' ).val(prompt( 'Enter your name:' , '' ));
  28.             $('#message' ).focus();
  29.             $.connection.hub.start().done(function  () {
  30.                 $('#sendmessage' ).click( function  () {
  31.                     chat.server.send($('#displayname' ).val(), $( '#message' ).val());
  32.                     $('#message' ).val( '' ).focus();
  33.                 });
  34.             });
  35.         });
  36. function  htmlEncode(value) {
  37. var  encodedValue = $( '<div />' ).text(value).html();
  38. return  encodedValue;
  39.         }
  40.     </script>
  41. }

Step 8

Create a chat menu in the _Layout.cshtml file as shown below.

  1. <li>@Html.ActionLink( "Home" , "Chat" , "Chat" )</li>

ASP.NET MVC Chat Application By Using SignalR

ASP.NET MVC Chat Application By Using SignalR

Conclusion

Save all the files in Visual Studio and run the same. In the browser, the system will ask us to enter the name. Copy the http://localhost:62167/Home/Chat URL and put it into another browser tab.

That's all. Now, we can enjoy the chat.

Code Sample

Startup.cs

  1. using  Microsoft.Owin;
  2. using  Owin;
  3. [assembly: OwinStartup(typeof (SignalRChat.Startup))]
  4. namespace  SignalRChat
  5. {
  6. public class  Startup
  7.     {
  8. public void  Configuration(IAppBuilder app)
  9.         {
  10.             app.MapSignalR();
  11.         }
  12.     }
  13. }

ChatHub.cs

  1. public class  ChatHub : Hub
  2.     {
  3. public void  Send( string  name, string  message)
  4.         {
  5.             Clients.All.addNewMessageToPage(name, message);
  6.         }
  7.     }

Global.asax.cs

  1. public class  MvcApplication : System.Web.HttpApplication
  2.     {
  3. protected void  Application_Start()
  4.         {
  5.             AreaRegistration.RegisterAllAreas();
  6.             FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
  7.             RouteConfig.RegisterRoutes(RouteTable.Routes);
  8.             BundleConfig.RegisterBundles(BundleTable.Bundles);
  9.         }
  10.     }

How To Build A Chat App C# Mvc

Source: https://www.c-sharpcorner.com/article/asp-net-mvc-chat-application-by-using-signalr/

Posted by: brassyousit.blogspot.com

0 Response to "How To Build A Chat App C# Mvc"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel