In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how ASP.NET uses SignalR to establish a persistent connection between browser and server". In daily operation, it is believed that many people have doubts about how ASP.NET uses SignalR to establish a persistent connection between browser and server. The editor consulted all kinds of materials and sorted out simple and useful operation methods. I hope it will be helpful to answer the question of "how ASP.NET uses SignalR to establish a lasting connection between the browser and the server"! Next, please follow the editor to study!
Preface
The browser visits the web page through the HTTP protocol, the browser sends a request, and the server returns a result. The server passively receives requests. What if the server actively sends information to the browser if it wants to turn it around?
There are many solutions, such as round robin (browsers regularly ask the server if there is any new data), WebSocket (HTML 5).
SignalR is a combination of these technologies, it automatically identifies which ways the current browser supports, and then chooses the best one. When we develop, we don't have to pay attention to these details, SignalR will help us to implement, and SignalR is developed by Microsoft, easy to use is a consistent style.
Environment
.net 4.5 or above, not too low.
If the Visual Studio version is too low and there is no SignalR, follow these steps to add:
From the Visual Studio menu "tools-> Library package Manager-> NuGet packages that manage the solution (available after opening the solution)", search for "SignalR" in the pop-up dialog box.
The Visual Studio 2013 I use doesn't need to add SignalR manually, it comes with it.
Actual combat
Open Visual Studio (my version is 2013) and create an ASP.NET project.
The first step is to add a SignalR permanent connection class
As shown below:
After adding it, we can see that Visual Studio also automatically adds some references for us, as well as the Scripts folder (which contains jquery.signalR-2.0.0.js). The initial code for this class is as follows:
Public class MyConnection1: PersistentConnection {protected override Task OnConnected (IRequest request, string connectionId) {return Connection.Send (connectionId, "Welcome!");} protected override Task OnReceived (IRequest request, string connectionId, string data) {return Connection.Broadcast (data);}}
The second step is to add "OWIN Startup class"
Then add a bit of code:
Public class Startup1 {public void Configuration (IAppBuilder app) {/ / for more information about how to configure the application, please visit http://go.microsoft.com/fwlink/?LinkID=316888 app.MapSignalR ("/ myPath"); / / myPath is written casually. }}
MyConnection1 is the name of the class created in the first step.
Step 3: add a "Web form"
The running effect is as follows:
Expand it.
MyConnection1:
Method OnConnected: when a connection is created.
Method OnReceived: when the data submitted by the client is received, the parameter data is the data it receives.
Method OnDisconnected: when disconnected. It's not used on it.
Method OnReconnected: when reconnected. It's not used on it.
……
Connection attribute of MyConnection1:
Method Send: sends data to the browser, with the first parameter connectionId (string type) and the second parameter the data to be sent.
Method Broadcast: sends data to all browsers (all connections, to be exact).
JS:
Var conn = $.connection ("/ myPath"); create a connection object.
Conn.start (). Done () start () is the code to be executed when the connection starts and done () connects successfully.
Conn.received () is executed when you receive data from the server.
Conn.send () sends data to the server. It's not used on it.
Let's take a look at a more complete code:
The above is not to say that some methods have not been used, but the following example is more complete:
MyConnection1:
Protected override Task OnConnected (IRequest request, string connectionId) {return Connection.Send (connectionId, "Welcome!");} protected override Task OnReceived (IRequest request, string connectionId, string data) {Connection.Send (connectionId, "I received:" + data); return Connection.Broadcast ("attention: I received data from the client"). ;}
The code for the Web form:
Messages to be sent to the server:
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.