Getting data to the client in an Java MMO
I'm trying to make an online MMO in Java. It will be a simple one where you get a character and can move around and shoot other players, all in top-down 2D. The server will be multithreaded and in Java, and the client will also be in Java. The client will send all user input to the server and the server will handle all the logic (movement of the players, enemies, etc...) and send the locations back to the client. Anyways, what would be the best way to get all the information about the locations of all the enemies and players to the client? Would the best way to do it be to put all the data in a开发者_开发知识库n object and serialize that and write that to the socket, and unserialize it on the client side?
Generally you send updates out in 2 ways:
- You regularly broadcast fast-changing but less important state such as entity positions as often as needed (eg. several times a second)
- You send out notification of important events as they happen.
Serializing data on the server and sending it to the client is fine, but only send what you need and don't use larger data types than necessary. In the future you may need to consider third party serialization libraries which are perhaps more highly performing or have a more compact representation but you're some way off that point now. You will also have to consider what to do when the object you're sending changes format in some way; will that break old clients?
One of the server's main jobs is to ensure that you only send the messages to people who really need them; this is usually done by a distance check at a minimum. You also can check to see if something needs sending at all; if an entity hasn't moved since last time, no need to broadcast its position again.
Client to server is a different matter: if you blindly deserialize the objects you are sent then hackers can cause you a world of pain. Plan for security from the start. Have the client send you simple data that is easily checked and verified, and throw away anything suspect that you receive.
精彩评论