Sending/Receiving object/references or part of Object with JavaNIO
I have designed a real time Physics simulation with NIO, which is about moving balls simultaneously (real time) on multiple clients
Currently, i m sending the coordinates of one ball with string parsing, but now i want to move multiple balls and want to have some generic mechanism other than sending string, so i create 4 balls on server.
How it can be realize that, coordinates of each ball correspond to same ball on client (which i created on client too), and each ball can be move according to its received coordinates from the sever.
This should be realize by sending the reference of each ball but i don't have idea how to realized that with java byte buffer and with NIO overall.
I have the problem with implementation, if somebody help me with code example , it would be great, anyway which can b开发者_运维技巧e generic for sending simulation data like object coordinates other than strings can be acceptable(if somebody did similar work) but it should implement the usecase as i described.
Thanks,
Jibbylala
P.S : May i know the reason of Downvoting question?
first things first:
you need an object identity to represent the moving/whatever stuff. The object has properties just like any other Object in java,in your case the ball has coordinates. Possibly the ball has shape (besides being a perfect sphere w/ a constant R), you'd like to send the description or type of the object 1st and then the change of some of its properties. For object identity you can use a single long or even an UUID but represented by 2 longs (16ytes)
You can even use standard java serialization but I'd advise against unless you are versed into the matter (but you'd not have asked the question then).
While making a binary protocol is not hard in order to replace your own string-based representation, it's also not an easy task for a first timer.
NIO, itself, features ByteBuffer, however that should not impose any effects on the external/stream model you represent your data. If you are able to represent "the balls" into some meaningful way into a byte[], you can achieve the same with ByteArray.
Use RMI with KryoNet, then you just use remote method calls and the calls are efficient enough that you no longer need to worry about them. There's RMI examples on the site for you to get started with. If you need realtime client interaction with the simulated balls it becomes a bit more complicated, and you might need to use UDP, but otherwise you can just send the positions. You should attempt to consolidate the information into maybe a method in your world object.
精彩评论