Original: ASP. NET SignalR hub does not use proxy implementations
Implementations that do not use the build agent JS
Some students may find it troublesome to use hubs, either by introducing virtual directories or by generating JS files during generation, and then introducing JS files for development. Isn't there a more refreshing way? Of course it is, don't (????)?? Hey, peel it up and make a choice after reading it.
A simple look at the generated JS file, in fact, a lot of code, we can also according to this JS to write a
First we need to have a connection object
Let Hubdemo = $.hubconnection ("/simplehub");
Then create the proxy for the Hubdemo class
var proxy = Hubdemo.createhubproxy ("hubdemo");
You can then open the connection, where the proxy object is used to execute the HelloService method
Hubdemo.start (). Done (function () { Proxy.invoke ("helloservice"); });
Remember the HelloService method will call the Helloclient function, so this is also not less
Proxy.on ("helloclient", Function () { console.log (" Received the server's greeting "); })
It's time to run and see the effect, well, it looks perfect.
Complete code as follows, you can see these class names and method names are to be specified, that is, although fully decoupled but this is difficult for the development, and these agents are only the proxy generated JS simplified version, and there is no smart reminder, because to write-.-two ways each have pros and cons, with which are good. Choose your own according to the project situation.
Let Hubdemo = $.hubconnection ("/simplehub"); varProxy = Hubdemo.createhubproxy ("Hubdemo"); Hubdemo.start (). Done (function () {Proxy.invoke ("HelloService"); }); Proxy.on ("helloclient", function () {Console.log ("receive greetings from the server"); });
Hubname and Hubmethodname
We can use these two attributes to rename our method to the class, for example, the method name is hello, but I want the client to call Myhello.
[Hubmethodname ("Hello")] Public void HelloService (stringint age2) { Clients.All.helloClient (); }
The effect of Hubname is similar to that of Hubmethodname, and the difference is that it acts on the class.
[Hubname ("HubDemo2")] Public class Hubdemo:hub
ASP. NET SignalR hub does not use proxy implementations