Skip to content

GoWorld服务器和客户端之间通信协议详解

Seis edited this page Oct 28, 2017 · 6 revisions

Packet

+----------------------+-----------------+
| PayloadLength uint32 | Payload ...byte |
+----------------------+-----------------+

In GoWorld, clients and server communicate using TCP/KCP/WebSocket data stream. Data are sent by packets. The size of the payload is PayloadLength bytes.

Messages

The payload of packets is interpreted as messages. Each message consists of an uint16 message type and other data fields. Data fields have various types. Data field types are listed as below.

  • byte, uint16, uint32, uint64, float32, float64, ... : basic data types, little endian
  • bool: same as byte with value 1/0
  • Bytes: bytes of data whose size if predefined
  • VarBytes: an uint32 size followed with size bytes of data
  • ClientID: 16 bytes of Client ID
  • EntityID: 16 bytes of Entity ID
  • VarStr: same as VarBytes
  • Data: VarBytes with marshalled data as bytes
  • Args: an uint16 count followed with count number of arguments as Data fields

We use real go code to show the format of each message.

Create Entity (Server -> Client)

	packet.AppendUint16(MT_CREATE_ENTITY_ON_CLIENT)
	packet.AppendUint16(gid)
	packet.AppendClientID(clientid)
	packet.AppendBool(isPlayer)
	packet.AppendEntityID(entityid)
	packet.AppendVarStr(typeName)
	packet.AppendFloat32(x)
	packet.AppendFloat32(y)
	packet.AppendFloat32(z)
	packet.AppendFloat32(yaw)
	packet.AppendData(clientData)

Destroy Entity (Server -> Client)

	packet.AppendUint16(MT_DESTROY_ENTITY_ON_CLIENT)
	packet.AppendUint16(gid)
	packet.AppendClientID(clientid)
	packet.AppendVarStr(typeName)
	packet.AppendEntityID(entityid)

Call Client Entity Method (Server -> Client)

	packet.AppendUint16(MT_CALL_ENTITY_METHOD_ON_CLIENT)
	packet.AppendUint16(gid)
	packet.AppendClientID(clientid)
	packet.AppendEntityID(entityID)
	packet.AppendVarStr(method)
	packet.AppendArgs(args)

Entity Attribute Updates

Notify MapAttr Change (Server -> Client)

	packet.AppendUint16(MT_NOTIFY_MAP_ATTR_CHANGE_ON_CLIENT)
	packet.AppendUint16(gid)
	packet.AppendClientID(clientid)
	packet.AppendEntityID(entityid)
	packet.AppendData(path)  // path of the map attr
	packet.AppendVarStr(key) // map attr key
	packet.AppendData(val)   // map attr value