2014-01-14 68 views
0

我有一個集線器需要從客戶端這樣的請求,調用:執行長輪詢數據庫在signalR樞紐

[HubName("Check")] 
    public class Check : Hub 
    { 
     private long _sessionId; 
     private string _sortType; 
     private int _tableCount; 
     private Timer _timer; 

     public void Car(long sessionId, string sortType, int tableCount) 
     { 
      _sessionId = sessionId; 
      _sortType = sortType; 
      _tableCount = tableCount; 
      StartCarTimer(); 
     } 

     public void StartCarTimer() 
     { 
      _timer = new Timer(3000); 
      _timer.Elapsed += CarTimerElapsed; 
      _timer.Start(); 
     } 

     void CarTimerElapsed(object sender, ElapsedEventArgs e) 
     { 
      int countCars = CountCars(); 

      if (countCarPrices > _tableCount) 
      { 
       _tableCount = countCarPrices; 
       Clients.All.car(GetCarTable()); 
      } 
     } 


     private int GetCars() 
     { 
      CarPrices carPrices = new CarPriceRepository().FindCarPricesById(_sessionId); 
      return carPrices; 
     } 
    } 

我簡化它一點點,但基本上我是輪詢數據庫每隔3幾秒鐘檢查變化,這真的感覺像可怕的設計。我一直在尋找無處不在的解決方案。通過一個朋友建議

一種方法是通知signalR集線器從更新數據庫服務的服務器端,但我不知道該怎麼做。

有人請指點我正確的方向?至少2客戶端之間

+0

你可以寫一個新的web服務在2個應用程序之間進行更新時進行通信 – Geezer68

+0

您能解釋您要做什麼以及您有什麼問題? – thepirat000

+0

這個問題在我想要做什麼以及我有什麼問題時很明顯。大聲笑... – Dimo

回答

-1

signalr工作
客戶端A,連接並等待消息,
客戶B,做一些事情(開始服務器的請求),服務器通知客戶端A revice消息

GlobalHost.ConnectionManager.GetHubContext<T>().Clients 

你可以用它來獲取所有客戶隨時隨地
客戶相同的客戶中心物業

Clients.Group("groupName").method(param) 

這樣的一些代碼,可以注意到你想注意的客戶,方法是一個js方法,它通過信號發生器自動生成與你相同的名稱集線器方法(第一個字母小寫)