将更改推送到网页而不刷新 [英] Push changes to a webpage without refreshing

查看:151
本文介绍了将更改推送到网页而不刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我们可以通过轮询定期更改来实现这一目标。这可以通过AJAX实现(例如使用 jQuery.load()以及 setInterval())。

I know that we can do this by polling for changes in regular intervals. And this can be achieved by AJAX (for example using jQuery.load() along with setInterval() ).

但我想知道还有其他方法吗?其他成本较低的方法或更有效的方法?一个很棒的聊天客户端遵循什么逻辑?一旦你开始打字,另一端就会知道你正在打字。

But I want to know that are there other methods to do this? Other less costly methods or more efficient methods? What logic an awesome chat client follows? As as soon as you start typing, the other end gets to know that you are typing.

我们在stackoverflow上遵循什么机制来更新upvote计数或显示编辑已经等等而没有刷新页面?

What mechanism do we follow here on stackoverflow to update the upvote count or to show that an edit has been made etc. etc. without the page being refreshed?

推荐答案

当要保持客户端和服务器在(接近)实时同步时,有三件事情会立即发生记住:

When it comes to keeping the client and server in-sync in (near) real-time, there are 3 things that immediately come to mind:


  • 长轮询:你已经提到了这个,你设置了一个定时器每隔10秒左右触发一次新AJAX请求的客户端。这可能是3中最低技术的,也是效率最低的;但它也是最兼容的(意味着它可以在所有浏览器中使用,甚至像IE6 / 7这样的东西)

  • long polling: you already mentioned this one, where you have a timer set on the client which triggers a new AJAX request every 10 seconds or so. This is probably the most "low tech" of the 3 as well as the least efficient; BUT it is also the most compatible (meaning it will work in all browsers, even things like IE6/7)

WebSockets : sdespont在评论中已经提到了这个。虽然WebSockets比长轮询更有效(因为它只是让双向客户端 - 服务器通信无限期地打开),但如果你所要做的就是从服务器获得定期更新,那么它可能是一个非常严厉的解决方案。 Firefox和Chrome的所有版本都支持它,和IE在IE10中添加了支持

WebSockets: sdespont already mentioned this one in the comments. While WebSockets a more efficient than long-polling (since it just keeps the two way client-server communication open indefinitely), it can be a very heavy-handed solution if all you're trying to do is get regular updates from the server. All versions from Firefox and Chrome support it, and IE added support in IE10

服务器发送的事件:这个似乎不太受欢迎(或者只是不太知名)。它允许服务器向客户端发送更改(与客户端请求来自服务器的更改相反,如长轮询的情况)。这也只是一种单向通信(服务器 - >客户端),并且在请求完成后连接关闭(与双向通信保持打开的WebSockets相反)。并非所有浏览器都支持它,并且根本没有IE支持

Server-sent events: this one seems to be less popular (or just not as well known). It allows the server to send changes to the client (as oppose to the client requesting changes from the server, as is the case with long-polling). This is also just a one-way communication (server --> client) and the connection gets closed after the request is complete (as oppose to WebSockets where 2-way communication stays open). Once again, not all browsers support it, and there is no IE support at all

这个也是一篇很好的文章,它解释了更现代的客户端 - 服务器通信方式之间的区别。
如果您想了解有关服务器发送事件的更多信息,请这是一个很好的写作

This is also a good article which explains the difference between the more modern ways of client-server communication. And if you want more info about Server-sent events, this is a good write up

这篇关于将更改推送到网页而不刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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