ASP. NET SignalR hub does not use proxy implementations

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.