I. Overview:
use ASP. SignalR 2 To create a live chat application. Add SignalR to the MVC 5 application and create a chat view to send and display the message.
In the demo, the Learning SIGNALR development tasks include:
Add that SignalR Library to the MVC 5 application.
Create a hub and a Hao startup class to push content to the client.
Use the SignalR jQuery library in the Web page to send messages and display updates from the hub.
The following screen shot shows the completed chat application running in the browser.
2. Achieve:
Create an ASP. NET MVC 5 application, install the SignalR library, add and create a chat application.
1> in Visual Studio, create a C # ASP. NET Framework 4.5, named Signalrchat, and click OK.
2> in the new ASP. NET Project dialog box, select MVC and click Change Authentication
Note: If the application chooses a different authentication provider, it will create the Startup.cs class, where no authentication is chosen for all of us to create a startup class ourselves.
3> Install SIGNALR
Open the Tools | Library Package Manager | Package Manager console, and then run the following command. This step adds a set of script files to the project and an assembly reference that enables the SignalR feature.
Input: Install-package Microsoft.AspNet.SignalR
The installation is complete, and a file like this appears under the Scripts folder:
4> Create a startup class:
Create a class under the root directory named startup:
Using owin;using Microsoft.owin; [Assembly:owinstartup (typeof (Signalrchat.startup))]namespace signalrchat{public class Startup { public void Configuration (Iappbuilder app) { /* connection or hub wire up and configuration should go here
app. MAPSIGNALR ();}}}
5.> add an existing item to the project by adding the Hubs folder:
Right-click the Hubs folder, click Add | New project, select Visual C # | Web | So SignalR node in the installed pane, from the center pane, select the SignalR Hub Class (v2) and create the name ChatHub.cs.
To modify the code:
Using system;using system.web;using microsoft.aspnet.signalr;namespace signalrchat{public class Chathub:hub {public void Send (string name, String message) {//' call ' the Addnewmessagetopage method to update client S. Clients.All.addNewMessageToPage (name, message);}}}
6>. Edit HomeController class discovery in Controllers/homecontroller.cs, add the following methods to the class. This method returns the view of the chat that you will create in a later step.
Public ActionResult Chat () { return View ();}
7>. On the Chat () method, right-click > Add View Page
Modify the code to:
@{viewbag.title = "Chat";} F5 run the project can achieve the above effect, you can have the user in real-time synchronization chat.
"Creating live Chat applications using Signalr+asp.net"