一.服務器推送技術Server Push詳解:
推送技術Server Push的基礎思想是將瀏覽器主動查詢信息改為服務器主動發送信息。服務器發送一批數據,瀏覽器顯示這些數據,同時保證與服務器的連接。當服務器需要再次發送一批數據時,瀏覽器顯示數據並保持連接。以后,服務器仍然可以發送批量數據,瀏覽器繼續顯示數據,依次類推。
二.推送達到的效果:

三.實現原理分析:
瀏覽器向服務器發出請求,服務器在連接數據庫,在數據庫中查找數據,若沒查找到,就(continue結束本次循環,進行下一次循環),如果找到了就取出數據,然后就可以break結束了(在查詢過程中很好資源,如果沒找到可以通過多線程休眠5s后,在進行下次循環!)
四.注意問題:
1.如果使用的MySql數據庫:mysql不支持top, top是Access的語法
應該使用limit 查詢:select * from user where name = 'xx' limit 1 注:limit 1(表示取第一條數據)
limit 2(表示取前兩條數據)
limit 1,2(從第一個開始,去兩條數據)
五.實現部分:
- 瀏覽器部分:
<html> <head> <title>ServerPush</title> <script src="jquery-2.1.4.js"></script> <script type="text/javascript"> var login= function() { var me = $("#me").val(); $.ajax({ type: "post", url: "ServerPush.ashx", data: { action: "login", me: me }, //me當前登陸的用戶 success: function (data) { $("#contest").append($("<li>" + data.Name + "對我說:" + data.Msg + "</li>")); login(); //繼續向服務器發送請求 }, error: function () { login(); //有時可能出現網絡異常,在這里重新發送請求 } }); } $(function () { $("#btnLogin").click(function () { //用戶登陸 $("#btnLogin").attr("disabled", "disabled"); //點擊登陸后就禁用這個按鈕 login(); //向服務器發送請求獲取發給我的數據 }); $("#btnSend").click(function () { //發送消息! var me = $("#me").val(); var toName = $("#toUserName").val(); var msg = $("#msg").val(); $.ajax({ type: "post", url: "ServerPush.ashx", data: { action: "send", toName: toName, msg: msg, me: me },//發送者:姓名和消息 success: function (data) { $("#contest").append($("<li>我對" + data.toName + "說:" + data.Msg + "</li>")); }, error: function () { alert("推送異常"); } }); }); }); </script> </head> <body> 我是:<input type="text" id="me" /><input type="button" id="btnLogin" value="登陸" /><br /> 發給:<input type="text" id="toUserName" /> 說:<input type="text" id="msg" /> <input type="button" id="btnSend" value="發送" /><br /> <br /> <ul id="contest"> </ul> </body>
-
- 服務器端為(一般處理程序(.ashx)):
1 public void ProcessRequest(HttpContext context) 2 { 3 context.Response.ContentType = "application/json"; 4 string action = context.Request["action"]; //獲取是登陸進來的,還是發送消息進來的 5 if (action == "login") 6 { 7 string user = context.Request["me"];//當前登陸用戶 8 while (true) 9 { //toName在數據庫中查詢發送我的所有消息 10 DataTable table = SqlHelper.ExecuteQuery("select *from t_serverPush where toName=@me limit 1", new MySqlParameter("@me", user)); 11 if (table.Rows.Count <= 0) 12 { 13 Thread.Sleep(500);//如果沒有查詢到數據就就休息500毫秒,避免對數據庫造成過大壓力 14 continue; 15 } 16 else 17 { 18 19 DataRow row = table.Rows[0]; 20 long id = (long)row["Id"]; 21 string me = (string)row["me"]; 22 string name = (string)row["toName"]; 23 string msg = (string)row["Msg"]; 24 SqlHelper.ExecuteNonQuery("delete from t_serverPush where Id=@id", new MySqlParameter("@id", id)); 25 var data = new { Name = me, Msg = msg }; 26 string json = new JavaScriptSerializer().Serialize(data); 27 context.Response.Write(json); 28 break; 29 } 30 } 31 32 } 33 else if (action == "send") //發送消息 34 { 35 string user = context.Request["me"]; 36 string toName = context.Request["toName"]; 37 string Msg =context.Request["Msg"]; 38 SqlHelper.ExecuteNonQuery("insert into t_serverPush (me,toName,Msg) values (@me,@name,@msg)", new MySqlParameter("@me", user), new MySqlParameter("@name", toName), new MySqlParameter("@msg", Msg)); 39 var data = new { toName = toName, Msg = Msg }; 40 string json = new JavaScriptSerializer().Serialize(data); 41 context.Response.Write(json); 42 } 43 else 44 { 45 throw new Exception("action異常"); 46 } 47 }
