国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網(wǎng)站 > 建站經(jīng)驗 > 正文

ASP.NET中Server!Push用法實例分析

2019-11-02 15:41:38
字體:
供稿:網(wǎng)友

 本文實例講述了ASP.NET中ServerPush用法。分享給大家供大家參考。具體分析如下:

什么是ServerPush,服務(wù)器向客戶端“推送“,其實就是”長連接“

只有瀏覽器請求服務(wù)器端,服務(wù)器端才給瀏覽器響應(yīng)數(shù)據(jù),不會主動向瀏覽器推送數(shù)據(jù),這是一種安全考慮,也是提高服務(wù)器的性能考慮,如果服務(wù)器向瀏覽器主動推送數(shù)據(jù),就要用到ServerPush等技術(shù)模擬實現(xiàn)。

舉個例子:

通過兩個頁面互相發(fā)送消息實現(xiàn),消息放到數(shù)據(jù)庫。

1 2 3
好看的暴走漫畫[www.62-6.com/1/baozoumanhua/]
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ù) { //做一個簡單的例子,以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 [email protected]",new SqlParameter("@ToUserName", me)); if (dt.Rows.Count <= 0) { Thread.Sleep(500);//沒找到,休息500ms再查詢,這樣避免對數(shù)據(jù)庫的查詢壓力,和占用WEB服務(wù)器
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 滕州市| 河曲县| 明溪县| 天气| 葵青区| 定西市| 萍乡市| 龙山县| 定兴县| 成都市| 固镇县| 岑溪市| 贵定县| 喀什市| 灵山县| 历史| 峨边| 壤塘县| 长沙县| 揭阳市| 封开县| 洪江市| 北票市| 全椒县| 昌吉市| 东港市| 文水县| 富锦市| 曲阜市| 德阳市| 旺苍县| 游戏| 砀山县| 凤山县| 略阳县| 洞口县| 平乐县| 抚松县| 荃湾区| 辉县市| 昆山市|