Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use SignalR in Asp.net

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how to use SignalR in Asp.net. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

In the patrol project, you need to send real-time messages and task start reminders, so you have the opportunity to get in touch with SignalR. In the course of use, you have found that it is very easy to communicate with SignalR. I would like to share it from three aspects:

What is SignalR

Asp.net SignalR is a class library used by Microsoft to realize real-time communication. In general, SignalR uses JavaScript's long polling (long polling) to achieve client-server communication. With the emergence of WebSockets in Html5, SignalR also supports WebSockets communication. In addition, the programs developed by SignalR are not only limited to hosting in IIS, but also can be hosted in any application, including console, client programs and Windows services, etc., and also support Mono, which means that it can be deployed across platforms in the Linux environment.

There are two types of objects within SignalR:

Http persistent connection (Persisten Connection) object: a function used to solve long-term connections. It is also possible for the client to actively request data from the server, and the server does not need to implement too many details, but only needs to deal with the five events provided in the PersistentConnection: OnConnected, OnReconnected, OnReceived, OnError and OnDisconnect.

Hub (hub) object: used to solve the function of real-time (realtime) information exchange. The server can use URL to register one or more Hub. As long as it connects to this Hub, it can share the information sent to the server with all clients. At the same time, the server can call the client script.

SignalR encapsulates the exchange of the whole information, and both the client and the server use JSON to communicate. All the Hub information declared on the server will generate JavaScript and output to the client, .NET relies on Proxy to generate proxy objects, and the interior of Proxy is to convert JSON into objects.

2. Why use SignalR

Chat rooms, such as online customer service system, IM system, etc.

Real-time push service for messages

Real-time push of Patrol location

Third, how to achieve SignalR, the following mainly introduces the first major function of SignalR, chat. Here is a simple DEMO:

1. Create a new asp.net web application

2. Select the template MVC and change no authentication at the same time

3. Select the new project, right-click-- > Select manage NuGet package-- > search signalr-- > install Microsoft ASP.NET SignalR

4. Create a new startup program Startup.cs

Add code to the class:

App.MapSignalR ()

5. Create a new SignalR hub class ChatHub.cs

6. Add the following code to the hub class ChatHub.cs

Public class ChatHub: Hub {/ public void Send (string message) {var name = Guid.NewGuid () .ToString () .ToUpper (); / / call all client-side sendMessage methods Clients.All.sendMessage (name, message) } / public override Task OnConnected () {Trace.WriteLine ("client connection succeeded"); return base.OnConnected ();}}

7. Modify the Index.cshtml page code

@ {ViewBag.Title = "chat window";} Chat @ section scripts {$(function () {/ / 1, references to auto-generated hub agents must begin with lowercase letters var chat = $. Connection.chatHub / / 2. The done function to start a connection to the server indicates that a click event $.connection.hub.start () .done (function () {$('# sendmessage') .click (function () {/ / call the Send method of the server-side hub should also begin with lowercase chat.server.send ($('# message'). Val ()) after the connection is successfully established. / / clear the input box information and get the focus $('# message'). Val ('). Focus ();}) / / 3. Define the client sendMessage called on the server side to display the new message chat.client.sendMessage = function (name, message) {/ / add the message $('# discussion'). Append (''+ htmlEncode (name) +':'+ htmlEncode (message) +');}; / / set the focus to the input box $('# message'). Focus ();}) / / Html encoding function htmlEncode (value) {var encodedValue = $('') .text (value) .html (); return encodedValue;}} for the displayed message

8. If you run the program directly and open multiple web pages, you can receive the same message. As follows:

To sum up, SignalR is easy to use, easy to configure and powerful.

This is the end of the article on "how to use SignalR in Asp.net". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report