1

我是 SignalR 的新手。我正在尝试执行此进度条示例
我无法通过 NuGet 下载和安装包,因为我的工作中有一个代理拒绝访问。所以我自己在项目中包含 de DLL 和脚本。

我的视图代码如下:

<h2>@ViewBag.Message</h2>
<input type="button" id="bookButton" value="Book flight" />
<br />
<b>Status:</b>&nbsp;&nbsp;&nbsp;<span id="msg"></span>
<hr />
<input type="button" id="percButton" value="Process pending emails" />
<div id="bar" style="border: #000 1px solid; width: 300px;">
</div>
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
<link href="@Url.Content("~/Content/gauge-bar.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/jquery-1.6.4.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/json2.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.signalr.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/signalr/hubs")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/simple-gauge-bar.js")" type="text/javascript"></script>
<script type="text/javascript">
    $(function () {
        var bookingHub = $.connection.bookingHub;
        $("#percButton").click(function () {
            bookingHub.processPendingEmails();
        });

        $("#bookButton").click(function () {
            bookingHub.bookFlight("fco", "jfk");
        });

        bookinghub.updategaugebar = function (perc) {
            $("#bar").html(gaugebar.generate(perc));
        };
        bookinghub.displaymessage = function (message) {
            $("#msg").html(message);
        };

        $.connection.hub.start();
    });
</script>

我的集线器代码:

 public class BookingHub : Hub
{
    public void Send(String message)
    {

        // Call the addMessage method on all clients.
        Clients.displayMessage(message);
    }

    public void BookFlight(String from, String to)
    {
        // Book first leg
        Clients.displayMessage(String.Format("Booking flight: {0}-{1} ...", from, to));
        Thread.Sleep(2000);

        // Book return 
        Clients.displayMessage(String.Format("Booking flight: {0}-{1} ...", to, from));
        Thread.Sleep(3000);

        // Book return 
        Clients.displayMessage(String.Format("Booking flight: {0}-{1} ...", to, from));
        Thread.Sleep(2000);

        // Some return value
        Clients.displayMessage("Flight booked successfully.");
    }

    public void ProcessPendingEmails()
    {
        Clients.updateGaugeBar(10);
        Thread.Sleep(2000);

        Clients.updateGaugeBar(40);
        Thread.Sleep(3000);

        Clients.updateGaugeBar(64);
        Thread.Sleep(2000);

        Clients.updateGaugeBar(77);
        Thread.Sleep(2000);

        Clients.updateGaugeBar(92);
        Thread.Sleep(3000);

        Clients.updateGaugeBar(99);
        Thread.Sleep(2000);

        Clients.updateGaugeBar(100);
    }
}

当我运行该项目时,我注意到 bookingHub 是未定义的,此时我有空引用错误。

我做错了什么来得到这个错误?

提前致谢。

4

2 回答 2

0

将 Global.asax 添加到您的 Web 应用程序并将其添加到 global.ajax.cs:

namespace SampleWebApplication
{
    public class Global : System.Web.HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            RouteTable.Routes.MapHubs();
        }
    }
}

请参阅https://github.com/SignalR/SignalR/wiki/QuickStart-Hubs

于 2013-04-13T08:23:23.457 回答
0

你是否缺少属性,

[HubName("bookingHub")]

在您的 BookingHub 课程上?例如

 [HubName("bookingHub")]
 public class BookingHub : Hub
{
    public void Send(String message)
    {

        // Call the addMessage method on all clients.
        Clients.displayMessage(message);
    }
...
于 2012-10-11T15:31:01.367 回答