一、SignalR是什么
Asp.net SignalR是微软为实现实时通信的一个类库。一般情况下,SignalR会使用JavaScript的长轮询(long polling)的方式来实现客户端和服务器通信,随着Html5中WebSockets出现,SignalR也支持WebSockets通信。另外SignalR开发的程序不仅仅限制于宿主在IIS中,也可以宿主在任何应用程序,包括控制台,客户端程序和Windows服务等,另外还支持Mono,这意味着它可以实现跨平台部署在Linux环境下。
SignalR内部有两类对象:
Http持久连接(Persisten Connection)对象:用来解决长时间连接的功能。还可以由客户端主动向服务器要求数据,而服务器端不需要实现太多细节,只需要处理PersistentConnection 内所提供的五个事件:OnConnected, OnReconnected, OnReceived, OnError 和 OnDisconnect 即可。
Hub(集线器)对象:用来解决实时(realtime)信息交换的功能,服务端可以利用URL来注册一个或多个Hub,只要连接到这个Hub,就能与所有的客户端共享发送到服务器上的信息,同时服务端可以调用客户端的脚本。
SignalR将整个信息的交换封装起来,客户端和服务器都是使用JSON来沟通的,在服务端声明的所有Hub信息,都会生成JavaScript输出到客户端,.NET则依赖Proxy来生成代理对象,而Proxy的内部则是将JSON转换成对象。
二、为什么要用SignalR
聊天室,如在线客服系统,IM系统等
消息的实时推送服务
巡更人员位置的实时推送
1. server -------- 创建一个MVC 项目,选择新建的项目,右击-->选择管理NuGet程序包-->搜索 signalr--> 安装Microsoft ASP.NET SignalR
新建启动程序 Startup.cs
在类中添加代码:
- app.MapSignalR();
在集线器类ChatHub.cs中添加如下代码
using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Web; using Microsoft.AspNet.SignalR; using System.Threading.Tasks; namespace ChatServer { public class ChatHub : Hub { public void Hello() { //Clients.All.hello(); //Clients.All.AddMsg(); } /// <summary> /// 供客户端调用的服务器端代码 /// </summary> /// <param name="message"></param> public void Send(string name,string message) { // 调用所有客户端的sendMessage方法 Clients.All.sendMessage(name, message); Trace.WriteLine("调用所有客户端的sendMessage方法"); } /// <summary> /// 客户端连接的时候调用 /// </summary> /// <returns></returns> public override Task OnConnected() { Trace.WriteLine("客户端连接成功"); return base.OnConnected(); } } }
2. 客户端(winform)
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Net.Http; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Microsoft.AspNet.SignalR.Client; namespace ChatClient { public partial class Form1 : Form { public Form1() { InitializeComponent(); Text = "Test SignalR"; FormClosing += Form1_FormClosing; } public IHubProxy HubProxy { get; set; } const string ServerURI = "https://localhost:44306/";//ServerURI = "https://localhost:44306/signalr"; public HubConnection Connection { get; set; } /// <summary> /// Creates and connects the hub connection and hub proxy. This method /// is called asynchronously /// </summary> private async void ConnectAsync() { Connection = new HubConnection(ServerURI); Connection.Closed += ShowConnectionClosed; HubProxy = Connection.CreateHubProxy("ChatHub"); //Handle incoming event from server: use Invoke to write to console from SignalR's thread HubProxy.On<string, string>("sendMessage", (name, message) => { this.Invoke( new Action(() => { RichTextBoxConsole.AppendText(String.Format("{0}: {1} ", name, message)); } )); } ); try { await Connection.Start(); } catch (HttpRequestException) { Text = "Unable to connect to server: Start server before connecting clients."; //No connection: Don't enable Send button or show chat UI return; } TextBoxMessage.Focus(); RichTextBoxConsole.AppendText("Connected to server at " + ServerURI + " "); } private void CloseConnection() { if (Connection != null) { Connection.Stop(); Connection.Dispose(); } } /// <summary> /// If the server is stopped, the connection will time out after 30 seconds (default), and the /// Closed event will fire. /// </summary> private void ShowConnectionClosed() { RichTextBoxConsole.AppendText("You have been disconnected. "); } private void btnConnect_Click(object sender, EventArgs e) { RichTextBoxConsole.AppendText("Connecting to server..."); ConnectAsync(); } private void btnSend_Click(object sender, EventArgs e) { if (Connection.State == Microsoft.AspNet.SignalR.Client.ConnectionState.Disconnected) { ShowConnectionClosed(); return; } HubProxy.Invoke("Send", "Client wgscd", TextBoxMessage.Text); TextBoxMessage.Text = String.Empty; TextBoxMessage.Focus(); } private void btnDisconnect_Click(object sender, EventArgs e) { CloseConnection(); } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { CloseConnection(); } } }