使用 php 和 jquery 的简单彗星示例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1320542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple comet example using php and jquery
提问by
Can anyone give me a good and simple example of the comet technique using PHP?
谁能给我一个使用 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.
我只需要一个使用持久 HTTP 连接或类似内容的示例。我不想使用轮询技术,因为我有类似的设置,不仅难以使用和管理它的大量资源。另外我使用的是 IIS7 而不是 Apache。
A good example would be really helpful so I can move on from this ugly polling technique.
一个很好的例子会很有帮助,所以我可以从这种丑陋的轮询技术中继续前进。
回答by Adrián Navarro
You should use polling, or use a web server which is specially conceived for long requests and COMET, with a good JS backend:
您应该使用轮询,或使用专门为长请求和 COMET 设计的 Web 服务器,并具有良好的 JS 后端:
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.
记住COMET是“等待数据,有数据返回退出”,所以JS后端必须解析数据并重新启动询问服务器的过程。
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)
在这个例子中,如果出现服务器端问题或者只是与用户端断开连接,整个过程都会中断(只有请求成功才会调用该函数)
回答by chanchal1987
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.
看看这个:如何用 PHP 实现 COMET。
这不是使用 JQuery。它是使用 PHP 和 Prototype 制作的。这很容易理解。我认为您可以在查看此内容后轻松制作 JQuery 脚本。
回答by Jamie
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.
我这里有一个非常简单的例子,可以让你开始使用 Comet。它涵盖了使用 NHPM 模块编译 Nginx,并包括用于 jQuery、PHP 和 Bash 中的简单发布者/订阅者角色的代码。
http://blog.jamieisaacs.com/2010/08/27/comet-with-nginx-and-jquery/
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/
可以在此处找到一个工作示例(简单聊天):http:
//cheetah.jamieisaacs.com/
回答by nitin
Check out this demo video for implementing Long Polling ( comet ).. It might help you all
查看此演示视频以实现长轮询(彗星)。它可能对大家有所帮助
回答by deceze
Never having used this technique and studying the Wikipedia articleon 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 codein the HTTP Streaming pagelinked to from the Wikipedia article.
从未使用过这种技术并研究过有关该主题的维基百科文章,“长轮询”似乎是唯一可行的解决方案。通过在服务器上无限循环和休眠脚本来实现听起来非常简单。从维基百科文章链接到的HTTP 流页面中有一些实际代码。
Have you tried any of this and stumbled on specific problems?
您是否尝试过这些并偶然发现了特定问题?
回答by dasilvj
回答by jvenema
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.
对于 IIS,有WebSync。但是,由于您使用的是 PHP,因此使用WebSync On-Demand可能会更好。任何一个都会为您提供您正在寻找的服务器推送,并且易于使用。也看看这个问题,这基本上就是你所追求的。
Here's a simple example of WebSync On-Demand in actionusing noscripting language. Simply open in two windows, and see the publish/subscribe in action.
下面是一个不使用脚本语言的 WebSync On-Demand 的简单示例。只需在两个窗口中打开,即可查看发布/订阅操作。
To publish from the server, you can use the PHP api.
要从服务器发布,您可以使用PHP api。

