从网上copy了一个即时消息的方法,有时候会抛异常导致页面无法访问,错误内容提示为:
未将对象引用设置到对象的实例。 在 System.Web.HttpApplication.CallHandlerExecutionStep.InvokeEndHandler(IAsyncResult ar) 在 System.Web.HttpApplication.CallHandlerExecutionStep.OnAsyncHandlerCompletion(IAsyncResult ar)
using System;
using System.Collections.Generic;
using System.Web;
using System.Threading;
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["from"];
if (content != "-1")
{
string value = context.Request.Params["to"];
string events = context.Request.Params["event"];
if (value == null)
{
value = "";
}
if (events == null)
{
events = "";
}
content += "," + value + "," + events;
}
//向Message类中添加该消息
Messages.Instance().AddMessage(content, asyncResult);
return asyncResult;
}
public void EndProcessRequest(IAsyncResult result)
{
}
public bool IsReusable
{
get { return false; ; }
}
public void ProcessRequest(HttpContext context)
{
}
}
[Serializable]
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)
{
try
{
context.Response.Write(this.Content);
}
catch
{
HttpContext.Current.Response.Write("-1");
}
if (cb != null)
{
cb(this);
}
_IsCompleted = true;
}
}
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();
public 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配置为:
<system.web>
<httpHandlers>
<add path="/wait.aspx" type="AsnyHandler" verb="GET" />
</httpHandlers>
</system.web>
js方法:
function wait() {
$.get("/wait.aspx", { from: "-1", to: "", event: "" },
function (data, status) {
//有消息提示,与客户端交互
wait();
}, "html"
).fail(function () { wait(); });
}
- Carriage Return (ASCII chr 13) is missing from tex
- How to store image outside of the website's ro
- 'System.Threading.ThreadAbortException' in
- Request.PathInfo issues and XSS attacks
- How to dynamically load partial view Via jquery aj
你调试看看哪里没有判断是否为null