项目作者: britzl

项目描述 :
Super simple socket server with broadcast functionality
高级语言: Lua
项目地址: git://github.com/britzl/broadsock.git
创建时间: 2017-07-26T18:15:32Z
项目社区:https://github.com/britzl/broadsock

开源协议:MIT License

下载


Broadsock

Broadsock is a TCP socket broadcast server and client for the Defold game engine. The primary purpose of the server is to synchronize the positions of game objects in a Defold game and create and delete game object instances as they are created and deleted on remote clients. The Broadsock server will listen for client connections, maintain a list of currently connected clients and broadcast any received data to all connected clients but the sender.

The Broadsock server and client communicates using a message format that handles raw bytes, strings and numbers.

Message format

A Broadsock message has the following format:

  1. +------------+-~~~~~~~~~~~~-+-~~~~~~~~~~~~~~~-+-~~~~~~~~-+
  2. | LENGTH [4] | UID [string] | MSG_ID [string] | DATA [*] |
  3. +------------+-~~~~~~~~~~~~-+-~~~~~~~~~~~~~~~-+-~~~~~~~~-+
  • LENGTH = Length of the message in bytes
  • UID = Unique ID of the player that sent the message (only when broadcast from server)
  • MSG_ID = Message id
  • DATA = Message data

Message ids

Broadsock has a number of reserved message ids:

  • CONNECT_OTHER - Sent to other connected clients when a new client connects. Contains the unique user id of the connected client, as well as IP and port.
  • CONNET_SELF - Sent from the server to the connecting client. Contains a unique user id for the connected client.
  • DISCONNECT - Broadcast from the server when a client disconnects
  • GO - Sent from a client every update. Contains a list of transform updates for registered game objects. Will be broadcast to the other clients.
  • GOD - Sent from a client when a game object is unregistered. Will be broadcast to the other clients.

Installation

You can use the Broadsock client in your own project by adding this project as a Defold library dependency. Open your game.project file and in the dependencies field under project add:

  1. https://github.com/britzl/broadsock/archive/master.zip

Or point to the ZIP file of a specific release.

Usage

Client integration

The easiest way to get started with Broadsock is to add the broadsock/client/broadsock.go instance to a collection in your game and then use message passing to connect and register game objects:

  1. local BROADSOCK = msg.url("example:/broadsock#script")
  2. function init(self)
  3. -- Tell broadsock to connect to a Broadsock server instance
  4. msg.post(BROADSOCK, "connect", { ip = "127.0.0.1", port = 5000 })
  5. end
  6. function on_message(self, message_id, message, sender)
  7. if message_id == hash("connected") and sender == BROADSOCK then
  8. -- Register a bullet factory
  9. msg.post(BROADSOCK, "register_factory", { url = "/factory#bullet", type = "bullet" })
  10. end
  11. end
  12. function on_input(self, action_id, action)
  13. if action_id == hash("fire") and action.released then
  14. -- Register a bullet game object
  15. -- This game object will automatically sync it's position with other broadsock connected clients
  16. -- The receiving clients will automatically create game objects on their end
  17. self.bullet_id = factory.create("/factory#bullet")
  18. msg.post(BROADSOCK, "register_gameobject", { id = self.bullet_id, type = "bullet" })
  19. end
  20. end
  21. function update(self, dt)
  22. ... move bullet
  23. -- Unregister a game object
  24. -- This game object will no longer sync it's position with other broadsock connected clients
  25. -- The fact that the game object was unregistered will be sent to the other clients and their
  26. -- remote counter parts will automatically be deleted
  27. go.delete(self.bullet_id)
  28. msg.post(BROADSOCK, "unregister_gameobject", { id = self.bullet_id })
  29. end

Server

The Broadsock server is also written for Defold. It is recommended to run the server using a headless version of Defold but this is no requirement. The easiest way to get started is to add the broadsock/server/broadsock.go instance to a collection and bundle and run.

Once the server is running it will listen for Broadsock client connections. The server will broadcast any received message to connected clients.