SIGNALR Getting Started persistent connection

Source: Internet
Author: User

The underlying API for accessing SIGNALR persistent connections provides an abstraction layer that hides the underlying inherent complexity in order to maintain the exploitation of persistent connections between the client and the server, and to send data on such connections using transport.

In fact, accessing the communication channel through the API is similar to using the socket at the bottom:

On the server side, we are notified when the connection is turned on or off, the data is received, and the message is sent to the client. On the client side, we can open or close the connection, send or accept any data. Like a socket, a message has no format, which can be said to be in the form of a traditional data format-a text string.

From the client's point of view, only one connection to the server can be used to send the data immediately, and a callback function called by the SIGNALR to perform the receiving of the information.

From the server side, a persistent connection is a class that inherits from Persistentconnection, and in order to allow control when an event is generated, some methods of that class can be overridden.

Each persistent connection can be accessed externally from a URL. It is possible to adopt methods similar to other frameworks, such as MVC and Web APIs.

The next step is to configure SIGNALR to associate each persistent connection with its access path.

The previous SIGNALR version had to perform the related registration through Global.asax, but it was integrated into Owin after 2.0.

SIGNALR an application runs on a host process based on Owin, the host program looks for a class that is named Startup in the application's root namespace, and then executes its configuration method.

Start the startup class at the root of the Web application,

Such as:

1 usingSystem;2 usingSystem.Threading.Tasks;3 usingMicrosoft.owin;4 usingOwin;5 6[Assembly:owinstartup (typeof(Startup))]7 8  Public classStartup9 {Ten      Public voidConfiguration (Iappbuilder app) One     { A   -     } -}

The above configuration method can be regarded as the starting method, in which the corresponding SIGNALR connection and path mapping can be established.

Now create a persistent connection class: Testconnection The class inherits from the Persistentconnection, and when some of the events related to the service and the Persistentconnection class are generated, the virtual method provided by the class is called. To implement this logic, you only need to rewrite the relevant methods.

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Threading.Tasks;5 usingsystem.web;6 usingMicrosoft.AspNet.SignalR;7 8  Public classtestconnection:persistentconnection9 {Ten     protected OverrideTask onconnected (irequest request,stringConnectionID) One     { A         returnConnection.send (ConnectionID,"welcome!"); -     } -  the     protected OverrideTask onreceived (irequest request,stringConnectionID,stringdata) -     { -         returnconnection.broadcast (data); -     } +}

In the Testconnection class, there are two methods that override the self-persistentconnection: onconnected and Onrecevied, respectively, when there is a client connection and when the client sends the message server to receive the message. In fact, in the testconnection can also rewrite other methods, here the default is the two.

These two methods have two common parameters, request and Connectid, The request indicates that the parameters associated with the requests are similar to that in ASP. Request,connectionid is a string that represents the unique identifier of the client, and the ConnectionID is different for each connected client.

In this method of onconnected,

Return Connection.send (ConnectionID, "welcome!" )//indicates to the client that was just connected to send "welcome! "String message, this method only sends a message to a specified user. ConnectionID is the client unique identifier.


In this method of onrecevied,

return Connection.broadcast (data),//data is a message sent by a client, the whole method means to send a data message to all clients.

SIGNALR Getting Started persistent connection

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.