Asp.net中的SignalR MVC 3:hub未定义

时间:2012-10-10 17:09:10

标签: asp.net-mvc-3 signalr signalr-hub signalr.client

我是SignalR的新手。我正在尝试这样做Progress Bar example 我无法通过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未定义,此时我有空引用错误。

我错误地得到了这个错误?

提前致谢。

2 个答案:

答案 0 :(得分:0)

您是否遗漏了该属性

[HubName("bookingHub")]
在您的BookingHub课上

? e.g。

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

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

答案 1 :(得分: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