質問

私は次のhttphandlerを持っています。ブラウザが投票する必要なく、ブラウザ(JQueryとGrowluiが実装されている場合)に更新をプッシュするために使用しています。私が達成したのは、ポーリングループをサーバーに移動することだけだと思います。

このクラスをより堅牢でスケーラブルにする方法を教えてもらえますか?

これがコードです。

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);
    }
役に立ちましたか?

解決

使用する場合は Thread.Sleep(), 、実装する必要があります System.Web.Ihttpasynchandler または、ハンドラーがスケーリングされません。

他のヒント

QueueUpdateはどのように呼び出されますか?私はあなたがそれから文字列を取り、それをあなたがユーザーに送り返すJavaScriptに直接それを置くことに気づきました。ユーザーがJavaScriptをエントリに挿入し、QueueUpDateに何らかの形で表示できる可能性はありますか?

また、JavaScript文字列に入れる前に、有効なメッセージRegexに対してメッセージを一致させます。誰かがあなたのGrowluiコールを完了してから、自分のJavaScriptをかなり簡単に挿入できるようです。少なくとも、挿入しているメッセージに、文字列を終了して新しいJavaScriptコマンドを開始できる単一の引用( ')が含まれていないことを確認できます。

たぶんそれはただパラノイアですが、それはより堅牢になります:)

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top