Asp.net SignalR makes real-time communication so simple. asp. netsignalr

Source: Internet
Author: User

Asp.net SignalR makes real-time communication so simple. asp. netsignalr

In the inspection project, you need to send Real-Time Messages and start a task reminder, so you have the opportunity to access SignalR. during use, it is found that it is very easy to implement communication using SignalR, here I will share my thoughts on three aspects:

I. What is SignalR?

Asp.net SignalR is a class library that Microsoft uses to implement real-time communication. Generally, SignalR uses the long polling (long polling) method of JavaScript to implement client-to-server communication. As WebSockets appears in Html5, SignalR also supports WebSockets communication. In addition, programs developed by SignalR are not only restricted to the host in IIS, but can also host in any application, including the console, client programs, and Windows Services. In addition, Mono is also supported, this means that it can be deployed across platforms in a Linux environment.

SignalR has two types of objects:

Persisten Connection object: used to solve the function of persistent connections. The client can also request data from the server. The server does not need to implement too many details, but only needs to handle the five events provided in PersistentConnection: OnConnected, OnReconnected, OnReceived, onError and OnDisconnect.

Hub objects: these objects are used to exchange real-time information. The server can use URLs to register one or more hubs, you can share the information sent to the server with all clients, and the server can call the client script.

SignalR encapsulates the entire information exchange. Both the client and server use JSON for communication. All Hub information declared on the server will generate JavaScript output to the client ,. NET is dependent on Proxy to generate Proxy objects, while the Proxy is internally converting JSON into objects.

Ii. Why should I use SignalR?

Chat rooms, such as online customer service systems and IM systems

Real-Time Message PUSH Service

Real-time push of patrol personnel location

3. How to Implement SignalR. The following describes the major function and chat of SignalR. The following is a simple DEMO:

1. Create an asp.net web Application

2. Select the template MVC and change the authentication-free mode.

3. Select the new project, right-click the project, and choose manage NuGet packages. Search for signalr and install Microsoft ASP. NET SignalR.

4. Create the Startup Program Startup. cs

Add code to the class:

app.MapSignalR();

5. Create a SignalR hub ChatHub. cs

6. Add the following code to the Hub ChatHub. cs:

Public class ChatHub: hub {// <summary> /// server code for the client to call /// </summary> /// <param name = "message"> </param> public void Send (string message) {var name = Guid. newGuid (). toString (). toUpper (); // call the sendMessage method Clients of all Clients. all. sendMessage (name, message );} /// <summary> // called when the client is connected /// </summary> /// <returns> </returns> public override Task OnConnected () {Trace. writeLine ("client connection successful"); return base. onConnected ();}}

7. Modify the Index. cshtml Page code

@ {ViewBag. title = "chat window ";} 

8. Run the program directly and open multiple webpages to receive the same message .. As follows:

In conclusion, SignalR is easy to use and has simple configuration and powerful functions.

The above Asp.net SignalR makes real-time communication so simple, that is, all the content shared by Alibaba Cloud. I hope to give you a reference and support for the customer's house.

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.