Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从服务器向所有客户端发送信号器消息_C#_Asp.net Mvc_Signalr - Fatal编程技术网

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

C# 从服务器向所有客户端发送信号器消息,c#,asp.net-mvc,signalr,C#,Asp.net Mvc,Signalr,这是关于,但我的问题有点不同: 我使用的是signalr的0.5.2版本,使用的是集线器。在旧版本中,我们鼓励您在中心上创建方法,以便向所有客户端发送消息,这就是我所拥有的: public class MyHub : Hub { public void SendMessage(string message) { // Any other logic here Clients.messageRecieved(message); }

这是关于,但我的问题有点不同:

我使用的是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");
var-hubContext=GlobalHost.ConnectionManager.GetHubContext();
hubContext.Clients.messagereceived(“你好”);

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

您可以使用静态方法:

信号员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);
    }

    ...
}
关于signarapi的好文章:


由Paolo Moretti在评论中提供我也有同样的问题,在我的示例中,addNotification是客户端方法:

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

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

  [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);
  }
[HttpPost]
[授权]
公共行动结果添加(项目)
{
MyHub方法副本(项目);
var hubContext=GlobalHost.ConnectionManager.GetHubContext();
hubContext.Clients.addNotification(“已添加项”);
}
私有无效MyHubMethodCopy(项)
{
itemService.AddItem(项目);
}
ASP.NET Core 2.x和3.x的更新: 您可以轻松访问具有依赖项注入的任何位置的集线器实例:

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.AspNet.SignalR;

AspNetHost.DependencyResolver
已在信号器0.5+中消失。请参阅(获取连接或集线器外部的连接/集线器的引用)另一个回答回答了我的问题,但您得到了+1,因为经过思考,我不确定是否需要调用集线器方法-就像您所说的“从客户端使用集线器方法”+1来指出如何使用集线器方法。我也会犯同样的错误。在这之后修复了它。@emrenevayeshirazi什么错误?这现在在ASP.NET核心中也是可能的(尽管信号器本身在当前时间仍处于alpha状态)。请参见此问题的核心版本。
 [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);
  }
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;