Home  >  Article  >  Backend Development  >  Using SignalR to implement push function in Asp.NET MVC

Using SignalR to implement push function in Asp.NET MVC

高洛峰
高洛峰Original
2016-12-24 13:59:411610browse

1. Introduction

Signal is an html websocket framework supported by Microsoft that runs on the Dot NET platform. The main purpose of its appearance is to enable the server to actively push (Push) messages to the client page, so that the client does not have to resend the request or use polling technology to obtain the message.
You can visit its official website: https://github.com/SignalR/ for more information.

2. What is Asp.net SignalR?

Asp.net SignalR is a class library from Microsoft for realizing real-time communication. Under normal circumstances, SignalR will use JavaScript's long polling method to implement client and server communication. With the emergence of WebSockets in Html5, SignalR also supports WebSockets communication. In addition, the programs developed by SignalR are not only limited to being hosted in IIS, but can also be hosted in any application, including consoles, client programs, and Windows services. It also supports Mono, which means that it can be deployed cross-platform on Linux. in environment.

There are two types of objects inside SignalR:

Http Persistent Connection (Persistent Connection) object: used to solve the function of long-term connections. The client can also actively request data from the server, and the server does not need to implement too many details. It only needs to handle the five events provided in PersistentConnection: OnConnected, OnReconnected, OnReceived, OnError and OnDisconnect.
Hub (hub) object: used to solve the function of real-time information exchange. The server can use the URL to register one or more Hubs. As long as it is connected to this Hub, it can be shared with all clients and sent to the server. information, and the server can call the client's script.
SignalR encapsulates the entire information exchange. The client and server use JSON to communicate. All Hub information declared on the server will generate JavaScript and output it to the client. .NET relies on Proxy to generate proxy objects. Internally, Proxy converts JSON into objects.
Since SignalR is designed for real-time use, this determines its usage. The specific applicable scenarios are as follows:
Chat rooms, such as online customer service systems, IM systems, etc.
Real-time updates of stock prices
Message push service
Real-time push of character locations in the game
Currently, what my company is developing is online customer service system.

3. Implementation mechanism

SignalR’s implementation mechanism is similar to .NET WCF or Remoting, both are implemented using remote agents. In terms of specific use, there are two interfaces with different purposes: PersistentConnection and Hubs. PersistentConnection implements long-term Javascript polling (similar to Comet), and Hub is used to solve the problem of real-time information exchange. It uses Javascript to dynamically load Implemented by entering the execution method. SignalR encapsulates the entire connection and information exchange process very beautifully. The client and server all use JSON to exchange data.

The following is the entire process of using the Hubs interface:
1. Define the corresponding hub class on the server side;
2. Define the proxy class corresponding to the hub class on the client side;
3. Between the client and server side Establish a connection;
4. Then the client can call the method of the proxy object to call the server-side method, that is, send the request to the server;
5. After the server-side receives the request, it can target a certain/group of clients Send a message to one client or all clients (broadcast).

4. Use Asp.net SignalR to implement broadcast messages on the Web

Through the introduction in the second part, I believe you have a preliminary understanding of Asp.net SignalR. Next, we will use two examples to deepen your understanding. Understanding of the operating mechanism of SignalR. The first example is how to use SignalR to implement broadcast messages on the Web.
Use Visual Studio 2013, create an MVC project
Install the SignalR package through Nuget. Right-click the reference - "Select Manage Nuget Packages -" Enter SignalR in the window that appears to find the SignalR package for installation.
After successfully installing SignalR, the scripts of the SignalR library will be added to the Scripts folder. As shown in the figure below:

Asp.NET MVC中使用SignalR实现推送功能

Add a SignalR hub (v2) to the project and name it ServerHub.

Asp.NET MVC中使用SignalR实现推送功能

Fill in the following code into the ServerHub class you just created.

using System;
using Microsoft.AspNet.SignalR;
 
namespace SignalDemo
{
 public class ServerHub : Hub
 {
 private static readonly char[] Constant =
 {
 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
 'w', 'x', 'y', 'z',
 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
 'W', 'X', 'Y', 'Z'
 };
 
 /// 
 /// 供客户端调用的服务器端代码
 /// 
 /// 
 public void Send(string message)
 {
 var name = GenerateRandomName(4);
 
 // 调用所有客户端的sendMessage方法
 Clients.All.sendMessage(name, message);
 }
 
 /// 
 /// 产生随机用户名函数
 /// 
 /// 用户名长度
 /// 
 public static string GenerateRandomName(int length)
 {
 var newRandom = new System.Text.StringBuilder(62);
 var rd = new Random();
 for (var i = 0; i < length; i++)
 {
 newRandom.Append(Constant[rd.Next(62)]);
 }
 return newRandom.ToString();
 }
 }
}


Create a Startup class. If the authentication is not changed when you start creating the MVC project, this class will be added by default. If it already exists, there is no need to add it again. Update the Startup class according to the following code.

using Microsoft.Owin;
using Owin;
 
[assembly: OwinStartupAttribute(typeof(SignalDemo.Startup))]
namespace SignalDemo
{
 public partial class Startup
 {
 #region MyRegion
 public void Configuration(IAppBuilder app)
 {
 app.MapSignalR();
 ConfigureAuth(app);
 }
 #endregion
 }
}

Create a Chat Action method in the Home controller

using System.Web.Mvc;
 
namespace SignalDemo.Controllers
{
 public class HomeController : Controller
 {
 public ActionResult Chat()
 {
 return View();
 }
 }
}

Create a Chat view in the Home folder in the Views file. The view code is as follows:

@{
 ViewBag.Title = "Chat";
}
 

Chat

    @section scripts { }


    修改App_Start文件夹内的RoutConfig类,将Action方法默认设置为Chat

    using System.Web.Mvc;
    using System.Web.Routing;
     
    namespace SignalDemo
    {
     public class RouteConfig
     {
     public static void RegisterRoutes(RouteCollection routes)
     {
     routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
     routes.MapRoute(
     name: "Default",
     url: "{controller}/{action}/{id}",
     defaults: new { controller = "Home", action = "Chat", id = UrlParameter.Optional }
     );
     }
     }
    }

       

    到此,我们的例子就实现完成了,接下来我们先来看看运行效果,之后再来解释到底SignalR是如何来完成广播消息的。运行的运行结果如下。

    Asp.NET MVC中使用SignalR实现推送功能

    从运行结果,你可以发现,在任何一个窗口输入信息并发送,所有客户端将收到该消息。这样的效果在实际应用中很多,如QQ,一登录QQ的时候都会推送腾讯广告消息。

      看完了运行结果,接下来我们来分析下代码,进而来剖析下SignalR到底是如何工作的。

      按照B/S模式来看,运行程序的时候,Web页面就与SignalR的服务建立了连接,具体的建立连接的代码就是:$.connection.hub.start()。这句代码的作用就是与SignalR服务建立连接,后面的done函数表明建立连接成功后为发送按钮注册了一个click事件,当客户端输入内容点击发送按钮后,该Click事件将会触发,触发执行的操作为: chat.server.send($('#message').val())。这句代码表示调用服务端的send函数,而服务端的Send韩式又是调用所有客户端的sendMessage函数,而客户端中sendMessage函数就是将信息添加到对应的消息列表中。这样就实现了广播消息的功能了。

      看到这里,有人是否会有疑问,前面的实现都只用到了集线器对象,而没有用到持久连接对象。其实并不是如此,$.connection这句代码就是使用持久连接对象,当然你也可以在重新OnConnected方法来查看监控客户端的连接情况,更新的代码如下所示:

    public class ServerHub : Hub
     {
     private static readonly char[] Constant =
     {
     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
     'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
     'w', 'x', 'y', 'z',
     'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
     'W', 'X', 'Y', 'Z'
     };
     
     /// 
     /// 供客户端调用的服务器端代码
     /// 
     /// 
     public void Send(string message)
     {
     var name = GenerateRandomName(4);
     
     // 调用所有客户端的sendMessage方法
     Clients.All.sendMessage(name, message);
     }
     
     /// 
     /// 客户端连接的时候调用
     /// 
     /// 
     public override Task OnConnected()
     {
     Trace.WriteLine("客户端连接成功");
     return base.OnConnected();
     }
     
     /// 
     /// 产生随机用户名函数
     /// 
     /// 用户名长度
     /// 
     public static string GenerateRandomName(int length)
     {
     var newRandom = new System.Text.StringBuilder(62);
     var rd = new Random();
     for (var i = 0; i < length; i++)
     {
     newRandom.Append(Constant[rd.Next(62)]);
     }
     return newRandom.ToString();
     }
     }

       


    这样在运行页面的时候,将在输出窗口看到“客户端连接成功”字样。运行效果如下图所示:

    Asp.NET MVC中使用SignalR实现推送功能

    在第二部分介绍的时候说道,在服务端声明的所有Hub信息,都会生成JavaScript输出到客户端,为了验证这一点,可以在Chrome中F12来查看源码就明白了,具体如下图所示:

    Asp.NET MVC中使用SignalR实现推送功能

     看到上图,你也就明白了为什么Chat.cshtml页面需要引入"signalr/hubs"脚本库了吧。

     d9a5242a011f9643fec06048e19a4b77
     15f14217e8cb6d9b781ee5d30ab615942cacc6d41bbb37262a98f745aa00fbf0
     3e25afd4fb2f01a3380fa6d18193664d
    9c680aa54eba498674e6e28bcbb95f282cacc6d41bbb37262a98f745aa00fbf0

    五、在桌面程序中如何使用Asp.net SignalR

       上面部分介绍了SignalR在Asp.net MVC 中的实现,这部分将通过一个例子来看看SignalR在WPF或WinForm是如何使用的。其实这部分实现和Asp.net MVC中非常相似,主要不同在于,Asp.net MVC中的SignalR服务器寄宿在IIS中,而在WPF中应用,我们把SignalR寄宿在WPF客户端中。

    下面让我们看看SignalR服务端的实现。

    /// 
     /// 启动SignalR服务,将SignalR服务寄宿在WPF程序中
     /// 
     private void StartServer()
     {
     try
     {
     SignalR = WebApp.Start(ServerUri); // 启动SignalR服务
     }
     catch (TargetInvocationException)
     {
     WriteToConsole("一个服务已经运行在:" + ServerUri);
     // Dispatcher回调来设置UI控件状态
     this.Dispatcher.Invoke(() => ButtonStart.IsEnabled = true);
     return;
     }
     
     this.Dispatcher.Invoke(() => ButtonStop.IsEnabled = true);
     WriteToConsole("服务已经成功启动,地址为:" + ServerUri);
     }
     
    public class ChatHub : Hub
     {
     public void Send(string name, string message)
     {
     Clients.All.addMessage(name, message);
     }
     
     public override Task OnConnected()
     {
     //
     Application.Current.Dispatcher.Invoke(() =>
     ((MainWindow)Application.Current.MainWindow).WriteToConsole("客户端连接,连接ID是: " + Context.ConnectionId));
     
     return base.OnConnected();
     }
     
     public override Task OnDisconnected(bool stopCalled)
     {
     Application.Current.Dispatcher.Invoke(() =>
     ((MainWindow)Application.Current.MainWindow).WriteToConsole("客户端断开连接,连接ID是: " + Context.ConnectionId));
     
     return base.OnDisconnected(true);
     }
     }
     
     public class Startup
     {
     public void Configuration(IAppBuilder app)
     {
     // 有关如何配置应用程序的详细信息,请访问 http://go.microsoft.com/fwlink/?LinkID=316888
     // 允许CORS跨域
     //app.UseCors(CorsOptions.AllowAll);
     app.MapSignalR();
     }
     }

       


    通过上面的代码,我们SignalR服务端的实现就完成了,其实现逻辑与Asp.net MVC的代码类似。

    接下来,让我们看看,WPF客户端是如何连接和与服务器进行通信的。具体客户端的实现如下:

    public IHubProxy HubProxy { get; set; }
     const string ServerUri = "http://localhost:8888/signalr";
     public HubConnection Connection { get; set; }
     
     public MainWindow()
     {
     InitializeComponent();
     
     // 窗口启动时开始连接服务
     ConnectAsync();
     }
     
     /// 
     /// 发送消息
     /// 
     /// 
     /// 
     private void ButtonSend_Click(object sender, RoutedEventArgs e)
     {
     // 通过代理来调用服务端的Send方法
     // 服务端Send方法再调用客户端的AddMessage方法将消息输出到消息框中
     HubProxy.Invoke("Send", GenerateRandomName(4), TextBoxMessage.Text.Trim());
     
     TextBoxMessage.Text = String.Empty;
     TextBoxMessage.Focus();
     }
     
     private async void ConnectAsync()
     {
     Connection = new HubConnection(ServerUri);
     Connection.Closed += Connection_Closed;
     
     // 创建一个集线器代理对象
     HubProxy = Connection.CreateHubProxy("ChatHub");
     
     // 供服务端调用,将消息输出到消息列表框中
     HubProxy.On("AddMessage", (name, message) =>
     this.Dispatcher.Invoke(() =>
     RichTextBoxConsole.AppendText(String.Format("{0}: {1}\r", name, message))
     ));
     
     try
     {
     await Connection.Start();
     }
     catch (HttpRequestException)
     {
     // 连接失败
     return;
     }
     
     // 显示聊天控件
     ChatPanel.Visibility = Visibility.Visible;
     ButtonSend.IsEnabled = true;
     TextBoxMessage.Focus();
     RichTextBoxConsole.AppendText("连上服务:" + ServerUri + "\r");
     }

       


    上面的代码也就是WPF客户端实现的核心代码,主要逻辑为,客户端启动的时候就调用Connection.Start方法与服务器进行连接。然后通过HubProxy代理类来调用集线器中Send方法,而集线器中的Send方法又通过调用客户端的addMessage方法将消息输出到客户端的消息框中进行显示,从而完成消息的推送过程。

    其效果和Asp.net MVC上的效果是一样的。

    总结

    到这里,本专题的所有内容就结束了,这篇SignalR快速入门也是本人在学习SignalR过程中的一些心得体会,希望可以帮助一些刚接触SignalR的朋友快速入门。本篇主要实现了SignalR的广播消息的功能,可以实现手机端消息推送的功能,接下来一篇将介绍如何使用SignalR实现一对一的聊天。


    For more articles related to using SignalR to implement the push function in Asp.NET MVC, please pay attention to the PHP Chinese website!


    Statement:
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn