SignalR服務器通常在IIS的asp.net應用程序上承載,但它也可以使用自托管庫來作為自托管的主機來運行(就像控制臺應用程序或Windows服務那樣)與Signal2.0一樣,自托管庫是基于.Net開放式Web接口(OWIN)來構建的。OWIN定義了.Net Web服務器和Web應用程序之間的抽象接口,將Web應用程序從服務器上解耦,使得OWIN可以在IIS之外建立自托管主機。
那我們為什么不在IIS中進行托管SignalR呢?可參考以下理由:
1)不能安裝IIS環境或IIS不能使用,比如無IIS的服務器主機
2)考慮到性能,需要避免IIS的額外開銷。
3)SignalR運行在Windows服務或Azure工作角色,或被用于其他現存的應用程序。
(一般windows操作系統服務器都可安裝IIS,非windows操作系統服務器都不能使用-_-!!!;SinglaR放在IIS中占的資源多還是自托管占資源多,運行效率和性能如何,都需要測試好;)
在本例子中,您將創建托管在控制臺應用程序中的服務器,當然,將其承載在Windeos服務及Azure工作角色中也是可行的。
1)已管理員權限運行VS2013,新建一個控制臺應用程序,命名為”SignalRSelfHost“并確定。
2)打開程序包管理控制臺。
3)在控制臺中輸入一下命令
Install-Pagkage Microsoft.AspNet.SignalR.SelfHost |
此命令將SingalR自托管庫添加到項目中。
4)繼續在控制臺輸入以下命令:
Install-Package Microsoft.Owin.Cors |
此命令將OWIN核心庫添加到項目中,因為SignalR主機與網頁客戶端端之間在不同的域中運行,該庫將用于跨域支持。由于SignalR服務和Web客戶端運行在不同的端口上,這意味著如果想在這些組件之間進行通訊,則必須啟動這些組件中的跨域功能。
5)替換PRogram.cs中的代碼:
using System; using Microsoft.AspNet.SignalR; using Microsoft.Owin.Hosting; using Owin; using Microsoft.Owin.Cors; namespace SignalRSelfHost { class Program { static void Main(string[] args) { string url = "http://localhost:8080"; using (WebApp.Start(url)) { Console.WriteLine("Server running on {0}", url); Console.ReadLine(); } } } class Startup { public void Configuration(IAppBuilder app) { app.UseCors(CorsOptions.AllowAll); app.MapSignalR(); } } public class MyHub : Hub { public void Send(string name, string message) { Clients.All.addMessage(name, message); } } } |
上面代碼包含了三個類:
A:Program,包含了Mian方法定義執行的主路徑。在該方法中,指定了本地主機使8080端口來啟動該Web應用程序。當然,您也可以實現SSL來進一步提高安全性。
B:Startup,包含了SignalR服務器的配置(本例子中,僅僅使用了UserCors配置類 ,并調用MapSignalR,為集線器建立路由映射。
C:MyHub,SignalR的集線器實現類,用于提供客戶端服務。這個類還包含了一個方法:Send,用于將接收到的客戶端消息廣播給其他已連接的客戶端。
6)編譯并運行,在服務器的地址將顯示在控制臺中。
7)如果執行失敗,除了發生System.Relection.TargetInvocationException錯誤,您需要給管理員權限重新運行VS2013并重新編譯運行。
8)在進行下一步前,請關閉控制臺程序。
在本例子中,您將使用同入門教程一致的JS客戶端。我只是進行一項修改,即定義集線器URL,作為自托管主機,服務器不一定在相同的URL作為連接地址(參考反向代理及負載平衡),所以URL需要顯示定義。
1) 在解決方案資源管理器中,添加Asp.Net Web應用程序,命名為”JavascriptClient”,然后確定。
2)已空模版創建項目。
3)在包管理控制臺中,在默認項目下拉選擇“JavaScriptClient“項目,并執行一下命令
Install-Package Microsoft.AspNet.SignalR.JS |
此命令安裝客戶端所需要的SignalR以及jQuery庫
4)添加一個新的Html頁面,命名為“Default.html“
5)用以下的代碼替換Html中的內容,同樣需要確認代碼中引用的腳本路徑是否一致。
<!DOCTYPE html> <html> <head> <title>SignalR Simple Chat</title> <style type="text/CSS"> .container { background-color: #99CCFF; border: thick solid #808080; padding: 20px; margin: 20px; } </style> </head> <body> <div class="container"> <input type="text" id="message" /> <input type="button" id="sendmessage" value="Send" /> <input type="hidden" id="displayname" /> <ul id="discussion"></ul> </div> <!--Script references. --> <!--Reference the jQuery library. --> <script src="Scripts/jquery-1.10.2.min.js"></script> <!--Reference the SignalR library. --> <script src="Scripts/jquery.signalR-2.0.3.min.js"></script> <!--Reference the autogenerated SignalR hub script. --> <script src="http://localhost:8080/signalr/hubs"></script> |
<!--Add script to update the page and send messages.--> <script type="text/javascript"> $(function () { //Set the hubs URL for the connection $.connection.hub.url = "http://localhost:8080/signalr"; // Declare a proxy to reference the hub. var chat = $.connection.myHub; // Create a function that the hub can call to broadcast messages. chat.client.addMessage = function (name, message) { // Html encode display name and message. var encodedName = $('<div />').text(name).html(); var encodedMsg = $('<div />').text(message).html(); // Add the message to the page. $('#discussion').append('<li><strong>' + encodedName + '</strong>: ' + encodedMsg + '</li>'); }; // Get the user name and store it to prepend to messages. $('#displayname').val(prompt('Enter your name:', '')); // Set initial focus to message input box. $('#message').focus(); // Start the connection. $.connection.hub.start().done(function () { $('#sendmessage').click(function () { // Call the Send method on the hub. chat.server.send($('#displayname').val(), $('#message').val()); // Clear text box and reset focus for next comment. $('#message').val('').focus(); }); }); }); </script> </body> </html> |
注意:次行代碼生命了SignalR的基礎連接URL:
$.connection.hub.url = "http://localhost:8080/signalr"; |
6)在解決方案上右擊,設置多個啟動項目為啟動
7)在Default.html上右擊,設置為起始頁。
8)運行該項目,將彈出控制臺服務以及Web頁面,如果Web頁面在控制臺服務器啟動前執行,您需要重新刷新一次頁面。
9)您可以輸入用戶名,打開多個瀏覽器來進行多用戶的聊天室進行測試了。^-^
新聞熱點
疑難解答