代码之家  ›  专栏  ›  技术社区  ›  Brett Rigby

来自控制器.Net核心2.1的调用信号器方法

  •  3
  • Brett Rigby  · 技术社区  · 6 年前

    我试图从(ASP.NET核心)MVC控制器中调用signalr Hub类中的方法,但在网上找不到演示如何调用的示例。

    我需要将一个id从一个MVC操作结果直接传递到我的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);
        }
    }
    

    请问有什么办法吗(或者我是不是看错了,有没有更好的方法来达到同样的效果?)

    更新:

    3 回复  |  直到 6 年前
        1
  •  5
  •   Stephen Gerard Darragh    6 年前

    我认为您误解了它们是如何一起工作的(这和我昨天之前做的是一样的),hub代码是用于客户端脚本代码的回调,然后执行操作,而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);
        }
    }
    
        2
  •  2
  •   marcusturewicz    6 年前

    你可以用 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.