标签:
1 ServerPush
ServerPush表示服务器端推送,实际是浏览器一直请求服务器,服务器一直等待 直到找到一条数据后立即跳出while返回页面。
<!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 src="file/jquery-1.11.2.js"></script> <script type="text/javascript"> //登录,一直请求服务器推送信息 var recv = function() { var me = $(‘#me‘).val().trim(); $.ajax({ type: ‘post‘, url: ‘ServerPushChat.ashx‘, data: { me: me, action: ‘receive‘ }, success: function (data) { $(‘#dv‘).append($(‘<p>‘ + data.data.FROMUSERNAME + ‘对我说:‘ + data.data.MSG + ‘</p>‘)); //无论成功还是失败,都需要再次向服务器请求‘给哥一条消息’ recv(); }, error: function () { //alert(‘服务器错误‘); recv(); } }); } $(function () { //点击登录,使服务器用serverPush推送信息 $(‘#btnLogin‘).click(function () { recv(); $(this).attr(‘disabled‘, ‘disabled‘); }); //发送,发送给服务器信息 $(‘#btnSend‘).click(function () { var me = $(‘#me‘).val().trim(); var toUserName = $(‘#toUserName‘).val().trim(); var msg = $(‘#msg‘).val().trim(); $.ajax({ type: ‘post‘, url: ‘ServerPushChat.ashx‘, data: { me: me, toUserName: toUserName, msg:msg,action: ‘send‘ }, success: function (data) { $(‘#dv‘).append($(‘<p>我对‘ + data.data.TOUSERNAME + ‘说:‘ + data.data.MSG + ‘</p>‘)); }, error: function () { alert(‘服务器错误‘); } }); }); }) </script> </head> <body> <div> 我:<input type="text" id="me" /><input type="button" id="btnLogin" value="登录" /><br /> 你:<input type="text" id="toUserName" /><br /> 内容:<input type="text" id="msg" /><input type="button" id="btnSend" value="发送" /><br /> </div> <div id="dv"> </div> </body> </html>
using Console_Core.BLL; using Console_Core.Model; using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Web; using System.Web.Script.Serialization; namespace Web_Cassini.Day6 { /// <summary> /// ServerPushChat 的摘要说明 /// </summary> public class ServerPushChat : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "application/json"; string action = context.Request["action"]; if (action == "receive") { #region 请求服务器一直推送信息 string me = context.Request["me"]; //验证 -- while (true) { List<object> list = new MyORM_BLL().SelectModelByField(typeof(TC_SERVERPUSHCHAT), "TOUSERNAME=‘" + me + "‘"); if (list.Count <= 0) { Thread.Sleep(500); continue; } else { TC_SERVERPUSHCHAT chat = list[0] as TC_SERVERPUSHCHAT; bool flag = new MyORM_BLL().DeleteModelById(typeof(TC_SERVERPUSHCHAT), (int)chat.ID); //把客户接收到的数据删除 context.Response.Write(new JavaScriptSerializer().Serialize(new { status = "ok", data = chat, msg = "接收到一条数据" })); break; //接收到一条数据 立即跳出while 返回页面 } } #endregion } else if (action == "send") { string me = context.Request["me"]; string toUserName = context.Request["toUserName"]; string msg = context.Request["msg"]; //验证 --- TC_SERVERPUSHCHAT chat=new TC_SERVERPUSHCHAT(); chat.FROMUSERNAME=me; chat.TOUSERNAME=toUserName; chat.MSG=msg; bool flag = new MyORM_BLL().InsertModel(chat, "SE_TC_STUDENT"); context.Response.Write(new JavaScriptSerializer().Serialize(new { status = "ok", data = chat, msg = "发送成功" })); } else { throw new Exception("action错误:"+action); } } public bool IsReusable { get { return false; } } } }
标签:
原文地址:http://www.cnblogs.com/adolphyang/p/4806001.html