从Controller .Net Core 2.1调用Signalr方法 [英] Call Signalr method from Controller .Net Core 2.1

查看:320
本文介绍了从Controller .Net Core 2.1调用Signalr方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从(ASP.NET Core)MVC控制器调用signalr Hub类中的方法,但是我找不到在线示例来说明如何实现.

I am trying to call a method in the signalr Hub class from an (ASP.NET Core) MVC Controller, but I cannot find an example online that shows how to.

注意:有许多示例将旧版本的Signalr与.Net Framework结合使用,但是我看不到任何显示如何在.Net Core中执行此操作的示例.

我需要将MVC操作结果中的ID直接传递到我的集线器,而无需将该ID传递到页面,然后必须将客户端连接返回到集线器.

I need to pass an id from the an MVC Action Result directly through to my Hub, without passing the id to the page, and then having to get a client connection back through to the 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);
    }
}

请问有办法吗? (还是我看错了方向,有没有更好的方法来达到相同的结果?)

Is there a way to do this please? (Or am I looking at this the wrong way, and is there a better way to achieve the same result?)

更新:如果我将Id传递到视图中,然后使用JavaScript调用Hub,那么它将调用DoSomething方法,因此我可以看到它们全部正确地挂在一起,但是当我尝试用C#来调用它.

Update: If I pass the Id into the view, and then use JavaScript to call the Hub, this then calls the DoSomething method, so I can see it all hangs together correctly, but not when I try to call it in C#.

推荐答案

我认为您误解了它们如何协同工作(直到昨天我都做过同样的事情),中心代码是针对客户端的脚本代码进行回调,然后再执行操作,而IHubContext用作将被发送到客户端的强类型方法.

I think you're misunderstanding how it all works together (which is the same thing I did up until yesterday), the hub code is for the client-side script code to call back into and then action, whereas the IHubContext is used as the strongly typed methods that will be sent to the Client-side

集线器

// 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.
    }
}

ChatClient界面

ChatClient Interface

// 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);
    }
}

这篇关于从Controller .Net Core 2.1调用Signalr方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆