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
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…