简体   繁体   中英

Java networking, beyond a simple chatroom

So I recently followed this tutorial on making a basic chatroom in Java. It uses multithreading and is a "connection-oriented" server. I was wondering how I could use the same Sockets and ServerSockets to send, say, the 3d position of an object instead of just a string?

Currently, the basic chatroom system just sends a string to the server and then the server sends it to all connected clients. What I want is to be able to have a client change the position of an object (most likely their character), and send the change of position to the server. Then (I would imagine) the server would send that change in position to each of the clients connected to it, and each client would in turn render this object at its new position.

I was wondering what the best way to do something like this was? Would it be to send a string and have the server parse it into a coordinate? Can I write more than one thing to a DataOutputStream at once?

I feel like I may have explained this poorly, so please ask some clarifying questions.

Thanks!

Create a Domain Object Model for your coordinate system. Then represent the changes to the positions using the objects in the above model. Serialize them into a transportable string like XML, JSON etc. Then unmarshall/deserialize the String to the original object and act upon them.

This separates your transport layer (using sockets to bradcast stuff) from the actual business logic (placement of objects) and the system becomes extendible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM