SignalR entry-level persistent connections and signalr entry-level persistence

Source: Internet
Author: User

SignalR entry-level persistent connections and signalr entry-level persistence

In order to maintain the development of persistent connections between the client and the server, and send data on such connections, the underlying API used to access the SignalR persistent connection provides an abstraction layer that hides the inherent complexity of the underlying layer.

In fact, using this API to access the communication channel is similar to using Socket at the underlying layer:

On the server side, we will be notified when the connection is opened or closed, data is received, and information is sent to the client. On the client, we can open or close the connection, send or accept any data. Like Socket, a message has no format. It can be said that the format is a text string in the traditional data format.

From the client perspective, you only need to initiate a connection to the server to use it immediately to send data and receive the execution information of a callback function called by SignalR.

From the server perspective, persistent connections are a class inherited from PersistentConnection. To allow control when an event is generated, You can override some methods of this class.

Each persistent connection can be accessed from outside through a URL. Therefore, you can use methods similar to other frameworks, such as mvc and web APIs.

Next, configure SignalR to associate each persistent connection with its access path.

Previous SignalR versions must be registered through global. asax, but they have been integrated into OWIN since 2.0.

The SignalR application is running in the OWIN-based host process. The host program searches for a class named Startup in the root namespace of the application, and then runs its Configuration method.

First, create a Startup class in the root directory of the Web application,

For example:

 1 using System; 2 using System.Threading.Tasks; 3 using Microsoft.Owin; 4 using Owin; 5  6 [assembly: OwinStartup(typeof(Startup))] 7  8 public class Startup 9 {10     public void Configuration(IAppBuilder app)11     {12  13     }14 }

 

The Configuration method above can be regarded as the startup method, in which the corresponding SignalR connection and path ing can be established.

Now a persistent connection class is created: TestConnection class inherits from PersistentConnection. When some events related to services and PersistentConnection class connections are generated, the virtual method provided by this class is called, to implement this logic, you only need to override the relevant methods.

 

 

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Threading.Tasks; 5 using System.Web; 6 using Microsoft.AspNet.SignalR; 7  8 public class TestConnection : PersistentConnection 9 {10     protected override Task OnConnected(IRequest request, string connectionId)11     {12         return Connection.Send(connectionId, "Welcome!");13     }14 15     protected override Task OnReceived(IRequest request, string connectionId, string data)16     {17         return Connection.Broadcast(data);18     }19 }

In the TestConnection class, there are two methods to override the self-PersistentConnection: OnConnected and OnRecevied, which indicate that when a client is connected and when the client sends a message, the server receives the message. In fact, other methods can be rewritten in TestConnection. The default two methods are used here.

These two methods have two common parameters: request and connectId. request indicates that the parameters related to the Request are similar to the request in asp.net. connectionId is a string that indicates the unique identifier of the client, connectionId is different for different clients connected.

 

In the OnConnected method,

Return Connection. Send (connectionId, "Welcome! "); // Indicates sending" welCome! "to the client you just connected !" String message. This method only sends messages to a specified user. ConnectionId is the unique identifier of the client.


In the OnRecevied method,

Return Connection. Broadcast (data); // data is a message sent from a client. The entire method is to send data messages to all clients.

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.