自动更新页面的代码大纲
我想弄乱实时信息,并且有一个非常标准的功能我想复制:
当您在单个问题视图中输入答案时,它会出现在此处,并在顶部弹出一个警报,显示“有 3 条新闻答案,点击显示”
它也出现在 Twitter 上 “此搜索中有 5 条新推文:点击更新”
我非常精通服务器和客户端代码,我正在寻找的是此类事情如何发生的基本概要(甚至不是伪代码,而是简单的英语)。
服务器上是否有一个 CRON 作业每分钟运行一次,向页面上的长轮询 AJAX 位发送信号?
页面本身是否轮询服务器?
欢迎任何和所有解决方案。谢谢!
I want to mess around with realtime information, and there is a pretty standard functionality that I want to duplicate:
It occurs here on SO when you're on a single-question view, typing your answer, and an alert pops up top saying "there are 3 news answers, click to show"
It also occurs on Twitter "There are 5 new tweets in this search: click to update"
I'm pretty versed in server and client side code, and what I'm looking for is the basic outline (not even psuedo code, but perhaps plain english) of how something like this happens.
Is there a CRON job on the server running every minute, that shoots a signal to a long-polled AJAX bit on the page?
Does the page itself poll a server?
Any and all solutions are welcome. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以通过使用 Javascript
setTimeout
方法在客户端定期运行的 AJAX 调用来实现这一点。您将有一个 Javascript 函数,该函数调用服务器端方法来检查是否发生更新、显示任何更新,然后调用 setTimeout 来调用自身。伪代码:
You can implement that using an AJAX call that runs on the client side at a regular interval using the Javascript
setTimeout
method. You'll have a Javascript function that calls your server side method that checks if an update has occurred, displays any update, then calls setTimeout to call itself.pseudo code:
从我的头脑中,我会通过 javascript 来实现它 - 设置超时来询问服务器。但这只是一个有根据的猜测。
From the top of my head, I'd make it via javascript - setting timeouts to question the server. That's only an educated guess though.
看起来SO使用定期更新程序向如下网址发出ajax请求:
https://stackoverflow.com/posts /2307584/answer-activity-heartbeat
这将返回一个 JSON 结果:
以下是存在新答案时的结果示例:
Looks like SO uses a periodical updater to make an ajax request to a url like:
https://stackoverflow.com/posts/2307584/answer-activity-heartbeat
This returns a JSON result:
Heres an example of the result when a new answer exists: