使用 PHP/JavaScript 从服务器向用户推送通知 [英] Push notifications from server to user with PHP/JavaScript
问题描述
简短:有没有办法将数据从服务器推送到登录的用户会话,而该用户不需要发出 HTTP 请求?
Long(er):我正在 Apache/PHP/MySQL 服务器上构建一个 HTML5 应用程序,它可以提供给移动设备上的用户(或者可能通过 PhoneGap 应用程序).
我想让用户知道应用程序何时有可用的新信息,而不会通过强制用户执行对服务器的调用或要求 AJAX 调用来增加过多的数据费用(是的,我知道它可以检查对于单字节 cookie,但我宁愿将其设为零).
一个很好的例子是在两个用户之间发送聊天消息,从一个用户向另一个用户提交.
在现代浏览器中,Websockets 或在旧版浏览器中,长轮询".还有一些库可以抽象其中的一些(例如 comet).
现在使用的一个非常常见的库是socket.io
,它自动连接可用的最佳技术,如果可用,则使用 webSockets,如果不可用,则退回到其他技术,例如长轮询.
其他一些参考:
Short: Is there a way to push data from the server to a logged in user session without that user needing to make an HTTP request?
Long(er): I'm building an HTML5 application, on an Apache/PHP/MySQL server, that could be served to users on mobile devices (or perhaps via a PhoneGap application).
I would like to let the user know when there is new information available from the application, without racking up excessive data charges by forcing that user to execute a call to the server, or requiring AJAX calls (yes I know it could check for a single byte cookie, but I'd rather make it zero).
A good example of this would be sending chat messages between two users on submit from one user to the other.
In modern browsers, Websockets or in older browsers, "long polling". There are also libraries that can abstract some of this (like comet).
A very common library in use now is socket.io
which automatically connects with the best technology available, using webSockets if available and falling back to other technologies like long polling if not available.
Some other references:
How do I implement basic "Long Polling"?
COMET (server push to client) on iPhone
Is there a difference between long-polling and using Comet
Pushing the web with websockets
Oh HTML5, Push Data to my Mobile
这篇关于使用 PHP/JavaScript 从服务器向用户推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!