ASP.NET using Jquery LigerUI polling message push to achieve with Ajax

Polling is the establishment of a clock in the client, from time to time to send a request to the server , access server data, is one way to achieve message push simplest and most easy to implement, the biggest drawback browser requires constant request to the server that take up a lot of bandwidth, resulting in a waste of resources

Here is the JS code:

 1 $(document).ready(function () {
 2             setInterval(invoke, 20000);
 3         });
 4         function invoke() {
 5             var xhr = $.ajax({
 6                 type: "POST",
 7                 dataType: 'json',
 8                 url: "Sys_msg.get_unRead_count.xhd",
 9                 data: "",
10                 success: function (result) {
11                     var obj = eval(result);
12                     IF (obj> MSG_COUNT)
 13 is                      {
 14                          $ .ligerTip ({
 15                              Content: "You have <font color = red>" + (obj-msg_count) + "</ font> Article new unread messages. Please check! <br> <a href='javascript:void(0)' onclick=\"window.top.f_addTab('Sys_msg','消息管理','personal/message/sys_msg.aspx')\"> into the process < / A> " ,
 16                              width: 240 ,
 . 17                              // var winH = $ (window) .height (), winW = $ (window) .width (); 
18 is                              X: tIPW - 260.,   // set the position of the bubble, the bubble lower right corner of the display 
. 19                              Y: tipH - 80 ,
 20 is                             the callback: function (T) {
 21 is                                  var I = 10; // 10-second countdown 
22 is                                  CalcTime (T, I);
 23 is                              }
 24                          });
 25                      }
 26 is                      MSG_COUNT = obj;
 27                      
28                  }
 29              });
 30          }
 31 is      var = $ tipH (window) .height (), tIPW = $ (window) .width ();     // Get screen size 
32      function CalcTime (T, I) //This function is achieved by the recursive countdown 
33 is          {
 34 is              i-- ;
 35              (I> = 0 )
 36          the setTimeout (? Function () {
 37 [              CalcTime (T, I);
 38 is          }, 1000 )
 39          : $ (T). Remove ();
 40          }

Background Code:

 1 public class Sys_msg
 2     {
 3         public static BLL.Sys_msg msg = new BLL.Sys_msg();
 4         public static Model.Sys_msg model = new Model.Sys_msg();
 5 
 6         public HttpContext Context;
 7         public string emp_id;
 8         public string emp_name;
 9         public HttpRequest request;
10         public string uid;
11         
12 
13         public Sys_msg()
14         {
15         }
16 
17         public Sys_msg(HttpContext context)
18         {
19             Context = context;
20             request = context.Request;
21 
22             var userinfo = new User_info();
23             employee = userinfo.GetCurrentEmpInfo(context);
24 
25             emp_id = employee.id;
26             emp_name = PageValidate.InputText(employee.name, 50);
27             uid = PageValidate.InputText(employee.uid, 50);
28             
29         }
30 
31 
32         public int get_unRead_count()
33         {
34             string serchtext = $" 1=1 ";
35             //int rowcount = 0;
36             serchtext += " and isRead=0";
37             serchtext += " and UserID like N'%" + emp_id + "%'";
38             DataSet ds = msg.GetList(serchtext);
39             //rowcount = ds.Tables[0].Rows.Count;
40             return ds.Tables[0].Rows.Count;
41         }
42 
43     }

 

Guess you like

Origin www.cnblogs.com/Alvis-Lv/p/11130309.html