本文實(shí)例講述了ASP.NET中ServerPush用法。分享給大家供大家參考。具體分析如下:
什么是ServerPush,服務(wù)器向客戶端“推送“,其實(shí)就是”長連接“
只有瀏覽器請求服務(wù)器端,服務(wù)器端才給瀏覽器響應(yīng)數(shù)據(jù),不會(huì)主動(dòng)向?yàn)g覽器推送數(shù)據(jù),這是一種安全考慮,也是提高服務(wù)器的性能考慮,如果服務(wù)器向?yàn)g覽器主動(dòng)推送數(shù)據(jù),就要用到ServerPush等技術(shù)模擬實(shí)現(xiàn)。
舉個(gè)例子:
通過兩個(gè)頁面互相發(fā)送消息實(shí)現(xiàn),消息放到數(shù)據(jù)庫。
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
43
44
45
46
47
48
49
50
51
52
53
54
|
/// <summary> /// ServerPush1 的摘要說明 /// </summary> public class ServerPush1 : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "application/json" ; string action = context.Request[ "action" ]; if (action == "send" ) //發(fā)送 { 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" ) //登陸,并持續(xù)查詢、接收對方發(fā)過來的數(shù)據(jù) { //做一個(gè)簡單的例子,以ServerPush1.ashx?me=sean //請把發(fā)給sean的消息發(fā)給我一條 string me = context.Request[ "me" ]; while ( true ) { DataTable dt = SQLHpler.ExecuteQuery( "SELECT TOP 1 * FROM T_Msgs WHERE ToUserName=@ToUserName" , new SqlParameter( "@ToUserName" , me)); if (dt.Rows.Count <= 0) { Thread.Sleep(500); //沒找到,休息500ms再查詢,這樣避免對數(shù)據(jù)庫的查詢壓力,和占用WEB服務(wù)器CPU資源 continue ; //下一次while } else { DataRow row = dt.Rows[0]; long id = ( long )row[ "Id" ]; string fromUserName = ( string )row[ "FromUserName" ]; string msg = ( string )row[ "Msg" ]; //查詢完之后要?jiǎng)h除消息,否則會(huì)出現(xiàn)死循環(huán),不停的給頁面輸出同一個(gè)消息 SQLHpler.ExecuteNonQuery( "DELETE FROM T_Msgs WHERE Id=@Id" , new SqlParameter( "@Id" ,id)); //創(chuàng)建一個(gè)匿名對象,將查詢到的數(shù)據(jù)存到里面 var data = new { FromUserName = fromUserName, Msg = msg, Id = id }; string json = new JavaScriptSerializer().Serialize(data); //將匿名對象轉(zhuǎn)換為json context.Response.Write(json); //將請求結(jié)果以json格式返回 break ; } } } else { throw new Exception( "action錯(cuò)誤" ); } } |
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
43
44
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> <html xmlns= "http://www.w3.org/1999/xhtml" > <head> <meta http-equiv= "Content-Type" content= "text/html; charset=utf-8" /> <title></title> <script type= "text/javascript" src= "jquery-1.8.3.min.js" ></script> <script type= "text/javascript" > var rev = function () { var mine = $( '#me' ).val(); $.ajax({ type: 'post' , url: 'serverPush1.ashx' , data: { action: 'receive' , me: mine }, //傳給serverPush.ashx根據(jù)me查找發(fā)給me的消息 success: function (data) { $( '#ulMsg' ).append($( '<li>' + data.FromUserName + '對我說:' + data.Msg + '</li>' )); rev(); //收到消息后再向服務(wù)器請求數(shù)據(jù),再給我一條消息 }, error: function () { rev(); //哪怕網(wǎng)絡(luò)請求失?。ū热缬脩艟W(wǎng)絡(luò)故障),也再次發(fā)送請求 } }); }; $( function () { //發(fā)送 $( '#btnSend' ).click( function () { var myName = $( '#me' ).val(); var toUserName = $( '#toUserName' ).val(); var msg = $( '#msgContext' ).val(); $.ajax({ type: 'post' , url: 'serverPush1.ashx' , data: { action: 'send' , me: myName, toUserName: toUserName, msg: msg }, //根據(jù)用戶輸入的信息,傳到服務(wù)端ServerPush.ashx進(jìn)行插入操作 success: function (data) { if (data.Status == 'ok' ) { //如果發(fā)送成功, $( '#ulMsg' ).append($( '<li>我對' + toUserName + '說:' + msg + '</li>' )); $( '#msgContext' ).val( '' ); } else { alert( '發(fā)送出錯(cuò),返回報(bào)文無法識(shí)別' ); } }, error: function () { alert( '發(fā)送出錯(cuò)' ); } }); }); //登陸,接收數(shù)據(jù) $( '#btnLogin' ).click( function () { rev(); $( this ).attr( "disabled" , "disabled" ); }); /* $('#btnLogin').click(function () {//接收 var mine = $('#me').val(); $.ajax({ type: 'post', url: 'serverPush1.ashx', data: { action: 'receive', me: mine }, //傳給serverPush.ashx根據(jù)me查找發(fā)給me的消息 success: function (data) { $('#ulMsg').append($('<li>' + data.toUserName + '對我說:' + data.msg + '</li>')); }, error: function () { alert('接收失敗'); } }); });*/ }); </script> </head> <body> 發(fā)送人:<input type= "text" id= "me" /><input type= "button" id= "btnLogin" value= "登陸" style= "" /><br /> 接收人:<input type= "text" id= "toUserName" /><br /> 輸入消息:<input type= "text" id= "msgContext" /><input type= "button" id= "btnSend" value= "發(fā)送" /><br /> 聊天記錄:<br /> <ul id= "ulMsg" > </ul> </body> </html> |
希望本文所述對大家的asp.net程序設(shè)計(jì)有所幫助。