反向 AJAX? 数据更改可以“PUSHED”吗? 编写脚本?
我注意到我的一些大量使用 ajax 的网站(我访问的网站,而不是我构建的网站)具有某些自动刷新功能。 例如,在 GMail 中,如果我收到一条新消息,我会看到新消息而无需重新加载页面。 这与基于 Facebook 浏览器的 IM 客户端相同。 据我所知,没有任何 java 小程序处理服务器-浏览器绑定,所以我只能假设它是由 AJAX 完成的,也许还有一些我不知道的元素。 因此,根据我的最佳猜测,它是通过以下两种方式之一完成的:
JavaScript 对服务器端脚本进行稳定的“ping”,检查可能可用的任何更新(这可以解释为什么其中一些页面会带来任何其他重型页面都难以爬行)。 或
JavaScript 闲置,服务器端脚本实际上“推送”浏览器的任何更新。 但我不确定这是否可能。 我想象有某种 AJAX 功能仍然可以执行 ping 操作,但它只是询问“有更新吗?” 服务器脚本有一个简单的布尔值,表示“不”或“我很高兴你问了”。 但如果是这种情况,任何数据更改都需要直接调用脚本,以便它准备好数据更改并对布尔函数进行更改。
那么这可能/可行/它是如何运作的? 我想象这样的事情:
有人向服务器发送电子邮件/IM/DB 更新,服务器使用脚本的 URL 加上一些相关的 GET 变量调用脚本,脚本记录更改并更新“可用更新”变量,AJAX 获取当响应实际上有更新时,AJAX 运行其正常的“更新页面”功能,该功能执行正常的更新脚本并将其输出到浏览器。
我问这个问题是因为js只是进行不断的检查,这看起来效率很低,这需要a)服务器每1.5秒工作一次,b)我的浏览器每1.5秒工作一次,这样我就可以说“天啊,我有了一个即时通讯工具,就像一个真正的即时通讯客户端一样!”
I have noticed that some of my ajax-heavy sites (ones I visit, not ones I have built), have certain auto-refresh features. For example, in GMail, if I get a new message, I see the new message without a page reload. It's the same with the Facebook browser-based IM client. From what I can tell, there aren't any java applets handling the server-browser binding, so I'm left to assume it's being done by AJAX and perhaps some element I'm unaware of. So by my best guess, it's done in one of two ways:
The javascript does a steady "ping" to a server-side script, checking for any updates that might be available (which would explain why some of these pages bring any other heavy-duty pages to a crawl). or
The javascript sits idly by and a server-side script actually "Pushes" any updates to the browser. But I'm not sure if this is possible. I'd imagine there is some kind of AJAX function that still pings, but all it simply asks "any updates?" and the server-script has a simple boolean that says "nope" or "I'm glad you asked." But if this is the case, any data changes would need to call the script directly so that it has the data changes ready and makes the change to that boolean function.
So is that possible/feasible/how it works? I imagine something like:
Someone sends an email/IM/DB update to the server, the server calls the script using the script's URL plus some relevant GET variable, the script notes the change and updates the "updates available" variable, the AJAX gets the response that there are in fact updates, the AJAX runs its normal "update page" functions, which executes the normal update scripts and outputs them to the browser.
I ask because it seems really inefficient that the js is just doing a constant check which requires a) the server to do work every 1.5 seconds, and b) my browser to do work every 1.5 seconds just so that on my end I can say "Oh boy, I got an IM! just like a real IM client!"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
了解Comet
Read about Comet
实际上,我一直在开发一个小型 .NET Web 应用程序,该应用程序使用 Ajax 和所描述的长轮询技术。
根据您使用的技术,您可以使用线程信号机制来保留您的请求,直到检索到更新。
使用 ASP.NET,我在一台计算机上运行服务器,因此我存储对 Producer 对象(其中包含处理数据的线程)的引用。 为了启动数据拉取,我的服务的 Subscribe 方法被调用,该方法创建一个向 Producer 注册的 Consumer 对象。 如果Consumer是长轮询模式,它有一个AutoResetEvent,每当它收到新数据时就会发出信号,每当Web客户端发出数据请求时,Consumer首先等待重置事件,然后返回它。
但是您提到了有关 PHP 的一些内容 - 据我所知,持久性是通过序列化来维护的,而不是实际上将对象保留在内存中,所以我不知道如何使用 $_CACHE[] 或 $_SESSION[ 来引用 Producer 对象]。 当我使用 PHP 进行开发时,我对多线程一无所知,所以我没有尝试过它,但我想你可以研究一下。
使用无限循环将消耗大量的处理能力 - 我会首先用尽所有其他选项。
I've actually been working on a small .NET Web App that uses the Ajax with long polling technique described.
Depending on what technology you're using, you could use thread signaling mechanisms to hold your request until an update is retrieved.
With ASP.NET I'm running my server on a single machine, so I store a reference to my Producer object (which contains a thread that processes the data). To initiate the data pull, my service's Subscribe method is called, which creates a Consumer object that's registered with the Producer. If the Consumer is long polling mode, it has a AutoResetEvent which is signaled whenever it receives new data, and whenever the web client makes a request for data, the Consumer first waits on the reset event, and then returns it.
But you're mentioning something about PHP - as far as I know persistence is maintained through serialization, not actually keeping the object in memory, so I don't know how you could reference a Producer object using $_CACHE[] or $_SESSION[]. When I developed in PHP I never really knew anything about multithreading so I didn't play around with it, but I guess you can look into that.
Using infinite loops is going to consume a lot of your processing power - I would exhaust all other options first.