Asp. Example analysis of serverpush usage in net

Source: Internet
Author: User
Tags anonymous continue json

The examples in this article describe the Serverpush usage in asp.net. Share to everyone for your reference. The specific analysis is as follows:

What is Serverpush, the server to the client "push", in fact, is "long connection"

Only the browser request server, the server to the browser to respond to data, will not actively push data to the browser, this is a security consideration, but also improve the performance of the server, if the server to the browser to actively push data, it is necessary to use the Serverpush technology simulation implementation.

As an example:

Message implementations are sent to each other through two pages, and messages are placed in the database.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 <summary>///ServerPush1 Summary description///</summary> public class Serverpush1:ihttphandler {public void Proc Essrequest (HttpContext context) {context. Response.ContentType = "Application/json"; String action = context. Request["Action"]; if (action = = "send")//Send {string me = context. Request["Me"]; String tousername = context. request["Tousername"]; String msg = context. request["MSG"]; Sqlhpler.executenonquery ("INSERT into T_msgs (fromusername,tousername,msg) VALUES (@FromUserName, @ToUserName, @Msg)" , New SqlParameter ("@FromUserName", Me), New SqlParameter ("@ToUserName", Tousername), New SqlParameter ("@Msg", MSG)); Context. Response.Write (New JavaScriptSerializer (). Serialize (New {Status = "OK"})); else if (action = = "Receive")//login, and continue to query and receive data from each other {///make a simple example to Serverpush1.ashx?me=sean//please send me a string me with the message to Sean = Context. Request["Me"]; while (true) {DataTable dt = sqlhpler.executequery (' Select top 1 * t_msgs WHERE tousername= @ToUserName ", New Sqlparam Eter ("@ToUserNAme ", ME)); if (dt. Rows.Count <= 0) {thread.sleep (500); not found, rest 500ms Requery, this avoids query pressure on the database, and consumes the Web server CPU resources continue;//the next while} else { DataRow row = dt. Rows[0]; Long id = (long) row["id"]; String fromusername = (string) row["Fromusername"]; String msg = (string) row["MSG"]; After the query to delete the message, otherwise there will be a dead loop, non-stop to the page output the same message sqlhpler.executenonquery ("DELETE from t_msgs WHERE id= @Id", New SqlParameter ("@Id ", id)); Create an anonymous object and save the query to the inside var data = new {Fromusername = fromusername, msg = msg, id = ID}; String json = new JavaScriptSerializer (). Serialize (data)//Converts an anonymous object to the JSON context. Response.Write (JSON);//Returns the request result to the break in JSON format; }} else {throw new Exception ("Action Error");}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 <! DOCTYPE html>

I hope this article will help you with your asp.net programming.

Related Article

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.