Synchronize data between frontend and backend
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开发者_JAVA百科 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.
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.
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.
精彩评论