SignalR can keep communication with the client and Server on real time. It`s a very wonderful independent tool for MVC development.
Here I just finished an example by myself . And try to deepen my understanding in it.
SignalR was supported by the windows server 2012 that under the IIS 8 and .NET Framework4.5 environment. In the backaround, it`s
support the WebSocket.
At first, just try to add the package by Nuget
1. Microsoft.AspNet.SignalR
2.Microsoft.AspNet.SignalR.Client
@{ ViewBag.Title = "Home Page";}
3. in view file support the last request as the source attacker.
You'd better bring the java script to your html file.
pasting
4, And a ProgressHub.cs as the following
pasting
using Microsoft.AspNet.SignalR;using Microsoft.AspNet.SignalR.Hubs;using System.Threading.Tasks;using System.Threading;namespace WebApplication1{ [HubName("progress")] public class ProgressHub : Hub { public int count = 1; public static void SendMessage(string msg, int count) { var hubContext = GlobalHost.ConnectionManager.GetHubContext(); hubContext.Clients.All.sendMessage(string.Format(msg), count); } public void GetCountAndMessage(string msg) { ProgressStart(); Clients.Caller.sendMessage(string.Format(msg), count); } private void ProgressStart() { Task.Run(() => { for (int i=1; i<=100; i++) { Thread.Sleep(500); SendMessage("処理中...", i); } }); } }}
5. Edit the Startup.cs file.
pasting
namespace WebApplication1{ public partial class Startup { public void Configuration(IAppBuilder app) { ConfigureAuth(app); app.MapSignalR(); // ← この行を追加 } }}
References
https://github.com/SignalR/SignalR/tree/dev/src/Microsoft.AspNet.SignalR.Client.Portable