2015-09-21 80 views
2

下面你可以看到我的SignalR自託管中心的簡化版本,在Windows服務:存儲特定客戶端的連接ID在SignalR樞紐

public static class SubscriptionHandler 
{ 
    public static int PriceFeedMembersCount = 0; 
} 

public class PriceHub : Hub 
{ 
    public Task SubscribeToPriceFeed() 
    { 
     IHubContext context = GlobalHost.ConnectionManager.GetHubContext<PriceHub>(); 
     if (SubscriptionHandler.PriceFeedMembersCount == 0) 
     { 
      context.Clients.All.updatePriceSubscriptionStatus(true); 
     } 
     SubscriptionHandler.PriceFeedMembersCount++; 
     return context.Groups.Add(Context.ConnectionId, "PriceFeed"); 
    } 

    public Task UnsubscribeFromPriceFeed() 
    { 
     IHubContext context = GlobalHost.ConnectionManager.GetHubContext<PriceHub>(); 
     SubscriptionHandler.PriceFeedMembersCount--; 
     if (SubscriptionHandler.PriceFeedMembersCount == 0) 
     { 
      context.Clients.All.updatePriceSubscriptionStatus(false); 
     } 
     return context.Groups.Remove(Context.ConnectionId, "PriceFeed"); 
    } 

    public void NotifySubscribers(Price price) 
    { 
     IHubContext context = GlobalHost.ConnectionManager.GetHubContext<PriceHub>(); 
     context.Clients.Group("PriceFeed").updatePrice(price); 
    } 
} 

而且我有兩種類型的客戶爲中心的:其中之一是Web應用程序,另一個是Windows服務。在這裏,您可以看到我的窗口服務演示實現作爲signalr客戶端:

public partial class WinSer45 : ServiceBase 
{ 
    private HubConnection hubConnection; 
    private IHubProxy priceProxy; 
    private Timer timer = new Timer(); 
    private bool hasSubscribers = false; 

    public WinSer45() 
    { 
     InitializeComponent(); 
    } 

    protected override void OnStart(string[] args) 
    { 
     timer.Interval = 1000; // saniyede bir 
     timer.Elapsed += timer_Elapsed; 
     timer.Enabled = true; 

     hubConnection = new HubConnection("http://localhost:8080/signalr", useDefaultUrl: false); 
     priceProxy = hubConnection.CreateHubProxy("PriceHub"); 
     hubConnection.Start().Wait(); 
     priceProxy.On<bool>("UpdatePriceSubscriptionStatus", hasSubscribers => 
     { 
      this.hasSubscribers = hasSubscribers; 
     }); 
    } 

    void timer_Elapsed(object sender, ElapsedEventArgs e) 
    { 
     if (hasSubscribers) 
     { 
      TestPrice testPrice = new TestPrice() { Id = 1, Buy = 1.2345, Sell = 9.8765, Symbol = "EURUSD" }; 
      priceProxy.Invoke("NotifySubscribers", testPrice).Wait();  
     } 
    } 

    protected override void OnStop() 
    { 
    } 
} 

正如你看到的我用的是hasSubscribers標誌,以儘量減少輪轂和客戶端之間的消息。並且hasSubscribers標誌被更改爲SubscribeToPriceFeedUnsubscribeFromPriceFeed methods。

如果你仔細看,你在SubscribeToPriceFeed看到下面的一行:

context.Clients.All.updatePriceSubscriptionStatus(真);

我不想將消息發送給所有客戶端,但我的客戶端Windows服務。如何將特定客戶端的連接ID存儲在我的集線器中?如果我能做到這一點,我知道我可以發送短信到特定的ConnectionId如下一行:

context.Clients.Client(的ConnectionId).updatePriceSubscriptionStatus(真);

由於提前,連接期間

回答

3

通源

這樣

hubConnection = new HubConnection("http://localhost:8080/signalr","source=windows",useDefaultUrl: false); 

HUB

public override Task OnConnected() 
{ 
    var source= Context.QueryString['source']; 
    return base.OnConnected(); 
} 

創建一類無線LL保持與源

public class user { 
    public string ConnectionID {set;get;} 
    public string Source {set;get;} 
} 

用戶在轂聲明列表

List<user> userList=new List<user>(); 

然後OnConnected

public override Task OnConnected() 
{ 
    var us=new user(); 
    us.Source = Context.QueryString['source']; 
    us.ConnectionID=Context.ConnectionId; 
    userList.Add(us); 
    return base.OnConnected(); 
} 

,並在推用戶廣播期間只由源過濾它

var windowsUser=userList.Where(o=>o.Source == "windows").ToList(); // you'll get the windows user list 
+0

切記: public override TaskRelated(){ return base.OnConnected(); } –