Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
signalr / SignalR Programming in Microsoft ASP.NET.pdf
Скачиваний:
65
Добавлен:
25.05.2015
Размер:
19.23 Mб
Скачать

#result p {

margin: 0 0 10px 0;

}

Script (Scripts/ProgressBar.js)

$(function () {

var hub = $.connection.progressBarHub; hub.client.update = function (value) {

$("#progressBar").css("width", value + "%")

.text(value + " %");

};

$("#start").click(function () { $(this).attr("disabled", true); $("#result")

.hide("slow")

.load("hardprocess.aspx?connId=" + $.connection.hub.id, function () {

$(this).slideDown("slow"); $("#start").attr("disabled", false);

});

});

$.connection.hub.start()

.done(function () { $("#start").attr("disabled", false);

});

});

Implementation on the server side

Hub

using Microsoft.AspNet.SignalR;

namespace ProgressBar

{

public class ProgressBarHub : Hub { }

}

Note that we do not need any method in the hub, because the information is sent to the clients through the external process, as shown in the following code example.

Expensive process (HardProcess.Aspx)

<%@ Page Language="C#"

Inherits="System.Web.UI.Page" EnableSessionState="false" %> <%@ Import Namespace="System.Diagnostics" %>

<%@ Import Namespace="System.Threading" %>

<%@ Import Namespace="Microsoft.AspNet.SignalR" %> <%@ Import Namespace="ProgressBar" %>

<%

Persistent connections and hubs from other threadsChapter 6

115

www.it-ebooks.info

Response.Expires = -1;

var connectionId = Request["connId"]; var hub = GlobalHost.ConnectionManager

.GetHubContext<ProgressBarHub>(); Stopwatch stopWatch = Stopwatch.StartNew();

// Simulate a very very hard process...

for (int i = 1; i <= 100; i++)

{

hub.Clients.Client(connectionId).update(i); Thread.Sleep(150);

}

%>

<p>The answer to life, the universe and everything is: 42.</p> <p>

And it only took <%:stopWatch.ElapsedMilliseconds / 1000 %> seconds to find it out.

</p>

This page receives the connection identifier as a parameter, which allows it to send the progress data only to the specific client that initiated the process.

Startup code (startup.cs)

using Owin;

public class Startup

{

public void Configuration(IAppBuilder app)

{

app.MapSignalR();

}

}

116 Chapter 6Persistent connections and hubs from other threads

www.it-ebooks.info

Соседние файлы в папке signalr