使用 PHP/JavaScript 从服务器向用户推送通知 [英] Push notifications from server to user with PHP/JavaScript

查看:23
本文介绍了使用 PHP/JavaScript 从服务器向用户推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简短:有没有办法将数据从服务器推送到登录的用户会话,而该用户不需要发出 HTTP 请求?

Long(er):我正在 Apache/PHP/MySQL 服务器上构建一个 HTML5 应用程序,它可以提供给移动设备上的用户(或者可能通过 PhoneGap 应用程序).

我想让用户知道应用程序何时有可用的新信息,而不会通过强制用户执行对服务器的调用或要求 AJAX 调用来增加过多的数据费用(是的,我知道它可以检查对于单字节 cookie,但我宁愿将其设为零).

一个很好的例子是在两个用户之间发送聊天消息,从一个用户向另一个用户提交.

解决方案

在现代浏览器中,Websockets 或在旧版浏览器中,长轮询".还有一些库可以抽象其中的一些(例如 comet).

现在使用的一个非常常见的库是socket.io,它自动连接可用的最佳技术,如果可用,则使用 webSockets,如果不可用,则退回到其他技术,例如长轮询.

其他一些参考:

如何实现基本的长轮询"?

iPhone 上的 COMET(服务器推送到客户端)

更多关于长轮询

长轮询和长轮询有区别吗?使用彗星

用 websockets 推动网络

哦 HTML5,将数据推送到我的手机

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

More on Long Polling

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆