Javascript 向客户端浏览器推送通知
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4899523/
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
Push notification to the client browser
提问by Harsha M V
I'd like to create an application where when a Super user clicks a link the users should get a notification or rather a content like a pdf for them to access on the screen.
我想创建一个应用程序,当超级用户单击链接时,用户应该收到通知,或者更确切地说是 pdf 之类的内容,以便他们在屏幕上访问。
Use Case: When a teacher wants to share a PDF with his students he should be able to notify his students about the pdf available for download and a link has to be provided to do the same.
用例:当教师想要与他的学生共享 PDF 时,他应该能够通知他的学生可以下载的 pdf,并且必须提供一个链接来执行相同的操作。
采纳答案by Leo
As you want to implement this in CakePHP (so I assume it's a web-based application), the user will have to have an 'active' page open in order to receive the push messages.
由于您想在 CakePHP 中实现它(所以我假设它是一个基于 Web 的应用程序),用户必须打开一个“活动”页面才能接收推送消息。
It's worth looking at the first two answers to this, but also just think about how other sites might achieve this. Sites like Facebook, BBC, Stackoverflow all use techniques to keep pages up to date.
值得一看前两个答案,但也要考虑其他网站如何实现这一点。Facebook、BBC、Stackoverflow 等网站都使用技术来保持页面最新。
I suspect Facebook just uses some AJAX that runs in a loop/timer to periodically pull updates in a way that would make it look like push. If the update request is often enough (short time period), it'll almost look realtime. If it's a long time period it'll look like a pull. Finding the right balance between up-to-dateness and browser/processor/network thrashing is the key.
我怀疑 Facebook 只是使用了一些在循环/计时器中运行的 AJAX,以使其看起来像推送的方式定期拉取更新。如果更新请求足够频繁(短时间段),它看起来几乎是实时的。如果时间很长,它看起来像拉。在最新和浏览器/处理器/网络颠簸之间找到适当的平衡是关键。
The actual request shouldn't thrash the system, but the reply in some applications may be much bigger. In your case, the data in each direction is tiny, so you could make the request loop quite short.
实际请求不应该影响系统,但某些应用程序中的回复可能要大得多。在您的情况下,每个方向的数据都很小,因此您可以使请求循环非常短。
Experiment!
实验!
回答by Tyler Holien
There are several ways you can accomplish this. The most supported way is through a technique called Comet or long-polling. Basically, the client sends a request to the server and the server doesn't send a response until some event happens. This gives the illusion that the server is pushing to the client.
有几种方法可以实现这一点。最受支持的方法是通过一种称为 Comet 或长轮询的技术。基本上,客户端向服务器发送请求,服务器在某些事件发生之前不会发送响应。这给人一种服务器正在推送给客户端的错觉。
There are other methods and technologies that actually allow pushing to the client instead of just simulating it (i.e. Web Sockets), but many browsers don't support them.
还有其他方法和技术实际上允许推送到客户端,而不仅仅是模拟它(即 Web 套接字),但许多浏览器不支持它们。
回答by Martin
Have a look at php-amqpliband RabbitMQ. Together they can help you implement AMQP (Advanced Message Queuing Protocol). Essentially your web page can be made to update by pushing a message to it.
看看php-amqplib和RabbitMQ。它们一起可以帮助您实现 AMQP(高级消息队列协议)。本质上,您的网页可以通过向其推送消息来更新。
[EDIT]I recently came across Pusherwhich I have implemented for a project. It is a HTML5 WebSocket powered realtime messaging service. It works really well and has a free bottom tier plan. It's also extremely simple to implement.
[编辑]我最近遇到了我为一个项目实施的Pusher。它是由 HTML5 WebSocket 驱动的实时消息服务。它运作良好,并有一个免费的底层计划。实现起来也非常简单。
回答by Distdev
Standard HTTP protocol doesn't allow push from server to client. You can emulate this by using for example AJAX requests with small interval.
标准 HTTP 协议不允许从服务器推送到客户端。您可以通过使用例如具有小间隔的 AJAX 请求来模拟这一点。