彗星的例子。我不明白

发布于 2024-11-04 18:24:17 字数 543 浏览 3 评论 0原文

protected void Page_Load(object sender, EventArgs e)
{
    Response.Buffer = false;

    while (true)
    {
        Response.Write(Delimiter + DateTime.Now.ToString("HH:mm:ss.FFF"));
        Response.Flush();

        // Suspend the thread for 1/2 a second
        System.Threading.Thread.Sleep(500);
    }

    // Yes I know we'll never get here, it's just hard not to include it!
    Response.End();
}

当执行Response.Flush()时,新的网页被发送到客户端 while 块将在服务器上永远运行 当新的maeeage到达客户端时,会刷新新数据 怎么可能继续同一个地方。不应该创建一个新的 Page 对象吗?

protected void Page_Load(object sender, EventArgs e)
{
    Response.Buffer = false;

    while (true)
    {
        Response.Write(Delimiter + DateTime.Now.ToString("HH:mm:ss.FFF"));
        Response.Flush();

        // Suspend the thread for 1/2 a second
        System.Threading.Thread.Sleep(500);
    }

    // Yes I know we'll never get here, it's just hard not to include it!
    Response.End();
}

When Response.Flush() is executed, the new webpage is sent to the client
The while block will run forever on the server
When the new maeeage reaches the client, there is a refresh for the new data
How is it posible to continue the same place. Shouldn't there be a new Page object created?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无敌元气妹 2024-11-11 18:24:17

您所描述的内容存在一些问题...

1)您描述了不匹配的客户端和服务器代码。那里的服务器代码仅适用于永远挂起的单个 AJAX 请求,并在客户端上收到通知,但不启动新请求。但是,在您的评论中,您描述了一个确实发起新请求的客户端。这两个概念不能一起工作。

2)详细阐述第1点,请求永无止境意味着大多数浏览器永远不会看到它。 “典型”AJAX 请求在整个内容到达之前不会更改状态,因此该请求将永远挂起。您必须结束请求,或者使用 XHR 请求,该请求会在每个块从服务器刷新时更改状态,这仅在某些浏览器中可行。

3) 即使 1) 有效,代理等最终也会终止请求。他们不喜欢永远搁置的请求。

4)这是一个阻塞同步请求,它会疯狂地消耗服务器资源。在灾难性失败之前,这不会超出线程池限制。

所以是的,你在这里给出的例子没有意义:)。

There are a few problems with what you've described...

1) You've described mis-matching client and server code. The server code there only works with a single AJAX request that hangs around forever, and gets notified on the client but doesn't start a new request. In your comments, however, you've described a client that does start a new request. Those 2 concepts won't work together.

2) Elaborating on point 1, the request never ending means that most browsers will never see it. "Typical" AJAX requests won't change state until the entire contents arrive, so that request will just hang forever. You'll have to end the request, or use an XHR request that changes state as each chunk is flushed from the server, which only would be viable in certain browsers.

3) Even if 1) worked, proxies, etc, would kill the request eventually. They don't like requests sitting around forever.

4) That's a blocking synchronous request, which'll eat server resources like crazy. That won't get beyond a the threadpool limit before it fails catastrophically.

So yea, the example you've given here doesn't make sense :).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文