How do I call a SignalR hub method from the outside?

后端 未结 3 837
有刺的猬
有刺的猬 2020-12-30 03:53

This is my Hub code:

public class Pusher : Hub, IPusher
{
    readonly IHubContext _hubContext = GlobalHo         


        
相关标签:
3条回答
  • 2020-12-30 04:18

    If you're looking to call a method in your hub from another project then it needs to reside within the same app domain. If it does here's how you can do it:

    Call a hub method from a controller's action (don't mind the title, it works for your scenario)

    0 讨论(0)
  • 2020-12-30 04:23

    You can't instantiate and call a hub class directly like that. There is much plumbing provided around a Hub class by the SignalR runtime that you are bypassing by using it as a "plain-old class" like that.

    The only way to interact with a SignalR hub from the outside is to actually get an instance of an IHubContext that represents the hub from the SignalR runtime. You can only do this from within the same process, so as long as your other "project" is going to be running in process with the SignalR code it will work.

    If your other project is going to be running in another process then what you would want to do is expose a sort of "companion" API which is either another SignalR hub or a regular old web service (with ASP.NET web API) that you can call from this other application to trigger the behavior you want. Whichever technology you choose, you would probably want to secure this so that only your authenticated applications can call it.

    Once you decide which approach you're going to take, all you would do to send messages out via the Pusher hub would be:

    // Get the context for the Pusher hub
    IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext<Pusher>();
    
    // Notify clients in the group
    hubContext.Clients.Group(group).GetData(data);
    
    0 讨论(0)
  • 2020-12-30 04:29

    Take a look at this link at the topic of (How to call client methods and manage groups from outside the Hub class).
    Code example simply creates a singleton instance of the caller class and pass in the IHubContext into it's constructor. Then you have access to desired context.Clients in caller class's methods:

    // This sample only shows code related to getting and using the SignalR context.
    public class StockTicker
    {
        // Singleton instance
        private readonly static Lazy<StockTicker> _instance = new     Lazy<StockTicker>(() => new StockTicker(GlobalHost.ConnectionManager.GetHubContext<StockTickerHub>()));
    
    private IHubContext _context;
    
    private StockTicker(IHubContext context)
    {
        _context = context;
    }
    
    // This method is invoked by a Timer object.
    private void UpdateStockPrices(object state)
    {
        foreach (var stock in _stocks.Values)
        {
            if (TryUpdateStockPrice(stock))
            {
                _context.Clients.All.updateStockPrice(stock);
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题