ASP. NET SignalR topic--The second quick explanation of persistentconnection persistent connection

Source: Internet
Author: User

We quickly set up a small case, but there is no way to introduce the method, this one I will analyze each.

One: From the override of the methods mentioned

Anyway, let's start with the code, as follows:

1      Public classmyconnection1:persistentconnection2     {3         protected OverrideTask onconnected (irequest request,stringConnectionID)4         {5             returnConnection.send (ConnectionID,"welcome!");6         }7 8         protected OverrideTask onreceived (irequest request,stringConnectionID,stringdata)9         {Ten             returnconnection.broadcast (data); One         } A}

The first thing we know is that all of the persistent connection classes are inherited from persistentconnection, so the question of interest is coming, persistentconnection in the end are defined

What??? So we use F12 to persistent.

In the above diagram, it is clear that the original persistent connection is not just the onconnected and onreceived methods, but also a drop-off ondisconnected event and a re-connected

Onreconnected event.

1. ondisconnected Demo

From this name can be seen out, dropped the line can trigger this event, right, OK, I can open the page, then refresh the page can trigger the Ondisconnected event, for

For a convenient demonstration, I added the Debug.WriteLine method to each method, such as:

1    Public classmyconnection1:persistentconnection2     {3         protected OverrideTask onconnected (irequest request,stringConnectionID)4         {5Debug.WriteLine ("onconnected");6             returnConnection.send (ConnectionID,"welcome!");7         }8 9         protected OverrideTask onreceived (irequest request,stringConnectionID,stringdata)Ten         { OneDebug.WriteLine ("onreceived"); A             returnconnection.broadcast (data); -         } -  the         protected OverrideTask ondisconnected (irequest request,stringConnectionID,BOOLstopcalled) -         { -Debug.WriteLine ("ondisconnected"); -             return Base. ondisconnected (Request, ConnectionID, stopcalled); +         } -  +         protected OverrideTask onreconnected (irequest request,stringConnectionID) A         { atDebug.WriteLine ("onreconnected"); -             return Base. onreconnected (Request, ConnectionID); -         } -}

From the three msg you can see, this is after the page renders, and then refresh the operation.

2. onreconnected Events

The demonstration of this event is also very simple, when the page is rendered, then shut down the IIS server, that is, iisexpress, and then re-open iisexpress, so that you can trigger

Onreconnected events, such as the following.

As you can see from the output console and the current Chrome page, the process of the first tab page is onconnected = ondisconnected = onreconnected, right, section

Two tab page, which is the second onconnected event, is a new page opened after restarting.

II: Connection link

The second area that concerns us is connection, because it is a link management class for managing the sending and exporting of information, such as:

You can see that this property is the Iconnection type, and then it's curious to see what kind of method is available for us to use, through the following figure, we see that there are only two fun ways, the first one

The broadcast is used to broadcast messages, as the name implies, is to send a message to the client that is linked to the server, and a send to a specific person.

Well, in the previous article, I just showed that the server pushes the message to the client and doesn't show how the client pushes the message to the server, but it's also very simple, because we have a powerful signalr.js,

For the convenience of me just after the start succeeds, add a Send method to the server to push the information, of course, you can add the Send method to a click event has been adapted to your business logic.

1<! DOCTYPE html>234<meta http-equiv="Content-type"Content="text/html; Charset=utf-8"/>5<title></title>6<meta charset="Utf-8"/>78<body>9<script src="/scripts/jquery-1.10.2.js"></script>Ten<script src="/scripts/jquery.signalr-2.1.2.js"></script> One<script type="Text/javascript"> A         varconn = $.connection ("/myconnection"); -  - Conn.start (). Done (function (data) { theConsole.log ("Current clientid="+data.id); -  -            conn.send ("Damn, I've got it.") "); -         }); +  -         //accept push from server + conn.received (function (data) { AConsole.log ("data returned by the server:"+data); at         }); -</script> -</body> -

The above-mentioned outline of the next process, I think so far, your persistent connection building should not be a problem, although there are group operations, but this does not prevent you to use SIGNALR to develop, OK,

This is the first to talk about this, follow up and then slowly.

ASP. NET SignalR topic--The second quick explanation of persistentconnection persistent connection

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.