[英]Calling SignalR hub clients from elsewhere in system
I've set up a SignalR hub to communicate between the server and client. 我已经设置了SignalR集线器以在服务器和客户端之间进行通信。 The hub server side code is stored in a class called Hooking.cs.
中心服务器端代码存储在名为Hooking.cs的类中。 What I want is to be able to call a method defined in Hooking.cs to allow me to broadcast messages to any connected clients from anywhere in my application.
我想要的是能够调用Hooking.cs中定义的方法,以允许我从应用程序中的任何位置向任何连接的客户端广播消息。 It seems that a new instance of Hooking.cs is created for every client/server call, so I had hoped that I would be able to use something like
似乎为每个客户端/服务器调用都创建了一个新的Hooking.cs实例,因此我希望可以使用类似
var hooking = new Hooking();
hooking.Test();
with the method Test() defined in Hooking.cs such as 与Hooking.cs中定义的方法Test()如
public static void Test() {
Clients.test()
}
and with a the client side javascript 并带有客户端javascript
var hooking = $.connection.hooking;
hooking.test = function() { alert("test worked"); };
$.connection.hub.start()
Unfortunately it isn't that simple, as Clients is not static, so not accessible from a static method. 不幸的是,它不是那么简单,因为客户端不是静态的,因此无法通过静态方法进行访问。
Looking through the SignalR source code I came across a method that looked promising, Hubs.Invoke(string hubName, string method, params object[] args)
, so I would hope I could use something such as Hubs.Invoke("Hooking", "Test")
but I can't make it work. 在查看SignalR源代码时,我遇到了一种看起来很有希望的方法
Hubs.Invoke(string hubName, string method, params object[] args)
,所以我希望可以使用Hubs.Invoke("Hooking", "Test")
但我无法使其正常工作。
Any help with this would be hugely appreciated 任何帮助将不胜感激
This is the correct way for SignalR 2.x: 这是SignalR 2.x的正确方法:
var context = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
context.Clients.All.addMessage(message);
Basically, you can use the dependency resolver for the current host to resolve the IConnectionManager
interface which allows you to get ahold of the context object for a hub. 基本上,您可以将依赖解析器用于当前主机,以解析
IConnectionManager
接口,该接口可让您获取集线器的上下文对象。
Further information can be found in the official documentation . 可以在官方文档中找到更多信息。
You can easily use a hub by following this 2 step- 您可以按照以下2个步骤轻松使用集线器 :
Instantiating by dependency injection like this- 像这样通过依赖注入实例化-
public class ClassName { ........ ........ private IHubContext _hub; public BulletinSenderController(IConnectionManager connectionManager) { _hub = connectionManager.GetHubContext<McpHub>(); ........ ........ } ............ ............ }
2.Using the hub
object like this- 2.像这样使用
hub
对象-
_hub.Clients.All.onBulletinSent(bulletinToSend);
More can be found here . 在这里可以找到更多。
Example code can be found in this git repo . 示例代码可以在此git repo中找到。
Have a look at how it's done in Chat.cs
in SignalR.Samples.Hubs.Chat
from https://github.com/SignalR/SignalR . 在https://github.com/SignalR/SignalR的
Chat.cs
中的SignalR.Samples.Hubs.Chat
查看它是如何完成的。
I can see in there that static Dictionary<TKey, TValue>
's are being instantiated at the top, so I imagine they are being maintained persistently too, either with the Chat
class being a persisted instance (?) or that array being updated somehow. 我可以在其中看到静态的
Dictionary<TKey, TValue>
在顶部被实例化,因此我想它们也将得到持久维护,或者将Chat
类作为持久化实例(?)或以某种方式更新该数组。 。
Check it out, David Fowler would probably be the best on this. 检查一下,David Fowler可能是最好的。
This has changed in .NET Core 2, now you can use dependency injection like this: 在.NET Core 2中,这已经发生了变化,现在您可以使用依赖项注入,如下所示:
private readonly IHubContext<MyHub,IMyHubInterface> _hubContext;
public MyController(MyHub,IMyHubInterface hubContext)
{
_hubContext = hubContext;
}
public bool SendViaSignalR()
{
_hubContext.Clients.All.MyClientSideSignalRMethod(new MyModel());
return true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.