C# 从服务器向所有客户端发送信号消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11472920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 17:59:15  来源:igfitidea点击:

Send signalr message from server to all clients

c#asp.net-mvcsignalr

提问by Matt Roberts

This is related to SignalR + posting a message to a Hub via an action method, but my question is a bit different:

这与SignalR + 通过操作方法向集线器发布消息有关,但我的问题有点不同:

I'm on version 0.5.2 of signalr, using hubs. In older versions, you were encouraged to create methods on the hub to send messages to all clients, which is what I have:

我使用的是 0.5.2 版本的信号器,使用集线器。在旧版本中,我们鼓励您在集线器上创建方法来向所有客户端发送消息,这就是我所拥有的:

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

    ...
}

So in 0.5.2, I want to send a message to all the clients (say from somewhere in the controller). How can I get access to the MyHubinstance?

所以在 0.5.2 中,我想向所有客户端发送一条消息(比如从控制器中的某个地方)。如何访问MyHub实例?

The only way I've seen referenced is:

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

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

Which is fine, but I want to call the method on my hub.

这很好,但我想在我的集线器上调用该方法。

采纳答案by Paul

You can do this by using a static method:

您可以使用静态方法执行此操作:

SignalR v.04-

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

    ...
}

SignalR 0.5+

信号R 0.5+

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

    ...
}

You can then call this like so:

然后你可以这样调用它:

MyHub.SendMessage("The Message!");

Good article on the SignalR API: http://weblogs.asp.net/davidfowler/archive/2012/05/04/api-improvements-made-in-signalr-0-5.aspx

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

Provided by Paolo Morettiin comments

由 Paolo Moretti在评论中提供

回答by formatc

I had same problem, in my example addNotification is client-side method:

我有同样的问题,在我的例子中 addNotification 是客户端方法:

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

On you client side you can add code to call your hub method in addNotification:

在您的客户端,您可以添加代码以在 addNotification 中调用您的集线器方法:

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

$.connection.hub.start();

Hub:

中心:

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

UPDATE: Reading your question over and over agian I really don't find a reason to do that. Hub methods are usually there to be called from client side, or I misunderstood you, anyway here is an update. If you want to do a server side thing and then notify clients.

更新:一遍又一遍地阅读你的问题,我真的找不到这样做的理由。集线器方法通常是从客户端调用的,否则我误解了你,无论如何这里有一个更新。如果你想做服务器端的事情,然后通知客户端。

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