- public class WebAsyncResultCollection : List<WebAsyncResult>, ICollection<WebAsyncResult>
- {
- private static WebAsyncResultCollection _instance = new WebAsyncResultCollection();
- public static WebAsyncResultCollection Instance
- {
- get { return WebAsyncResultCollection._instance; }
- }
- public bool SendMessage(string clientID, string message)
- {
- var result = this.FirstOrDefault(r => r.ClientID == clientID);
- if (result != null)
- {
- Remove(result);
- bool sendsuccess = false;
- if (result.Context.Response.IsClientConnected)
- {
- sendsuccess = true;
- result.Context.Response.Write(message);
- }
- result.SetComplete();
- return sendsuccess;
- }
- return false;
- }
- }
对于异步处理程序的开始方法,我们收集信息并放入集合。
- public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData)
- {
- var clientID = context.Request.QueryString["id"];
- WebAsyncResultCollection.Instance.SendMessage(clientID, "ClearClientID");
- WebAsyncResult result = new WebAsyncResult(cb, context, clientID);
- WebAsyncResultCollection.Instance.Add(result);
- return result;
- }
【不完善】由于客户端收到一次消息后结束请求,由客户端再次发起请求,中间会有