ASP.NET SignalR 是為 ASP.NET 開發人員提供的一個庫,可以簡化開發人員將實時 Web 功能添加到應用程序的過程。實時 Web 功能是指這樣一種功能:當所連接的客戶端變得可用時服務器代碼可以立即向其推送內容,而不是讓服務器等待客戶端請求新的數據。
官網:http://signalr.net/
下載:install-package Microsoft.AspNet.SignalR
本節將簡單快速介紹
實現原理
基于SignalR(SR)的實現原理,所以SR在客戶端瀏覽器IE8以上基本都是完全兼容的??梢哉f完全支持jQuery 1.6.4的瀏覽器就能支持SignalR。
Hello World
創建空的Asp.Net項目
安裝
install-package Microsoft.AspNet.SignalR
install-package bootstrap
添加一個集線器類
public class ChatHub : Hub { public void Send(string name, string message) { // Call the broadcastMessage method to update clients. Clients.All.broadcastMessage(name, message); } }
添加一個OWIN Startup類
[assembly: OwinStartup(typeof(SignalRChart.Startup))]namespace SignalRChart{ public class Startup { public void Configuration(IAppBuilder app) { // 有關如何配置應用程序的詳細信息,請訪問 http://go.microsoft.com/fwlink/?LinkID=316888 app.MapSignalR(); } }}
添加一個index.html
1.導入js
<!--Script references. --> <!--Reference the jQuery library. --> <script src="Scripts/jquery-1.9.1.min.js"></script> <!--Reference the SignalR library. --> <script src="Scripts/jquery.signalR-2.2.0.min.js"></script> <!--Reference the autogenerated SignalR hub script. --> <script src="signalr/hubs"></script>
2.hub
// Declare a PRoxy to reference the hub.var chat = $.connection.chatHub;// Call the Send method on the hub.chat.server.send(name, message); // Create a function that the hub can call to broadcast messages.chat.client.broadcastMessage = function (name, message) {}
快速分析
Hub代碼
Client代碼
注意事項
In ASP.NET MVC 4 you can do the following: <script src="~/signalr/hubs"></script> If you're writing an ASP.NET MVC 3 application, make sure that you are using Url.Content for your script references: <script src="@Url.Content("~/signalr/hubs")"></script>
代碼下載:等待整理
本文作者:Never、C
本文鏈接:http://www.49028c.com/neverc/p/4617488.html
新聞熱點
疑難解答