从 Controller .Net Core 2.1 调用 Signalr 方法

Call Signalr method from Controller .Net Core 2.1

我正在尝试从(ASP.NET 核心)MVC 控制器调用信号器集线器 class 中的方法,但我无法在网上找到说明如何调用的示例。

注意:有很多将旧版本的信号器与 .Net Framework 结合使用的示例,但是 none 我可以看到它展示了如何在 .Net Core 中执行此操作。

我需要将来自 MVC 操作结果的 ID 直接传递到我的 Hub,而不是将 ID 传递到页面,然后必须将客户端连接返回到 Hub。

public class ChatHub : Hub
{ 
    public async Task DoSomething(int id)
    {            
        //// Something in here.
    }
}



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

    public HomeController(IHubContext<ChatHub> hubContext)
    {
        _hubContext = hubContext;
    }

    public async Task<ActionResult> Index(int id) 
    {

         //// Call the DoSomething method from here, passing the id across.
         await _hubContext.Clients.All.SendAsync("AddToGroup", groupId);
    }
}

请问有办法吗? (还是我看错了,有没有更好的方法可以达到同样的效果?)

更新: 如果我将 Id 传递到视图中,然后使用 JavaScript 调用集线器,这将调用 DoSomething 方法,所以我可以看到它们都正确地挂在一起,但当我尝试在 C# 中调用它时却没有。

您可以使用 IHubContext 来执行此操作:

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

    public HomeController(IHubContext<ChatHub> hubContext)
    {
        _hubContext = hubContext;
    }

    public async Task<ActionResult> Index(int id) 
    {
         //// Call the DoSomething method from here, passing the id across.
         await _hubContext.Clients.All.SendAsync("DoSomething", id);
    }
}

Full docs and examples here.

我认为您误解了它们是如何协同工作的(这与我昨天所做的相同),集线器代码用于客户端脚本代码回调然后执行操作,而IHubContext 用作将发送到客户端的强类型方法

枢纽

// This class is used by the JavaScript Client to call into the .net core application.
public class ChatHub : Hub<IChatClient>
{

    public static ConcurrentDictionary<string, string> Connections = new ConcurrentDictionary<string, string>();

    // As an example, On connection save the user name with a link to the client Id for later user callback
    public override Task OnConnectedAsync()
    {
        var user = Context.User.Identity.Name;

        Connections.AddOrUpdate(this.Context.ConnectionId, user, (key, oldValue) => user);

        return base.OnConnectedAsync();
    }

    public override Task OnDisconnectedAsync(Exception exception)
    {
        // Do something on disconnect.
    }

    // Add other methods you want to be able to call from JavaScript side in here...
    public void SendMessage(int id, string message)
    {
        // Message doing stuff here.
    }
}

聊天客户端界面

// This provides strongly-typed methods that you'll have on the Client side but these don't exist on the server.
public interface IChatClient
{
    //So this method is a JS one not a .net one and will be called on the client(s)
    Task DoSomething(int id);

    Task NotificationUpdate(int id, string message);
}

控制器

public class HomeController : Controller
{
    private readonly IHubContext<ChatHub, IChatClient> _hubContext;

    public HomeController(IHubContext<ChatHub, IChatClient> hubContext)
    {
        _hubContext = hubContext;
    }

    public async Task<ActionResult> Index(int id) 
    {

         // This calls the method on the Client-side
         await _hubContext.Clients.All.DoSomething(id);
    }
}