使用 php 和 jquery 的简单彗星示例

发布于 2024-08-02 21:30:41 字数 175 浏览 3 评论 0原文

谁能给我一个使用 PHP 的彗星技术的简单好例子?

我只需要一个使用持久 HTTP 连接或类似连接的示例。我不想使用轮询技术,因为我已经设置了类似的东西,并且不仅难以使用和管理它的大量资源。另外我使用的是 IIS7 而不是 Apache。

一个好的例子真的很有帮助,这样我就可以摆脱这种丑陋的轮询技术了。

Can anyone give me a good and simple example of the comet technique using PHP?

I just need an example that uses a persistent HTTP connection or something similar. I don't want to use a polling technique, because I have something like that set up and not only is it difficult to work with and manage its a big hog of resources. Also I am using IIS7 not Apache.

A good example would be really helpful so I can move on from this ugly polling technique.

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

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

发布评论

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

评论(7

离笑几人歌 2024-08-09 21:30:41

你应该使用轮询,或者使用专门为长请求而设计的Web服务器和COMET,具有良好的JS后端:

function listen() {
    $.get("/mylongrequestfile", {}, function(data) {
        $("#mydiv").html(data);
        listen(); // then launch again
    }));
};

记住COMET是“等待数据,如果有数据返回并退出”,因此JS后端必须解析数据并重新启动询问服务器的过程。

在这个例子中,如果服务器端出现问题或者只是与用户端断开连接,整个过程就会中断(只有请求成功时才会调用该函数)

You should use polling, or use a web server which is specially conceived for long requests and COMET, with a good JS backend:

function listen() {
    $.get("/mylongrequestfile", {}, function(data) {
        $("#mydiv").html(data);
        listen(); // then launch again
    }));
};

Remember that COMET is "wait for data, if there's data return and exit", so JS backend will have to parse the data and re-launch the process of asking the server.

In this example, if there is a server side problem or just a disconnection from the user side, the entire process will be broken (the function is only called if the request is successful)

悲喜皆因你 2024-08-09 21:30:41

请查看:如何使用 PHP 实现 COMET
这不是使用 JQuery。它是使用 PHP 和 Prototype 制作的。这很容易理解。我认为看完这篇文章你可以轻松制作 JQuery 脚本。

Check this out: How to implement COMET with PHP.
This is not using JQuery. It is made using PHP and Prototype. It is very easy to understand. I think you can made JQuery script easily after viewing this.

大姐,你呐 2024-08-09 21:30:41

我这里有一个非常简单的例子,可以帮助您开始使用 comet。它涵盖了使用 NHPM 模块编译 Nginx,并包含 jQuery、PHP 和 Bash 中简单发布者/订阅者角色的代码。

http://blog.jamieisaacs.com/2010 /08/27/comet-with-nginx-and-jquery/

可以在这里找到一个工作示例(简单的聊天):
http://cheetah.jamieisaacs.com/

I have a very simple example here that can get you started with comet. It covers compiling Nginx with the NHPM module and includes code for simple publisher/subscriber roles in jQuery, PHP, and Bash.

http://blog.jamieisaacs.com/2010/08/27/comet-with-nginx-and-jquery/

A working example (simple chat) can be found here:
http://cheetah.jamieisaacs.com/

回忆躺在深渊里 2024-08-09 21:30:41

从未使用过此技术并研究过关于“长轮询”主题的维基百科文章 “似乎是唯一可行的解​​决方案。通过在服务器上无限循环和休眠脚本来实现听起来很简单。 实际代码HTTP Streaming 页面 链接到维基百科文章。

您是否尝试过其中任何一个并偶然发现了特定问题?

Never having used this technique and studying the Wikipedia article on the topic, "Long Polling" seems like the only viable solution. It sounds pretty simple to implement by infinitely looping and sleeping a script on the server. There's some actual code in the HTTP Streaming page linked to from the Wikipedia article.

Have you tried any of this and stumbled on specific problems?

请你别敷衍 2024-08-09 21:30:41

查看此演示视频以实现长轮询 ( comet )..
它可能对大家有帮助

http://www.screenr.com/SNH

Check out this demo video for implementing Long Polling ( comet )..
It might help you all

http://www.screenr.com/SNH

策马西风 2024-08-09 21:30:41

你可以看看这篇文章,这是理解彗星编程的一个很好的开始概念。

您会发现两个例子。第一个使用 iframe 技术,而第二个使用持久连接。

You can take a look at this article, it's a really good start to understand comet programming concepts.

You will find two examples on it. The first one use the iframe technique whereas the second one use a persistent connection.

雨后咖啡店 2024-08-09 21:30:41

对于 IIS,有 WebSync。不过,由于您使用的是 PHP,因此最好使用 WebSync On-Demand。任何一种都可以为您提供所需的服务器推送,并且使用起来很简单。查看这个问题n 也是如此,这基本上就是您所追求的。

以下是实际使用 WebSync On-Demand 的简单示例,使用no 脚本语言。只需在两个窗口中打开,即可查看发布/订阅的实际情况。

要从服务器发布,您可以使用 PHP api

For IIS, there's WebSync. Since you're using PHP, however, you might be better off with WebSync On-Demand. Either one will give you the server-push you're looking for, and is simple to use. Check out this question as well, which is basically what you're after.

Here's a simple example of WebSync On-Demand in action using no scripting language. Simply open in two windows, and see the publish/subscribe in action.

To publish from the server, you can use the PHP api.

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