"This article goes from Network http://janeky.iteye.com/blog/1614175"
This period of time in the handling of the service-side character mobile broadcast encountered a problem, recorded.
1. Questions
Now the page tour is aligned to the direction of the client, large map, thousands of people with the screen. Therefore, it also brings a lot of challenges to the development of the service side of page tour. Suppose a scene map is 8000*8000 size, and there are 1000 people in it. Within 1 seconds, each player moves once. The most primitive approach is to broadcast messages to users in the same scenario. A simple calculation of the broadcast volume: 1000*1000=1000000 broadcast volume, a bit scary.
2. The programme
The goal of optimization must be to reduce the amount of broadcast. We see, the scene is particularly large, this is not a good thing for the art colleague, for the service side, is not a bad thing. Assuming the optimal state, the user can be all over the corner. So, we just want to broadcast the message to the user who can see the mobile target. Assuming a screen size of 1600*1000, a screen theoretically distributes 8000*8000/(1600*1000) = 25 people. or every second each person moves, the total broadcast message volume is: 25*25*40 = 25000. Haha, a whole less than 40 times times the amount of broadcast, service pressure less, for the boss saved a lot of money, go back to apply for a bonus.
3. Implement
According to the above ideas, the implementation is very simple. Previously for each user of the scene broadcast, now only add one step filtering process, according to the coordinates to select the range to see the moving target players. method is relatively simple, here does not provide detailed code implementation, there is not clear, you can leave a message to discuss.
4. Optimization
to do the above, basically has met the standard. However, as a technology, the pursuit of perfection is endless. And we're wondering, can we optimize it further? Good optimization is always based on a careful analysis of the problem. We believe that analyzing the contents of the broadcast, adding a mobile, then the b,c on the same screen will be subject to a broadcast, the content is roughly (a,from,to). Similarly, B moves, and a,c is also subject to such a type of broadcast. C received two broadcasts in the same time period. Can these two broadcasts be merged and become (A,from,to;b,from,to). This is the inspiration for optimization. For the low-real-time Turn page tour (not even the battle Arpg), the player sees the other player's movement, does not require certain real-time. To do this, we can consider merging
News.
implementation is very simple and can be achieved using a model similar to that of the producer-consumer. The displacement messages from each front end are put into the queue. The backend has a separate thread that, at intervals, takes out data, merges messages, and broadcasts them to the relevant user. After the A/b displacement, C receives only one message.
5. Summary