1、概述
通過前面幾篇文章
史上最全面的SignalR系列教程-2、SignalR 實現推送功能-永久連接類實現方式
史上最全面的SignalR系列教程-3、SignalR 實現推送功能-集線器類實現方式
RDIFramework.NET敏捷開發框架通過SignalR技術整合即時通訊(IM)
我們對SignalR的概念以及SignalR的最主要的兩類通信模型(Persistent Connections與Hubs)進行了詳細的對比講解,也做了案例展示。本篇將為大家介紹.NET特有的Self-Host自托管的應用,即以Self-Host自托管為宿主加載SignalR服務。
宿主一詞我們不會陌生,它可以看作是一個基礎設施,它為一些服務和功能提供最底層的支持,如你的web應用程序可以運行在iis或者apache上,而這兩個東西就是web應用程序的宿主,而今天說的自主宿主SelfHost它可以自己去監聽自己的服務,如你可以把一個web應用程序宿主到一個console控制台程序上,或者把一個webApi宿主到一個console或者windowService上,這都是可以的。
Self-Host的介紹我們可以參考msdn官方事例https://docs.microsoft.com/en-us/dotnet/framework/wcf/samples/self-host 這是一個wcf的事例。
SignalR常常依托於ASP.NET應用程序運行於IIS中,但它還可以自我托管(比如作為console winform、Windows service),只要我們使用self-host庫就可以了。該庫向所有的SignalR 2庫一樣,構建於OWIN (Open Web Interface for .NET)。OWIN定義了一個在.NET web 服務端和web 應用程序的抽象。OWIN解耦了從服務端來的web 應用程序,這使得OWIN對於子托管web應用程序於自己的進程中得以表現得很完美。
有的小伙伴可能就要問了,為什么一定要使用這種方式來托管呢?基於IIS的方式不是更簡單嗎?不托管於IIS的原因主要有以下方面的考慮:
- 我們已經構建了一個服務端程序,沒有運行在IIS上,重新搭建成本太高。
- IIS的方式性能開銷很大。
- signalR需要擴展到現有程序中,如Windows服務中等。
- 其他的一些原因....
本篇主要講解以下內容:
- 使用Self-Host方式創建SignalR服務端
- 用javascript客戶端訪問SignalR Self-Host服務端
- 用WinForm客戶端接收SignalR Self-Host服務端消息
2、使用Self-Host方式創建SignalR服務端
為了更簡單的說明self-host托管的方式,我們用控制台應用程序來做演示。當然我們也可以自托管到Windows服務中去,如果你更希望托管到Windows服務,可以參考Self-Hosting SignalR in a Windows Service
在上一項目的基礎上,我們新建一個名為:SignalRSelfHost的控制台應用程序,如下所所示:
要使用Selft-Host宿主SignalR,必須引用Microsoft.AspNet.SignalR.SelfHost包。我們在程序包管理控制台輸入以下命令安裝SelfHost包。
Install-Package Microsoft.AspNet.SignalR.SelfHost
要想支持跨域訪問,還需要安裝
Install-Package Microsoft.Owin.Cors
如果用命令的方式安裝失敗,我們可以用NuGet的方式安裝,如下圖所示。
編寫代碼如下:
using Microsoft.AspNet.SignalR;
using Microsoft.Owin.Cors;
using Microsoft.Owin.Hosting;
using Owin;
using System;
namespace SignalRSelfHost
{
class Program
{
static void Main(string[] args)
{
// This will *ONLY* bind to localhost, if you want to bind to all addresses
// use http://*:8080 to bind to all addresses.
// See http://msdn.microsoft.com/en-us/library/system.net.httplistener.aspx
// for more information.
string url = "http://localhost:8077";
using (WebApp.Start(url))
{
Console.WriteLine("SignalR Server running on {0}", url);
Console.ReadLine();
}
}
}
/// <summary>
/// 該類含有SignalR服務端的配置(該教程使用的唯一的配置是用來調用UseCors),
/// MapSignalR為所有形式的Hub對象創建了路由規則
/// </summary>
class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseCors(CorsOptions.AllowAll);
app.MapSignalR();
}
}
/// <summary>
/// SignalR的Hub 類是程序要提供給客戶端的
/// 該類就一個方法,Send:客戶端可以用來發送消息給其他客戶端
/// </summary>
public class MyHub : Hub
{
//服務端的方法,客戶端可以去調用
public void Send(string name, string message)
{
//調用客戶端的方法addMessage(string s1,string s2);
Clients.All.addMessage(name, message);
}
}
}
代碼說明:
-
Program:包含程序的主方法。在這個方法中,類型為Startup的web應用程序啟動於指定的URL (http://localhost:8077)。 如果需要更加安全一點,可以支持SSL,請去這里看看How to: Configure a Port with an SSL Certificate
-
Startup: 該類含有SignalR服務端的配置(該教程使用的唯一的配置是用來調用UseCors), MapSignalR為所有形式的Hub對象創建了路由規則。
-
MyHub:SignalR的Hub 類是程序要提供給客戶端的。 該類就一個方法:Send, 客戶端可以用來發送消息給其他客戶端。
編譯運行SignalR控制台服務端如下。
可以看到我們的控制台服務端已經成功啟動起來了。
3、用javascript客戶端訪問SignalR Self-Host服務端
3.1、新建SignalRSelfHostJSClient的MVC空項目
3.2、添加Microsoft.AspNet.SignalR.JS的引用
3.3、新建Default.html頁面
代碼如下:
<!DOCTYPE html>
<html>
<head>
<title>SignalRSelfHostJSClient Chat</title>
<style type="text/css">
.container {
background-color: #a1c6ec;
border: thick solid #e62fc7;
padding: 20px;
margin: 20px;
}
</style>
</head>
<body>
<div class="container">
<input type="text" id="message" style="width:350px;" placeholder="請輸入消息內容..."/>
<input type="button" id="sendmessage" value="發送" />
<input type="hidden" id="displayname" />
<ul id="discussion"></ul>
</div>
<!--Script references. -->
<!--Reference the jQuery library. -->
<script src="Scripts/jquery-1.6.4.min.js"></script>
<!--Reference the SignalR library. -->
<script src="Scripts/jquery.signalR-2.4.1.min.js"></script>
<!--Reference the autogenerated SignalR hub script. -->
<script src="http://localhost:8077/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:8077/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').prepend('<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>
3.4、運行效果
先運行SignalRSelfHost控制台服務端后,再運行兩個我們的測試客戶端,分別輸入不同的用戶名,試試聊天的效果,如下圖所示。
4、用WinForm客戶端接收SignalR Self-Host服務端消息
4.1、新建SignalRSelfHost的WinForm項目
4.2、使用NuGet安裝Microsoft.AspNet.SignalR.Client包
4.3、制作測試界面如下:
編寫界面代碼如下:
using Microsoft.AspNet.SignalR.Client;
using System;
using System.Configuration;
using System.Windows.Forms;
namespace SignalRSelfHostWinFormClient
{
/// <summary>
/// SignalRSelfHost WinForm測試客戶端
/// 用於接收消息(需配合SignalRSelfHostJSClient項目使用)
/// </summary>
public partial class SignalRSelfHostWinFormClientTest : Form
{
//定義代理,廣播服務連接相關
private static IHubProxy HubProxy { get; set; }
private static readonly string ServerUrl = ConfigurationManager.AppSettings["SignalRServer"];
//定義一個連接對象
public static HubConnection Connection { get; set; }
public SignalRSelfHostWinFormClientTest()
{
CheckForIllegalCrossThreadCalls = false;
InitializeComponent();
}
private void SignalRSelfHostWinFormClientTest_Load(object sender, EventArgs e)
{
Connection = new HubConnection(ServerUrl);
Connection.Closed += Connection_Closed;
HubProxy = Connection.CreateHubProxy("MyHub");
HubProxy.On<string, string>("addMessage", RecvMsg);//接收實時信息
Connection.Start().ContinueWith(task =>
{
if (!task.IsFaulted)
{
msgContent.AppendText(string.Format("與Signal服務器連接成功,服務器地址:{0}\r\n",ServerUrl));
}
else
{
msgContent.AppendText("與服務器連接失敗,請確認服務器是否開啟。\r\n");
}
}).Wait();
}
private void Connection_Closed()
{
msgContent.AppendText("連接關閉...\r\n");
}
private void RecvMsg(string name, string message)
{
msgContent.AppendText(string.Format("接收時間:{0},發送人:{1},消息內容:{2},\r\n", DateTime.Now, name, message));
}
}
}
4.4、運行效果:
通過以上的詳細講解,我們已經非常清楚的了解了如何通過SignalR打通各終端以實現相互溝通的通信。例子雖然比較簡潔,但完全可以以此為基礎,擴展更復雜的業務應用。
5、代碼下載
實例源碼可以移步github下載,地址:https://github.com/yonghu86/SignalRTestProj
6、參考文章
一路走來數個年頭,感謝RDIFramework.NET框架的支持者與使用者,大家可以通過下面的地址了解詳情。
RDIFramework.NET官方網站:http://www.rdiframework.net/
RDIFramework.NET官方博客:http://blog.rdiframework.net/
同時需要說明的,以后的所有技術文章以官方網站為准,歡迎大家收藏!
RDIFramework.NET框架由海南國思軟件科技有限公司專業團隊長期打造、一直在更新、一直在升級,請放心使用!
歡迎關注RDIFramework.net框架官方公眾微信(微信號:guosisoft),及時了解最新動態。
掃描二維碼立即關注