protocol object creation - Squarific/Game-Of-Everything GitHub Wiki

  • Encoding: UTF-8
  • Parsing: JSON
  • Description:

This protocol is designed to keep all objects in the world in sync on all clients.

  • How to use:

All messages should have a type property which is the constant string "object-creation".

Init

When you open the world you should send a init message. All clients that own objects in the world are supposed to send the create message when someone sends the init message.

It should also have a data property called data which is the constant string "init".

Create

When you want to create an object you send the create message. If you receive a create message you should if you want create the message. If the object with the given id already exists, you should just update it's propertys.

The create message should contain an id property. You should create a random string of sufficient length so that no two ids have a big chance of colliding. A good id length is 16

Optionally it could contain a position or speed property that are objects containing dimensions. For example: {"x": 4, "y": "3}. You should at least support x, y and optionally z. They should be initialized on 0. Position is units mostly pixels and speed is units per ms.

Destroying

If you want to destroy an object you can send the destroy message. With data set to the string "destroy" and your id.

Syncying

If you want to make sure all objects are in sync (i.e. objects that no longer have an owner shouldn't exist) you can just send the init message and remove all objects that noone sends you after a certain timeout.

  • Example:

Init message:

{"type": "object-creation", "data": "init"}

Create message:

{"type": "object-creation", "data": "create", "objecttype": "ball", "id": "jc9275nwan8xh72k2uw"} {"type": "object-creation", "data": "create", "objecttype": "ball", "id": "jc9275nwan8xh72k2uw", "position": {"x": 5}, "speed": {"x": 0.1}}

Destroy message:

{"type: "object-creation", "data": "destroy", "id": "jc9275nwan8xh72k2uw"}