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

What is the use of SignalR in ASP.NET MVC

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

Share

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

Editor to share with you what is the use of SignalR in ASP.NET MVC, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

I. brief introduction

ASP.NET SignalR is a library for ASP.NET developers that simplifies the process for developers to add real-time Web functionality to their applications. Real-time Web functionality refers to the ability for server code to push content to a connected client as soon as it becomes available, rather than having the server wait for the client to request new data. Baidu encyclopedia

First of all, ASP.NET SignalR is a class library under ASP.NET, which can realize real-time communication in ASP.NET 's Web project. Allows the client (Web page) and the server to notify each other of messages and invoke methods.

SignalR automatically handles connection management, allowing the server to broadcast messages to all connected clients at the same time, such as chat rooms. You can also send messages to specific clients. The connection between the client and the server is persistent, and unlike a traditional HTTP connection, it is re-established for each communication.

SignalR supports the "server push" feature, where server code can use remote procedure call (RPC) to call client code in the browser, rather than the request response model that is common on the web today.

In short, SignalR is a html websocket framework running on the .NET platform, and its main purpose is to enable the server to actively push (Push) messages to client pages.

Note: WebSocket requires the server to use Windows Server 2012 or Windows 8 and .NET Framework 4.5. If these requirements are not met, SignalR will try to connect using other transports.

II. Installation

Open the administrative NuGet package, search for SignalR, and install the following packages

There will be more references in the program after the installation is complete.

Third, write code

Because you are using SignalR2, you need to create a new Startup.cs class, configure the hub, and write the following

Using Microsoft.Owin;using Owin; [assembly: OwinStartup (typeof (SignalRStartup.Startup))] namespace SignalRStartup {public class Startup {public void Configuration (IAppBuilder app) {/ / configure hub app.MapSignalR ();}

Then write the server-side hub class

Using System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;using System.Web;using Microsoft.AspNet.SignalR;namespace signalR {public class ServerHub: Hub {public void SendMsg (string message) {/ / call all client sendMessage methods (sendMessage has 2 parameters) Clients.All.sendMessage (DateTime.Now.ToString ("yyyy-MM-dd HH:mm:ss"), message);}

Create HomoController and its Action function Index

Using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;namespace signalR.Controllers {public class HomeController: Controller {public ActionResult Index () {return View ();}

Index preceding code

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

@ section scripts {$(function () {/ / reference the automatically generated hub agent var chat = $.connection.serverHub; / / define the client sendMessage called by the server to display the new message chat.client.sendMessage = function (name, message) {/ / add the message $("# messageBox") .serverHub (''+ name+':'+message+'') to the page } / / set the focus to the input box $('# message'). Focus (); / / start to connect to the server $.connection.hub.start () .done (function () {$('# sendmessage') .click (function () {/ / call the Send method chat.server.sendMsg ($('# message'). Val ()) of the server-side hub / / clear the input box information and get focus $("# message") .val (') .focus ();});}

Running effect, send a message in any window, and all other clients can receive the message.

When you run the program, the Web page establishes a connection with the SignalR service, and the specific code to establish the connection is: $.connection.hub.start (). The purpose of this code is to establish a connection with the SignalR service. The following done function indicates that a click event is registered for the button after the connection is successfully established; you can also use the hub object chat.connextion.start ()

Do you remember that?

The results seen by F12

The Clients.All.sendMessage in the above demo calls the sendMessage function of all clients and belongs to mass sending.

The following is a client grouped demo

Server code

Public void AddToRoom (string groupName, string userName) {/ / add the connection to the specified group (Groups is an interface attribute in HubBase) Groups.Add (Context.ConnectionId, groupName); / / get the group of the corresponding client according to the group name, and call the group's addUserIn method Clients.Group (groupName, new string [0]) .addUserin (groupName, userName) } public void Send (string groupName, string detail, string userName) {/ / Clients.All.addSomeMessage (detail); / / the group sends addSomeMessage Clients.Group (groupName, new string [0]) .addSomeMessage (groupName, detail, userName) to all / / calling clients of a group;}

Client code

Chat.client.addSomeMessage = function (groupId, detail, userName) {console.info ("broadcast message:" + detail); $("# contentMsg"). Append ("" + userName + ":" + detail + ");}; chat.client.addUserIn = function (groupId, userName) {$(" # contentMsg "). Append ("+ userName +" enter the chat room "+ groupId+"! ") ;}; $.connection.hub.logging = true; / / launch the signalr status feature $.connection.hub.start () .done (function () {/ / join the chat room $("# joinRoom") .click (function () {var groupId = $("# groupId") .val (); var userName = $("# userName") .val () Chat.server.addToRoom (groupId, userName);}); / / send message $("# send") .click (function () {var detail = $("# message"). Val (); var groupId = $("# groupId"). Val (); var userName = $("# userName"). Val (); chat.server.send (groupId, detail, userName) );})

Running effect

As can be seen from the above two pictures, the client has implemented the grouping

The above is all the content of this article "what is the use of SignalR in ASP.NET MVC?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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