Asp.Net Core SignalR acquisition hub example, sending a message from the external hub

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/u011127019/article/details/90812008

An acquiring operation IHubContext sending a message from the controller

In this way, simple common

public class HomeController : Controller
{
    private readonly IHubContext<NotificationHub> _hubContext;

    public HomeController(IHubContext<NotificationHub> hubContext)
    {
        _hubContext = hubContext;
    }
}
public async Task<IActionResult> Index()
{
    await _hubContext.Clients.All.SendAsync("Notify", $"Home page loaded at: {DateTime.Now}");
    return View();
}

Second, the message is sent from the operation acquisition IHubContext middleware

Access IHubContextintermediate conduit as follows:

app.Use(async (context, next) =>
{
    var hubContext = context.RequestServices
                            .GetRequiredService<IHubContext<MyHub>>();
    //...
});

III / IHubContext acquired from the non Http request from timer task 

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IHttpContextAccessor accessor)
{

    MvcContext.Accessor = accessor;
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    app.UseStaticFiles();
    app.UseMvc();

    //配置SignalR
    app.UseSignalR(routes => routes.MapHub<CountHub>("/count"));

    //启动定时任务
    Task.Run(() =>
    {
        while (true)
        {
            var hubContext = app.ApplicationServices
            .GetService<IHubContext<CountHub>>();
            hubContext.Clients.All.SendAsync("someFunc", new { random = "定时任务" });
            Thread.Sleep(1000);
        }
    });
}

Fourth, acquires from the request context IHubContext instance, with this embodiment the controller

    public class MvcContext
    {
        public static IHttpContextAccessor Accessor;
        public static HttpContext GetContext()
        {
            return Accessor.HttpContext;
        }
    }
    public class HubOperate
    {
        public IHubContext<CountHub> GetHubOne()
        {
            return MvcContext.GetContext()
                 .RequestServices
                 .GetRequiredService<IHubContext<CountHub>>();
        }
    }

Test code:

    [Route("test1/{action}")]
    public class Test1Controller : Controller
    {
        public IActionResult One()
        {
            HubOperate _hub = new HubOperate();
            _hub.GetHubOne().Clients.All.SendAsync("someFunc", new { random = "efgh" });
            return Content("执行完成");
        }
    }

How to get the global context: Asp.Net Core HttpContext context acquisition request

 

More:

 Asp.Net Core 2.0 technology using SignalR - Getting Started

 Asp.Net Core WebSocket binding

SingalR self-hosted (self-host) Example 2 achieved Wpf client and Web client rectangle synchronization

Guess you like

Origin blog.csdn.net/u011127019/article/details/90812008