国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

第四章SignalR自托管主機

2019-11-17 02:02:38
字體:
供稿:網(wǎng)友

第四章SignalR自托管主機

Posted on 2015-03-21 11:32 珠海華仔 閱讀(...) 評論(...) 編輯 收藏

第四章SignalR自托管主機

SignalR服務器通常在IIS的asp.net應用程序上承載,但它也可以使用自托管庫來作為自托管的主機來運行(就像控制臺應用程序或Windows服務那樣)與Signal2.0一樣,自托管庫是基于.Net開放式Web接口(OWIN)來構建的。OWIN定義了.Net Web服務器和Web應用程序之間的抽象接口,將Web應用程序從服務器上解耦,使得OWIN可以在IIS之外建立自托管主機。

那我們?yōu)槭裁床辉贗IS中進行托管SignalR呢?可參考以下理由:

1)不能安裝IIS環(huán)境或IIS不能使用,比如無IIS的服務器主機

2)考慮到性能,需要避免IIS的額外開銷。

3)SignalR運行在Windows服務或Azure工作角色,或被用于其他現(xiàn)存的應用程序。

(一般windows操作系統(tǒng)服務器都可安裝IIS,非windows操作系統(tǒng)服務器都不能使用-_-!!!;SinglaR放在IIS中占的資源多還是自托管占資源多,運行效率和性能如何,都需要測試好;)

1.設置項目:

在本例子中,您將創(chuàng)建托管在控制臺應用程序中的服務器,當然,將其承載在Windeos服務及Azure工作角色中也是可行的。

1)已管理員權限運行VS2013,新建一個控制臺應用程序,命名為”SignalRSelfHost“并確定。

2)打開程序包管理控制臺。

3)在控制臺中輸入一下命令

Install-Pagkage Microsoft.AspNet.SignalR.SelfHost

此命令將SingalR自托管庫添加到項目中。

4)繼續(xù)在控制臺輸入以下命令:

Install-Package Microsoft.Owin.Cors

此命令將OWIN核心庫添加到項目中,因為SignalR主機與網(wǎng)頁客戶端端之間在不同的域中運行,該庫將用于跨域支持。由于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方法定義執(zhí)行的主路徑。在該方法中,指定了本地主機使8080端口來啟動該Web應用程序。當然,您也可以實現(xiàn)SSL來進一步提高安全性。

B:Startup,包含了SignalR服務器的配置(本例子中,僅僅使用了UserCors配置類 ,并調(diào)用MapSignalR,為集線器建立路由映射。

C:MyHub,SignalR的集線器實現(xiàn)類,用于提供客戶端服務。這個類還包含了一個方法:Send,用于將接收到的客戶端消息廣播給其他已連接的客戶端。

6)編譯并運行,在服務器的地址將顯示在控制臺中。

7)如果執(zhí)行失敗,除了發(fā)生System.Relection.TargetInvocationException錯誤,您需要給管理員權限重新運行VS2013并重新編譯運行。

8)在進行下一步前,請關閉控制臺程序。

2.使用javaScript客戶端訪問服務器端:

在本例子中,您將使用同入門教程一致的JS客戶端。我只是進行一項修改,即定義集線器URL,作為自托管主機,服務器不一定在相同的URL作為連接地址(參考反向代理及負載平衡),所以URL需要顯示定義。

1) 在解決方案資源管理器中,添加Asp.Net Web應用程序,命名為”JavascriptClient”,然后確定。

2)已空模版創(chuàng)建項目。

3)在包管理控制臺中,在默認項目下拉選擇“JavaScriptClient“項目,并執(zhí)行一下命令

Install-Package Microsoft.AspNet.SignalR.JS

此命令安裝客戶端所需要的SignalR以及jQuery庫

4)添加一個新的Html頁面,命名為“Default.html“

5)用以下的代碼替換Html中的內(nèi)容,同樣需要確認代碼中引用的腳本路徑是否一致。

<!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>:&nbsp;&nbsp;' + 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頁面在控制臺服務器啟動前執(zhí)行,您需要重新刷新一次頁面。

9)您可以輸入用戶名,打開多個瀏覽器來進行多用戶的聊天室進行測試了。^-^


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 井冈山市| 饶平县| 银川市| 黑山县| 芦山县| 巴塘县| 沾益县| 松潘县| 雷波县| 霍林郭勒市| 金塔县| 阳西县| 柏乡县| 东至县| 玉林市| 汕头市| 义乌市| 汨罗市| 五原县| 黑河市| 衡南县| 兴安县| 梁河县| 雷山县| 江孜县| 从化市| 历史| 增城市| 四平市| 辽阳市| 晋州市| 奉节县| 太仓市| 资源县| 西宁市| 阆中市| 华蓥市| 兰考县| 彭泽县| 临江市| 贺兰县|