前端与后端数据同步
我们正在开发一款基于浏览器的社交游戏,它是一款 MMORPG 游戏。我们使用 html/javascript/css 作为前端技术(与 flash 相比)。当用户第一次加载我们的游戏时,将下载一堆文件(外部js/css)并请求多个ajax调用。除非用户手动刷新页面,否则整个页面不会再次加载。我们创建一个 js 对象来存储用户特定的数据,如现金、等级等。并且在用户以后的操作过程中可能会发生变化。所以我们需要从后端同步数据。一个特殊的问题是用户的数据也可以被动更新(由其他用户),这可能发生在其他用户的争斗中。所以我的问题是如何以及何时同步?
到目前为止,我们经历了两个阶段,1)每当用户单击选项卡(在 html 中显示/隐藏 div)或可能的操作(例如战斗/买东西)时,我们每次都会调用 ajax 请求从数据库获取新数据。 2)与1)类似,但我们只是添加一个代理来指示用户数据是否更改,以减少无用的数据库查询。 ajax 调用首先会转到代理。
对于 1) 我们可能有 70%+ 无用的 ajax 调用和数据库查询
对于 2) 我们可能有 70%+ 无用的 ajax 调用
现在我们使用 2) 的方法。但我不认为这是最好的解决方案,因为我们仍然有 70% 以上无用的 ajax 调用。那么对于这个用例有没有更好的解决方案?
解释
无用的ajax调用:由于2次ajax调用之间的间隔,用户数据可能根本不会改变。
We're developing a browser based social game, and it is a MMORPG game. We are using html/javascript/css as the frontend tech(comparing with flash). when the user loads our game for the first time, a bunch of files will be downloaded(external js/css) and several ajax call will be requested. The whole page won't be loaded again unless user refresh the page manually. And we create a js object to store the user-specific data, like cash, level and more. And it's subject to change during the user's coming operations. so we need to synchronize the data from the backend. A special issue is user's data can also be updated passively(by other users), this may happen in a fight by other users. so my question is how and when to synchronize?
Till now we underwent two stages, 1)whenever user clicks tabs(to show/hide a div in html) or possible operations(like fight/buy some thing), then we call the ajax request to get the fresh data from database each time. 2) similar with 1) but we just add a proxy to indicate whether the user's data is changed or not to reduce the useless database query. The ajax call will go to proxy at first.
for 1) we may have 70%+ useless ajax calls and db queries
for 2) we may have 70%+ useless ajax calls
Now we use 2)'s approach. But I don't think it's the best solution, since we still have 70%+ useless ajax calls. so is there any better solution to this use case?
explanations
useless ajax call: since the interval between 2 ajax calls, the user data may not change at all.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好问题,我认为最好的解决方案是实施服务器推送。这个想法很简单,因为客户端和服务器之间有连接,服务器知道数据何时被修改,因此他可以通知客户端(在您的情况下为 html/javascript/css)任何更改。这应该消除无用的轮询(您的 ajax 调用)。
现在,您可能想检查一下,或者只是在 google 中搜索其他 Ajax Push 解决方案:
http://www.icefaces.org/main/ajax-java/ajaxpush。 iface
这也取决于您的应用服务器所使用的技术,在最坏的情况下,您将不得不实现自己的 Ajax Push。
Good question, I think there best solution will be to implement server push. The idea is simple, as you have the connection between the client and server, the server knows when the data is modified, therefore he can inform the client (in your case the html/javascript/css) about any changes. This should eliminate useless polling (your ajax calls).
Now, you may want to check this out, or just search google for other Ajax Push solutions:
http://www.icefaces.org/main/ajax-java/ajaxpush.iface
That will also depend on the technology you use for the application sever, in the worst case, you would have to implement your own Ajax Push.
我同意 Kel 的观点,Ajax-Push AKA Comet 是最好的解决方案。
从经验中我已经知道 Persevere 是一个很好的解决方案,它与Dojo 工具包。
如果性能至关重要,我还建议您应该尝试 Wt,因为它允许使用 C++ 编写网站。
根据您的需求,它们都是不错的选择。
I agree with Kel here Ajax-Push AKA Comet is the best solution.
I already know from experience that Persevere is a good solution and it works the best with the Dojo toolkit.
I can also suggest that you should try Wt if performance is critical as it allows to code websites in C++.
Both of them are great choices depeanding on your needs.