Getting started with SignalR Hub, signalrhub

Source: Internet
Author: User

Getting started with SignalR Hub, signalrhub

Based on persistent connections, SignalR provides a high-level abstraction layer: Hub. Based on the flexibility of javascript and the dynamic characteristics of C #, Hub is a crucial development model, it eliminates the boundaries between the two independent physical environments, client and server.

The most common usage mode in the Web environment allows us to transparently call methods between the client and the server. This is two-way RPC, that is, you can directly call the server method from the client, and the server can also call the client method.

To implement the above functions, SignalR will use the Hub class of the server to automatically create proxy objects on the client, and enter the remote calls of their actual methods in their methods; on the contrary, when the server calls a method on the client, it is parsed by a special dynamic protocol packaged by the server and pushed to other breakpoints using the underlying transmission protocol, then they will reach each client and be explained and run on the client.

Now, create a Hub class on the server, for example:

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using Microsoft.AspNet.SignalR; 6  7 namespace WebApplication2 8 { 9     public class TestHub : Hub10     {11         public void Hello()12         {13             Clients.All.hello();14         }15     }16 }

 

Unlike persistent connections, Hub-based services do not need to map the specified Hub and access URL at startup, because the system will automatically find the relevant Hub class, at the same time, they can be accessed through a separate base URL (/SignalR), of course, to facilitate this base URL can be changed:

1 using System; 2 using System. threading. tasks; 3 using Microsoft. owin; 4 using Owin; 5 using Microsoft. aspNet. signalR; 6 [assembly: OwinStartup (typeof (WebApplication2.Startup)] 7 8 namespace WebApplication2 9 {10 public class Startup11 {12 public void Configuration (IAppBuilder app) 13 {14 app. mapSignalR (); // default ing path "/SignalR" 15 // or 16 app. mapSignalR ("/test", new HubConfiguration (); // you can set the access path and Hub configuration items. You do not need to specify the Hub class to be started, because they are automatically recognized 17} 18} 19}

 

In the TestHub class,

1 public class TestHub: Hub2 {3 public void Hello () 4 {5 Clients. All. sayHi ("Hello"); 6} 7}

Hello is a method in the TestHub class. This method can be called by the client using js.

In this method, Clients. All. sayHi () indicates to call the sayHi function written by the client in js to All Clients.

To prevent exposing the real name of TestHub, we can create an alias Test. Because this alias will be used when called by the client.

[HubName ("test")] public class TestHub: Hub {
// Omit Part of the Code
}

 

In addition to notifying all clients to call the client method, the server can also restrict the clients to be sent. At the same time, the Clients attribute has many dynamic members for our use:

Clients. All: Allow "call" to connect to All Clients on this Hub.

Clients. AllExcept: indicates that the call must be sent to all Clients, except those that act as parameters. The parameters here can be connectionId strings, arrays, etc.

Clients. Caller determines that the Caller's receiver is the client currently calling the Hub method.

Clients. Client: sends a call to the Client with the specified connectionId. The parameter can be a string or an array.

Client. Others: represents all connected clients, but does not include clients that are calling this method.

In the method, you can access this. Context. ConnectionId to obtain the unique identifier of the client that is currently using the method.

A hello method has been defined in the TestHub class on the server, and the purpose of this method is to call the sayHi Method on all clients.

Create a client now:

Create an html page and write the following code in the Code:

1 <! DOCTYPE html> 2 

 

Related Article

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.