C# 让这个httpHandler更健壮?
我有以下HttpHandler;我使用它将更新推送到浏览器(jQuery和GrowlUI是在这里实现的),而不需要浏览器进行轮询。我想我所完成的就是将轮询循环移动到服务器 谁能告诉我如何使这个类更健壮和可伸缩 这是代码C# 让这个httpHandler更健壮?,c#,asp.net,httphandler,robust,C#,Asp.net,Httphandler,Robust,我有以下HttpHandler;我使用它将更新推送到浏览器(jQuery和GrowlUI是在这里实现的),而不需要浏览器进行轮询。我想我所完成的就是将轮询循环移动到服务器 谁能告诉我如何使这个类更健壮和可伸缩 这是代码 public class LiveUpdates : IHttpHandler { //TODO: Replace this with a repository that the application can log to. private static rea
public class LiveUpdates : IHttpHandler
{
//TODO: Replace this with a repository that the application can log to.
private static readonly Dictionary<string, Queue<string>> updateQueue;
static LiveUpdates()
{
updateQueue = new Dictionary<string, Queue<string>>();
}
public void ProcessRequest(HttpContext context)
{
context.Response.Buffer = true;
while (context.Response.IsClientConnected)
{
if (context.User == null) return;
if (!context.User.Identity.IsAuthenticated) return;
Thread.Sleep(1000);
if (!updateQueue.ContainsKey(context.User.Identity.Name)) continue;
if (updateQueue[context.User.Identity.Name].Count == 0) continue;
GrowlStatus(context.Response, updateQueue[context.User.Identity.Name].Dequeue());
}
}
protected static void GrowlStatus(HttpResponse Response, string Message)
{
// Write out the parent script callback.
Response.Write(String.Format("<script type=\"text/javascript\">parent.$.growlUI('Message', '{0}');</script>", Message));
// To be sure the response isn't buffered on the server.
Response.Flush();
}
public static void QueueUpdate(IPrincipal User, string UpdateMessage)
{
if (!updateQueue.ContainsKey(User.Identity.Name))
{
updateQueue.Add(User.Identity.Name, new Queue<string>());
}
updateQueue[User.Identity.Name].Enqueue(UpdateMessage);
}
public static void ClearUpdates(IPrincipal User)
{
if (updateQueue.ContainsKey(User.Identity.Name)) updateQueue.Remove(User.Identity.Name);
}
公共类LiveUpdates:IHttpHandler
{
//TODO:将其替换为应用程序可以登录的存储库。
私有静态只读字典更新;
静态LiveUpdates()
{
updateQueue=新字典();
}
公共void ProcessRequest(HttpContext上下文)
{
context.Response.Buffer=true;
while(context.Response.IsClientConnected)
{
if(context.User==null)返回;
如果(!context.User.Identity.IsAuthenticated)返回;
睡眠(1000);
如果(!updateQueue.ContainsKey(context.User.Identity.Name))继续;
如果(updateQueue[context.User.Identity.Name].Count==0)继续;
GrowlStatus(context.Response,updateQueue[context.User.Identity.Name].Dequeue());
}
}
受保护的静态void GrowlStatus(HttpResponse响应,字符串消息)
{
//写出父脚本回调。
Write(String.Format(“parent.$.growlUI('Message','{0}');”,Message));
//确保响应未在服务器上缓冲。
Response.Flush();
}
公共静态无效队列更新(IPrincipal用户,字符串更新消息)
{
如果(!updateQueue.ContainsKey(User.Identity.Name))
{
updateQueue.Add(User.Identity.Name,new Queue());
}
updateQueue[User.Identity.Name].Enqueue(UpdateMessage);
}
公共静态无效ClearUpdate(IPrincipal用户)
{
if(updateQueue.ContainsKey(User.Identity.Name))updateQueue.Remove(User.Identity.Name);
}
QueueUpdate是如何调用的?我注意到您从中提取字符串,并将其直接放入发送给用户的javascript中。用户是否有可能将javascript插入条目中,并让QueueUpdate以某种方式显示出来
此外,在将消息放入javascript字符串之前,我会将消息与有效的消息正则表达式进行匹配。似乎有人可以完成您的growlUi调用,然后非常轻松地插入自己的javascript。至少您可以确保插入的消息不包含单引号(')它可以终止字符串并开始一个新的javascript命令
也许这只是一种妄想,但它会使它更加健壮:)QueueUpdate是如何调用的?我注意到您从中提取字符串并将其直接放入发送给用户的javascript中。用户是否有可能将javascript插入条目中,并让QueueUpdate以某种方式显示出来 此外,在将消息放入javascript字符串之前,我会将消息与有效的消息正则表达式进行匹配。似乎有人可以完成您的growlUi调用,然后非常轻松地插入自己的javascript。至少您可以确保插入的消息不包含单引号(')它可以终止字符串并开始一个新的javascript命令
也许这只是一种妄想,但它会使它更加健壮:)如果您计划使用
Thread.Sleep()
,您必须实现,否则您的处理程序将无法扩展。如果您计划使用Thread.Sleep())
,您必须实现,否则您的处理程序将无法扩展。有趣的是,我将对此进行研究。同时阅读以下内容:是的,我无法发布答案,因为我正在检查一个多请求问题,但它可能与我的源相关有趣的是,我将对此进行研究。同时阅读以下内容:是的,我无法发布答案,因为我正在检查一个多请求上一个问题,但它可能与我的sourceLol有关,我猜你不是在问“总体稳健”Lol,我猜你不是在问“总体稳健”