2013-07-15 48 views
3

我嘗試在.NET 4下的WebForms應用程序中爲儀表板創建通知。我已下載SignalR 1.2版本(包括.net客戶端和服務器)並準備簡單的通知示例。不幸的是,它不工作,我不明白爲什麼。如果我輸入http://myserver.com/notificationSample/signalr/hubs將出現javascript代理,並且看起來沒問題。SignalR和.NET客戶端無法在ASP.NET WebForms頁面上工作

看看下面的實現,有人看到任何bug嗎?

一個)集線器實施

[HubName("NewMessage")] 
public class NewMessageNotifier : Hub 
{ 
    public void NotifyDashboards() 
    { 

     Clients.All.NewMessageCreated(); 
    } 
} 

b)中通知呼叫方(服務器)〜/頁/ NotificationCaller.aspx

public partial class NotificationCaller : Page 
{ 
    private HubConnection connection; 
    private IHubProxy proxy; 

    protected void Page_Load(object sender, EventArgs e) 
    { 
      connection = new HubConnection("http://myserver.com/notificationSample"); 

      proxy = connection.CreateHubProxy("NewMessage"); 

      connection.Start().Wait();     

    } 
    // it is handler for onclick event on Button control 
    protected void NotifyDashboard(object sender, EventArgs e) 
    { 
     proxy.Invoke("NotifyDashboards").Wait(); 
    } 
} 

C)儀表板(客戶端,偵聽)〜/頁/控制板。 ASPX

public partial class Dashboard: BasePage 
{ 
    private HubConnection connection; 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     connection = new HubConnection("http://myserver.com/notificationSample"); 

     var proxy = connection.CreateHubProxy("NewMessage"); 

     proxy.On("NewMessageCreated", ShowNotification); 

     connection.Start(); 
    } 

    private void ShowNotification() 
    { 
     ShowAlert("New message added!"); 
    } 

} 

回答

4

你是在錯誤的方式使用它

首先 B和C是客戶端,服務器獲取自身開始,所有你需要的是做的是

RouteTable.Routes.MapHubs(); 

添加到全球的

Application_Start 

方法。 ASAX

如果你打算使用一個網頁作爲客戶端,你應該從JavaScript做到這一點,因爲你現在正在做什麼都不行,因爲

connection.Start() 

是異步,它沒有做任何事情之前,該請求將結束,它不會等待傳入連接,因爲所有將被處置

現在該怎麼做?它會在這裏需要很多的網頁,所以這裏有幾個環節

A Simple Tutorial

The Hubs Server API

The Hubs JavaScript API

並且如果你錯過了,a video that explains what is SignalR, how it works and a simple app你可以找到here

+0

謝謝,這幫助了我很多,現在我看到了我的誤解。我想澄清一件事。你說,對於網頁我必須使用JS,所以我假設SignalR .NET客戶端可以與WinForms,SilverLight,WP,WPF等技術一起工作,只要你願意,它們可以在內存中存儲對象。它不適用於WebForms,WebApi和ASP MVC。那是對的嗎? – tkestowicz

+0

@tkestowicz這是正確的 – MEYWD

+0

現在一切都足夠清晰。再次感謝。 – tkestowicz