如何实现服务器一次推送更多值?

发布于 2024-07-24 06:47:22 字数 605 浏览 2 评论 0原文

实现多件事的服务器推送的最佳方法是什么?

假设我只想更新用户状态,这样我就可以定期轮询服务器的状态(大约 1000 毫秒)并更新页面。

我发现的另一种方法是服务器等待大约 30 秒,同时检查是否有任何更改,如果找到更改,服务器将响应推送回客户端,客户端更新页面,然后进行另一次轮询。

但是我该如何实现这个来检查网站上的 10 件事呢? 例如,如果我希望 stackoverflow 在有人投票时刷新问题投票,但我能想到的唯一方法是

向服务器询问每个问题的投票 -> 服务器回复页面上每个问题的投票

但我如何找出哪些问题投票发生了变化? 我可以发送所有当前投票,然后让服务器比较这些值并仅回复那些确实发生更改的投票,但我认为在检查大约 30 个值时这样做是非常无效的。

一个例子就是Facebook,几乎所有内容都是通过服务器推送来刷新的,但是服务器如何找出哪些内容发生了变化,哪些内容没有变化呢?

我发现的所有内容(包括我的书“Ajax Patterns”)仅解释了如何轮询一个值,但我没有找到任何如何一次轮询多个值(例如超过 10 个值)的内容。

What is the best way to implement server push for more than one thing.

Lets say I want to just update user status, so I can periodicaly poll the server for status in like 1000ms and update the page.

The other way I can found is that server waits for like 30 sec, while it checks if there was any change and if one is found, server push response back to client, which update page and then make another poll.

But how would I implement this to check for like 10 things on website? For example if I want stackoverflow to refresh question votes when someone vote, but the only way how to do this that I can think of is

ask server for votes for each question -> server replies with votes of every question on the page

but how can I find out which question votes did change? I could send all current votes and then let server compare the values and reply only with those that did change, but I think that it would be very uneffective to do this while checking for like 30 values.

One example for all would be Facebook, where almost everything is refreshed by server push, but how can server find out what did change and what didnt?

Everything that I found (including my book "Ajax Patterns") explains only how to poll for one value, but I didn't find anything how to poll for many values at a time (like more than 10).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

东北女汉子 2024-07-31 06:47:22

如果您准备使用会话,最简单的方法是拥有一个串行请求 ID,并让服务器跟踪会话上发送到客户端的最新信息。 然后客户端询问是否有比上次刷新时更新的数据。

例如:假设客户端在没有任何更改后发送 ajax 请求 #5001,服务器可能会回复“false”。 然后有人发布消息,或者发生某种更改,因此在请求 #5002 中,服务器发送更改元素的列表(无论这些元素是什么)。 然后在请求 #5003 中它会再次回复 false,因为自请求 #5002 以来没有任何变化。

JSON 客户端/服务器架构非常适合此目的。 它允许轻松序列化对象层次结构/映射。 我更喜欢javascript中的客户端jQuery,而服务器端则微不足道。

If you are prepared to use sessions, the easiest way would be to have a serial request ID, and let the server keep track of the latest information sent to the client on the session. Then the client asks if there is newer data than the last time it refreshed it.

e.g: Lets say the client sends ajax request #5001 after nothing has changed, the server might reply 'false'. Then someone posts a message, or there is a change of some kind, and so in request #5002 the server sends a list of changed elements (whatever these are). Then in request #5003 it would reply false again, because nothing has changed since request #5002.

A JSON client/server architecture would be perfect for this. It allows easy serialisation of object hierarchies/maps. I prefer jQuery for the client in javascript, and the server side is trivial to spit out.

無心 2024-07-31 06:47:22

我建议在服务器端用修订号标记您的数据,以便客户端知道其数据的修订版; 创建一个复合查询,客户端可以通过该查询发送一组修订,而服务器可以使用其可能具有的任何更新修订来响应该修订列表。 这样,客户端只向一个服务器查询是否有更新; 您只需将客户感兴趣的所有数据一起批处理到一个查询中。 该方法还具有允许客户端更改其感兴趣的数据集的优点; 如果您的服务器端实现足够灵活,您可以使用相同的实现来满足所有动态数据需求。

I'd suggest to tag your data with revision numbers at the server side, so the client knows what revision of the data it has; create a composite query whereby the client can send a set of revisions, and the server can respond to that list of revisions with any updated revisions it may have for them. In that way, the client is only making one server query to see if there are any updates; you're just batching together all the data the client is interested in into one query. This method also has the strength of allowing the client to change the set of data that it's interested in; if your implementation server-side is flexible enough, you can use the same implementation for all your dynamic data needs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文