2012-10-10 77 views
0

我是SignalR的新手。我正在努力做到這一點Progress Bar example
我無法通過NuGet下載和安裝軟件包,因爲我的工作中存在拒絕訪問的代理服務器。所以我自己將de DLL和腳本包含在項目中。Asp.net MVC 3中的SignalR:集線器未定義

我的看法代碼如下:

<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是不確定的,而我在得到空引用錯誤這點。

我做錯了,得到這個錯誤?

在此先感謝。

+0

當您訪問/ signalr /集線器你怎麼讓你的瀏覽器嗎? –

+0

檢查頁面的源代碼,看看是否所有的庫都被加載。我曾經有過同樣的問題,因爲我需要在global.asax中重新映射集線器路由(如果您注入信號器可能是問題) –

回答

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); 
    } 
... 
+0

屬性不是必需的 –

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