简体   繁体   English

套接字每秒发送多次对象

[英]Sockets to send many objects multiple times a second

So I have begun making a game that I would like to be multi player using sockets. 因此,我已经开始制作一款我想成为使用套接字的多人游戏。 It is a simple 2D game and all the objects I wish to send extend a class called Entity . 这是一个简单的2D游戏,我希望发送的所有对象都扩展了一个名为Entity的类。 I would like the positions of the objects to be sent at 30Hz with minimal delay or 'ping'. 我希望以最小的延迟或“ ping”以30Hz发送对象的位置。 Currently I am sending the objects as an ArrayList which I am sure is not the best way to go about this, maybe I am wrong. 目前,我将对象作为ArrayList发送,我确信这不是解决此问题的最佳方法,也许我错了。 Also, the code needs to allow for someone to join and send their player data (for example their name) without interfering with the ObjectInputStream that is expecting the locations rather than info. 此外,代码还需要允许某人加入并发送其玩家数据(例如他们的姓名),而不会干扰期望位置而不是信息的ObjectInputStream Here is what I have tried so far (Note that these are in a method that is called 30 times a second at regular intervals) 到目前为止,这是我尝试过的操作(请注意,这些方法以固定的时间间隔每秒被调用30次)

Client: 客户:

public void Update {
    ...
    outStream = new ObjectOutputStream(socket.getOutputStream());
    outStream.writeObject(w.getAllLocalEntities());
        //send objects created by this client (e.g bullets) and this client
    inStream = new ObjectInputStream(socket.getInputStream());
    w.setEntities((ArrayList<Entity>) inStream.readObject());
        //recieve all objects the server has recieved from all clients
}

Some objects are also created server side such as zombies etc. Server: 服务器端也会创建一些对象,例如僵尸等。服务器:

public void Update {
    ...
    inStream = new ObjectInputStream(socket.getInputStream());
    server.addEntities((ArrayList<Entity>) inStream.readObject());
        //recieve client objects
    outStream = new ObjectOutputStream(socket.getOutputStream());
    outStream.writeObject(server.getEntities());
        //send all objects
}

This seems to be very inefficient so I was wondering if anyone knows a better way of going about this. 这似乎效率很低,所以我想知道是否有人知道更好的解决方法。 My main goal would be to Minimise latency/ping bearing in mind there can be a lot of objects (upwards of 500). 考虑到可能存在很多对象(最多500个),我的主要目标是使延迟/ ping最小化。 So the question is... can you improve my method? 所以问题是...您可以改善我的方法吗? Any help is appreciated. 任何帮助表示赞赏。

Instead of sending data at 30Hz send data only when they changed. 仅在它们更改时发送数据,而不是以30Hz发送数据。

Otherwise you risk to send many unmodified informations and you wait (at least few milliseconds) before sending new modified data. 否则,您可能会冒险发送许多未修改的信息,并且等待(至少几毫秒)后才能发送新的修改后的数据。

Try to use an event driven paradigm : when a new event happens send the event to all the listeners. 尝试使用事件驱动的范例 :当发生新事件时,将事件发送给所有侦听器。


Additionally don't send the whole object and don't use standard object serialization. 另外,不要发送整个对象,也不要使用标准的对象序列化。 If an object is composed with a fix part and a variable part (for example name and type can be fixed, location and power can change) send the whole data the first time and after send only the variable part. 如果对象由固定部分和可变部分组成(例如,名称和类型可以固定,位置和功率可以更改),则第一次发送整个数据,而仅发送可变部分。 It saves lot of network traffic and cpu compsumption. 这样可以节省大量网络流量和cpu消耗。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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