从系统的其它地方调用SignalR客户中心 [英] Calling SignalR hub clients from elsewhere in system

查看:404
本文介绍了从系统的其它地方调用SignalR客户中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了SignalR中心服务器和客户端之间进行通信。中心服务器侧code为存储在称为Hooking.cs类。我要的是要能够调用Hooking.cs中定义的方法,让我在我的应用程序的任何地方发送广播消息到任何连接的客户端。看来,Hooking.cs的新实例为每个客户端/服务器调用创建的,所以我希望我将能够使用像

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

该方法测试()在Hooking.cs定义,如

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

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

和一个客户端的JavaScript

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.

通过SignalR源$ C ​​$ C我跨看起来前途无量的方法来找, Hubs.Invoke(字符串hubName,字符串的方法,params对象[]参数) ,所以我希望我可以用的东西,如 Hubs.Invoke(挂钩,测试),但我不能使它工作。

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.

任何帮助,这将是巨大的AP preciated

Any help with this would be hugely appreciated

推荐答案

有是一个的例如,在文档

这篇关于从系统的其它地方调用SignalR客户中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆