使用 JavaNIO 发送/接收对象/引用或对象的一部分

发布于 2024-10-24 17:37:28 字数 495 浏览 6 评论 0原文

我用 NIO 设计了一个实时物理模拟, 这是关于在多个客户端上同时(实时)移动球

目前,我通过字符串解析发送一个球的坐标, 但现在我想移动多个球,并且想要除了发送字符串之外还有一些通用机制,所以我在服务器上创建了 4 个球。

如何实现,每个球的坐标对应于客户端上的同一个球(我也在客户端上创建),并且每个球可以根据从服务器接收到的坐标移动。

这应该通过发送每个球的引用来实现,但我不知道如何使用 java 字节缓冲区和 NIO 来实现这一点。

我在实现方面遇到问题,如果有人帮助我提供代码示例,那就太好了,无论如何,它可以通用用于发送模拟数据,例如除字符串之外的对象坐标是可以接受的(如果有人做了类似的工作),但它应该实现我所描述的用例。

谢谢,

Jibbylala

P.S:我可以知道否决问题的原因吗?

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 be 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

<逆流佳人身旁 2024-10-31 17:37:28

首先要做的事情是:

您需要一个对象标识来表示移动/任何东西。该对象具有与 java 中任何其他对象一样的属性,在您的情况下,球具有坐标。球可能有形状(除了是一个带有常数 R 的完美球体之外),您希望首先发送对象的描述或类型,然后发送其某些属性的更改。
对于对象标识,您可以使用单个 long 甚至 UUID,但由 2 个 long(16ytes)表示

您甚至可以使用标准 java 序列化,但我建议不要使用,除非您精通此事(但您不会问那么问题)。

虽然制作二进制协议来替换您自己的基于字符串的表示并不困难,但对于初学者来说这也不是一件容易的事。

NIO 本身具有 ByteBuffer 功能,但这不会对您表示数据的外部/流模型产生任何影响。如果您能够将“球”以某种有意义的方式表示为 byte[],则可以使用 ByteArray 实现相同的目的。

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.

爱格式化 2024-10-31 17:37:28

将 RMI 与 KryoNet 结合使用,然后您只需使用远程方法调用,并且调用的效率足以让您不再需要担心他们。该网站上有 RMI 示例供您入门。如果您需要与模拟球进行实时客户端交互,它会变得有点复杂,您可能需要使用 UDP,但否则您可以只发送位置。您应该尝试将信息合并到您的世界对象中的一个方法中。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文