簡體   English   中英

從服務器向所有客戶端發送信號器消息

[英]Send signalr message from server to all clients

這與SignalR + posting a message to a Hub via an action method有關,但我的問題有點不同:

我在使用集線器的 signalr 版本 0.5.2。 在舊版本中,鼓勵您在集線器上創建方法以向所有客戶端發送消息,這就是我所擁有的:

public class MyHub : Hub
{
    public void SendMessage(string message)
    {
        // Any other logic here
        Clients.messageRecieved(message);
    }

    ...
}

所以在 0.5.2 中,我想向所有客戶端發送消息(比如從控制器中的某個地方)。 我怎樣才能訪問MyHub實例?

我看到引用的唯一方法是:

var hubContext = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
hubContext.Clients.messageRecieved("hello");

這很好,但我想在我的集線器上調用該方法。

您可以使用靜態方法執行此操作:

SignalR v.04-

public class MyHub : Hub
{
    internal static void SendMessage(string message)
    {
        var connectionManager = (IConnectionManager)AspNetHost.DependencyResolver.GetService(typeof(IConnectionManager));
        dynamic allClients = connectionManager.GetClients<MyHub>();
        allClients.messageRecieved(message);
    }

    ...
}

信號R 0.5+

public class MyHub : Hub
{
    internal static void SendMessage(string message)
    {
        IHubContext context = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
        context.Clients.messageRecieved(message);
    }

    ...
}

然后你可以這樣稱呼它:

MyHub.SendMessage("The Message!");

關於 SignalR API 的好文章: http ://weblogs.asp.net/davidfowler/archive/2012/05/04/api-improvements-made-in-signalr-0-5.aspx

由 Paolo Moretti 在評論中提供

我有同樣的問題,在我的例子中 addNotification 是客戶端方法:

var hubContext = GlobalHost.ConnectionManager.GetHubContext<SignalR.NotificationsHub>();
hubContext.Clients.addNotification("Text here");

在您的客戶端,您可以添加代碼以在 addNotification 中調用您的集線器方法:

var notification = $.connection.notificationHub;
notification.addNotification = function (message) {
 notification.addServerNotification(message); // Server Side method
}

$.connection.hub.start();

中心:

 [HubName("notificationHub")]
    public class NotificationsHub : Hub
    {
        public void addServerNotification(string message)
        {
          //do your thing
        }
    }

更新:一遍又一遍地閱讀你的問題,我真的找不到這樣做的理由。 集線器方法通常是從客戶端調用的,或者我誤解了你,無論如何這是一個更新。 如果你想做服務器端的事情然后通知客戶。

  [HttpPost]
  [Authorize]
  public ActionResult Add(Item item)
  {
      MyHubMethodCopy(item);
      var hubContext = GlobalHost.ConnectionManager.GetHubContext<SignalR.NotificationsHub>();
    hubContext.Clients.addNotification("Items were added");

  }

  private void MyHubMethodCopy(Item item)
  {
      itemService.AddItem(item);
  }

ASP.NET Core 2.x 和 3.x 更新:

您可以在具有依賴注入的任何地方輕松訪問 Hub 實例:

public class HomeController : Controller
{
    private readonly IHubContext<MyHub> _myHubContext;

    public HomeController(IHubContext<MyHub> myHubContext)
    {
        _myHubContext = myHubContext;
    }

    public void SendMessage(string msg)
    {
        _myHubContext.Clients.All.SendAsync("MessageFromServer", msg);
    }
}

如果它給你語法錯誤,請確保你有:

using Microsoft.AspNetCore.SignalR;

並且您沒有:

using Microsoft.AspNet.SignalR;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM