Comet
PustLet
WebSocket
Ajax长连接
function connection(){ $.ajax({ type:"GET", url:"/api2/session/event?", data:{ "uid":obj.uid, "sessionID":"cool" }, success:function(serverData){ connection(); }, error:function(XMLHttpRequest, textStatus, errorThrown){ connection(); } }); }
(一)当用户登陆时,发送等待请求给服务器wait()
function wait() { $.post("comet_broadcast.asyn", { content: "-1" }, function (data, status) { var result = $("#divResult"); result.html(result.html() + "<br/>" + data); //服务器返回消息,再次立连接 wait(); }, "html" ); }
向服务器发送信息send()
function send() { // 向comet_broadcast.asyn发送请求 // 消息体为文本框content中的内容 // 请求接收类为AsnyHandler $.post("comet_broadcast.asyn", { content: $("#content").val() }); //清空内容 $("#content").val(""); }
//初始化连接
wait();
(二)AsnyHandler异步处理
using System; using System.Collections.Generic; using System.Web; using System.Threading; /// <summary> /// Summary description for AsnyHandler /// </summary> public class AsnyHandler:IHttpAsyncHandler { public AsnyHandler() { } public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData) { //myAsynResult为实现了IAsyncResult接口的类,当不调用cb的回调函数时 ,该请求不会返回到给客户端,会一直处于连接状态 myAsynResult asyncResult = new myAsynResult(context, cb, extraData); String content = context.Request.Params["content"]; //向Message类中添加该消息 Messages.Instance().AddMessage(content, asyncResult); return asyncResult; } #region public void EndProcessRequest(IAsyncResult result) { } public bool IsReusable { get { return false; ; } } public void ProcessRequest(HttpContext context) { } #endregion } public class myAsynResult : IAsyncResult { bool _IsCompleted = false; private HttpContext context; private AsyncCallback cb; private object extraData; public myAsynResult(HttpContext context, AsyncCallback cb, object extraData) { this.context = context; this.cb = cb; this.extraData = extraData; } private string _content; public string Content { get { return _content; } set { _content=value; } } #region IAsyncResult接口 public object AsyncState { get { return null; } } public System.Threading.WaitHandle AsyncWaitHandle { get { return null; } } public bool CompletedSynchronously { get { return false; } } public bool IsCompleted { get { return _IsCompleted; } } #endregion //在Message类中的添加消息方法中,调用该方法,将消息输入到客户端,从而 实现广播的功能 public void Send(object data) { context.Response.Write(this.Content); if (cb!=null) { cb(this); } _IsCompleted = true; ; } }
(三)Message
using System; using System.Collections.Generic; using System.Web; /// <summary> /// Summary description for Messages /// </summary> public class Messages { //记录所有请求的客户端 List<myAsynResult> clients = new List<myAsynResult>(); #region 实现该类的单例 private static readonly Messages _Instance = new Messages(); private Messages() { } public static Messages Instance() { return _Instance; } #endregion public void AddMessage(string content,myAsynResult asyncResult) { //当传入的内容为"-1"时,表示为建立连接请求,即为了维持一个从客户端到服务器的连接而建立的连接 //此时将该连接保存到 List<myAsynResult> clients中,待再有消息发送过来时,该连接将会被遍历,并且会将该连接输出内容后,结束该连接 if (content=="-1") { clients.Add(asyncResult); } else { //将当前请求的内容输出到客户端 asyncResult.Content = content; asyncResult.Send(null); //否则将遍历所有已缓存的client,并将当前内容输出到客户端 foreach (myAsynResult result in clients) { result.Content = content; result.Send(null); } //清空所有缓存 clients.Clear(); } } }
(四)Web.Config
<?xml version="1.0"?> <configuration> <system.web> <httpHandlers> <add path="comet_broadcast.asyn" type="AsnyHandler" verb="POST,GET"/> </httpHandlers> <compilation debug="true"/></system.web> </configuration>
出现的问题:
1.加入到IIS为什么就无法运行?
2.添加了async的处理程序还是无法运行?
3.HTTP长连接是Comet还是PushLet还是WebSocket?或者是轮询还是iframe流?
内容参考:http://www.cnblogs.com/hanxianlong/archive/2010/04/27/1722018.html