从系统中的其他位置调用 SignalR hub 客户端

发布于 2024-12-07 22:09:08 字数 792 浏览 0 评论 0 原文

我已经设置了一个 SignalR 集线器来在服务器和客户端之间进行通信。中心服务器端代码存储在名为 Hooking.cs 的类中。我想要的是能够调用 Hooking.cs 中定义的方法,以允许我从应用程序中的任何位置向任何连接的客户端广播消息。似乎为每个客户端/服务器调用创建了一个新的 Hooking.cs 实例,因此我希望能够使用

var hooking = new Hooking();
hooking.Test();

Hooking.cs 中定义的方法 Test() 之类的东西,例如

public static void Test() {
    Clients.test()
}

和客户端side javascript

var hooking = $.connection.hooking;
hooking.test = function() { alert("test worked"); };
$.connection.hub.start()

不幸的是,事情并没有那么简单,因为客户端不是静态的,因此无法通过静态方法访问。

通过查看 SignalR 源代码,我发现了一个看起来很有前途的方法,Hubs.Invoke(string hubName, string method, params object[] args),所以我希望我可以使用诸如 < code>Hubs.Invoke("Hooking", "Test") 但我无法使其工作。

对此的任何帮助将不胜感激

I've set up a SignalR hub to communicate between the server and client. The hub server side code is stored in a class called 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. 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

var hooking = new Hooking();
hooking.Test();

with the method Test() defined in Hooking.cs such as

public static void Test() {
    Clients.test()
}

and with a the client side 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.

Any help with this would be hugely appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

极致的悲 2024-12-14 22:09:08

这是 SignalR 2.x 的正确方法:

var context = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
context.Clients.All.addMessage(message);

基本上,您可以使用当前主机的依赖关系解析器来解析 IConnectionManager 接口,该接口允许您获取集线器的上下文对象。

更多信息可以在 中找到官方文档

This is the correct way for 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.

Further information can be found in the official documentation.

银河中√捞星星 2024-12-14 22:09:08

Hub.GetClients 在 0.4.0 版本中已消失。

wiki 您现在可以使用:

IConnectionManager connectionManager = AspNetHost.DependencyResolver.Resolve<IConnectionManager>();
dynamic clients = connectionManager.GetClients<MyHub>();

Hub.GetClients has disappeared in version 0.4.0.

From the wiki you can now use:

IConnectionManager connectionManager = AspNetHost.DependencyResolver.Resolve<IConnectionManager>();
dynamic clients = connectionManager.GetClients<MyHub>();
但可醉心 2024-12-14 22:09:08

您可以按照以下 2 个步骤轻松使用集线器 -

  1. 通过像这样的依赖注入进行实例化 -

    公共类类名
    {
        …………
        …………
        私有 IHubContext _hub;
    
        公共BulletinSenderController(IConnectionManager连接管理器)
        {
            _hub = connectionManager.GetHubContext();
            …………
            …………
        }
    
        …………
        …………
    }
    

- 2.像这样使用 hub 对象 -

_hub.Clients.All.onBulletinSent(bulletinToSend);

更多内容可以找到 此处

可以找到示例代码在此 git 存储库中

You can easily use a hub by following this 2 step-

  1. 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-

_hub.Clients.All.onBulletinSent(bulletinToSend);

More can be found here.

Example code can be found in this git repo.

迷路的信 2024-12-14 22:09:08

看看它是如何在 https://github.com/SignalR/SignalR

我可以在那里看到静态 Dictionary 在顶部被实例化,所以我想它们也被持久地维护,或者通过 Chat类是一个持久实例(?)或者该数组正在以某种方式更新。

看看吧,大卫·福勒可能是这方面最好的。

Have a look at how it's done in Chat.cs in SignalR.Samples.Hubs.Chat from https://github.com/SignalR/SignalR.

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.

Check it out, David Fowler would probably be the best on this.

冰雪梦之恋 2024-12-14 22:09:08

这在 .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;
    }

This has changed in .NET Core 2, now you can use dependency injection like this:

    private readonly IHubContext<MyHub,IMyHubInterface> _hubContext;

    public MyController(MyHub,IMyHubInterface hubContext)
    {
        _hubContext = hubContext;
    }

    public bool SendViaSignalR()
    {
        _hubContext.Clients.All.MyClientSideSignalRMethod(new MyModel());
        return true;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文