与 php 保持实时连接?

发布于 2024-07-18 05:20:26 字数 332 浏览 9 评论 0原文

我正在开发一个项目,让我不断地 ping php 脚本以获取新数据,所以如果我正确理解这一点,这意味着被 ping 的 php 脚本会无限期地运行。 它可以工作,但我猜它对服务器造成巨大压力,并且可能被认为是丑陋和糟糕的做法。 我的说法正确吗?

有什么方法可以保持与脚本的连接,并利用 php 的内置输出缓冲来刷新我需要的内容,但使用某种循环使脚本无限运行,以便当新数据可用时,它可以输出。 这也是一个坏主意吗?

我只是在寻找有更多经验的输入表单开发人员。

最后一件事...

是否有其他方法可以保持数据的持续流动(不包括闪存或 silverlight 等技术)?

I am working on a project has me constantly pinging a php script for new data, so if I understand this correctly that means that the php script being pinged gets run over and over indefinitely. It works but i'm guessing its a huge strain on the server, and is probably considered ugly and bad practice. Am I right about that?

Is there any way I could keep the connection to the script alive and make use of php's built in output buffering to flush the contents I need, but keep the script running for infinity using some sort of loop so when new data is available it can be output. Is this a bad idea as well?

I'm just looking for input form developers out there with more experience.

One last thing...

Are there any other ways to keep a constant flow of data going (excluding technologies such as flash or silverlight)?

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

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

发布评论

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

评论(5

ペ泪落弦音 2024-07-25 05:20:27

如果您当前所拥有的功能在针对您可能在此应用程序中预期的负载类型进行测试时并且继续有效,那么这并不是真正被认为是不好的做法。 如果可行的话,保持简单并不是犯罪。 任何做你所描述的事情都会违背网络的原始模型,所以你正在冒险进入不稳定的领域。

我强烈建议您查看 Comet 技术。 它最流行的用途与您想要的相反 - 服务器不断地将信息推送到页面 - 但它显然可以双向工作。 尽管您的里程可能有所不同,但我听说过一些好消息。 正如维基百科所描述的:

在 Web 开发中,Comet 是一个新词,用于描述 Web 应用程序模型,其中长期持有的 HTTP 请求允许 Web 服务器将数据推送到浏览器,而无需浏览器明确请求。 Comet 是实现这种交互的多种技术的总称。 所有这些方法都依赖于浏览器默认包含的功能,例如 Javascript,而不是非默认插件。

If what you have currently works and continues to work when tested against the kind of load you might expect in this application, it is not really considered bad practice. It is not a crime to keep it simple if it works. Anything that does what you are describing is going to go against the grain of the original model of the web, so you're venturing into shaky territory.

I do recommend you check out the Comet technique. It is mostly popular for the inverse of what you want - the server pushing information to a page continuously - but it can obviously work both ways. Although your mileage may vary, I've heard good things. As Wikipedia describes it:

In web development, Comet is a neologism to describe a web application model in which a long-held HTTP request allows a web server to push data to a browser, without the browser explicitly requesting it. Comet is an umbrella term for multiple techniques for achieving this interaction. All these methods rely on features included by default in browsers, such as Javascript, rather than on non-default plugins.

赢得她心 2024-07-25 05:20:27

看起来 php 并不是最好的语言选择。 可能会考虑像 scala 或 erlang 这样的东西,它们被设置为更好地处理这种类型的长期消息传递。

It almost seems like php wouldn't be the best choice of language for this. Possibly consider something like scala or erlang which are setup to handle this type of long lived messaging better.

未央 2024-07-25 05:20:27

你必须学习如何在 php 中使用套接字。

从这里开始:http://php.net/manual/en/book.sockets。 php

这里有关于编写独立 php 应用程序的有用手册:高级 PHP 编程

You have to learn how to use sockets in php.

Start from here: http://php.net/manual/en/book.sockets.php

And afair here is useful manual about writing standalone php apps: Advanced PHP Programming

眼泪也成诗 2024-07-25 05:20:27

我想说这要看情况。 如果您希望由客户端启动数据传输,那么您最好的选择是一些 ajax(例如 getxmlhttpobject,或者如果您想作弊,则只是 iframe :P)。 如果您希望由服务器启动传输,那么 php 可能不是您想要使用的语言。

I'd say that depends. If you want the data transfers to be started by the client, your best choice here would be some ajax (like getxmlhttpobject or just iframes if you feel like cheating :P). If you want the transfers to be started by the server, then, perhaps php is not the language you want to use.

冷清清 2024-07-25 05:20:27

您可以使用 ajax 来进行 http 流。 看一下 ajaxpatterns

You can use ajax to have http-streaming. Take a look at ajaxpatterns.

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