Because the project needs, you need to implement the push of the message on the Web page. Baidu search for a moment, found that the implementation of the message on the Web page push, you can use the ASP.net signalr class library, of course, can also use the H5 websocket Ajax reincarnation. Of course here we use the Signalr class library in asp.net. Because it can implement real-time push of the message on the Web page. What is real-time push, I simply say my own understanding of it. Real time: Things happen in the same time class, of course, in the computer is not absolute real-time, because the CPU in the same time slice can only handle one task, then this time the question again?
We usually use the computer and the Internet, and listen to music is how to achieve it, because the current CPU calculation speed is very fast. CPU will handle different tasks of the time slice, the CPU will be the time slice to a very small, very small, small to our human perception. For example, in the current time slice, the CPU is processing music tasks, in the next time, the CPU is processing the Internet task. So I think that there is no absolute real-time in the computer, but we are not aware of the human.
push: The push in here refers to the push on the page message. For example: Users A and B on their respective computers open an identical message to push the page. Suppose user A now sends a message to User B. It's going to go through one of those processes. User a->server-> User B. Of course, the concrete bottom of the implementation process, I do not discuss the ha. Because this is not the subject to be explored now. Because the server has an address that is generally fixed. So it is easier for the client to send messages to the server because the destination address is fixed. How does the server send a message to the client? This is a little bit. Because the address of the client is not fixed, and HTTP is stateless, the user's address cannot be remembered.
So to solve this problem, the ancestors of the computer used several methods:
1, the Client "Heartbeat". Visit the server every once in a while to see if the server has a task for its clients. The cycle of Ajax is the way it is used. The disadvantage is that the real time is not too high.
2, the server and the client long connection, this article is going to talk about the SIGNALR is the use of this idea. Disadvantage: The pressure on the server is large.
OK, let's talk about SIGNALR now. SIGNALR is a library for ASP.net developers who can simplify the process of adding live WEB functionality to applications by developers. Real-time WEB functionality is a feature in which server code can immediately push content to a connected client when it becomes available, rather than having the server wait for the client to request new data. This also implements the real-time message push. My personal understanding of the implementation of the principle is first by the server to customize a function for a client call to send messages to another client. Of course, the client also needs to set a function. Because the server needs to invoke this function of the client.
The following is the specific way to operate it.
1, Environment:win 10+vs2015 Community Edition
I use ASP.net mvc. Open the VS 2015| file first | new | project (SIGNALRMVC) |asp.net Web application | Empty template, MVC, platform that's probably it.
Now say the specific document you need to include.
1), SIGNALR Hub class. A function that writes a Shantiu call to a client segment.
2), Owin class. The function to register the server.
3, the front page (including the foreground of the message box writing, function writing) of course, the front desk needs some documents.
In general vs no signalr class, we need to add this feature before we start the task. Select vs Tools | NuGet Package Manager | NuGet Package Manager Console | Install-package Microsoft.Aspnet.SignalR to install SIGNALR. After installation, 1, we create a new folder in the project to chathubs| a new SIGNALR hub class, and write the following code:
Using Microsoft.AspNet.SignalR;
Namespace Signalrmvc.chathubs
{public
class Chathub:hub
{public
void SendMessage (string name, String message)
{
//Clients.All.hello ();
Clients.All.receiveMessage (name, message);
The user invokes the client's function}}}
2, under the Chathubs folder to create a new Owin class. and write the following code:
Using Microsoft.owin;
Using Owin;
[Assembly:owinstartup (typeof (SignalRMvc.ChatHubs.Startup))]
Namespace Signalrmvc.chathubs
{public
class Startup
{public
void Configuration (Iappbuilder app )
{
app. MAPSIGNALR ();
Hub registration of the server}}
3. Create a new home controller in controllers. and write the following code:
Using SYSTEM.WEB.MVC;
Namespace Signalrmvc.controllers
{public
class Homecontroller:controller
{
//Get:home
Public ActionResult Clientchat ()
{return
View ();
}
}
}
4. Right-click on the Controller's method to add a view (without using a template or Layout page). and write the following code:
@{Layout = null;} <! DOCTYPE html>
If the direct copy is used. Be aware of the directory and version of the files introduced in the foreground code. The first letter of the foreground code is best used in lowercase, and the first letter of the background code is best capitalized. Because JS defaults to use the Hump nomenclature, C Sharp use Pascal naming method. It's easy to make mistakes without paying attention to this detail. Because the background code in the implementation of the dynamic generation of some JS code, JS code default use of the Hump naming method. If your code in the foreground is named Pascal, it's easy to make a mistake. And it's not easy to find. I've had a personal experience.
Below we are under the local test: Firefox and Chrome to simulate the two clients, of course, its own computer also on the server. The effect chart is as follows:
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.