Client Connections
Communication between Clients
Last updated
Communication between Clients
Last updated
Client Connections are CoherenceSyncs that the CoherenceMonoBridge can handle for you and let you uniquely identify users connected, find them by their ID, spawn CoherenceSyncs whenever a new user joins the session, and send commands between those users.
When using Client Connections, CoherenceMonoBridge will spawn a CoherenceSync for each connection (Client or Simulator). Those CoherenceSyncs are subject to a different ruleset than standard CoherenceSyncs:
They can't be created or destroyed by the Client - they are always driven by CoherenceMonoBridge.
They are global - they are replicated across Clients regardless of the LiveQuery extent.
Client Connections shine whenever there's a need to communicate something to all the connected players. Usage examples:
Global chat
Game state changes: game started, game ended, map changed
Server announcements
Server-wide leaderboard
Server-wide events
The global nature of Client Connections doesn't fit all game types - for example, it rarely makes sense to keep every Client informed about the presence of all players on the server in an MMORPG. If this is your use case, don't set Client Connections on your CoherenceMonoBridge.
To enable Client Connections, turn Global Query on in your CoherenceMonoBridge (it should be by default):
Disabling Global Query on one Client doesn't affect other Clients, i.e. the ClientConnection Object of this Client will still be visible to other Clients that have the Global Query turned on.
Most of the Client Connection functionality is accessible through the CoherenceMonoBridge.ClientConnections
object:
Each connection is represented by a plain C# CoherenceClientConnection
object. It contains all the important information about a connection - its ClientID
, Type
, whether it IsMyConnection
, and a reference to the GameObject
and Coherence Sync
associated with it.
The CoherenceClientConnection.ClientID
is guaranteed to not change during a connection's lifetime. However, if a Client disconnects and then connects again to the same Room/World, a new ClientID
will be assigned (since a new connection was established).
Each Client Connection can have a CoherenceSync automatically being spawned and associated with it. Those objects, like any other objects with CoherenceSync, can be used for syncing properties or sending messages, with a little twist - they are global and thus not limited by the LiveQuery extent. That makes them perfect candidates for operations like:
Syncing global information - name, stats, tags, etc.
Sending global messages - chat, server interaction
To enable connection objects:
This step is described in detail in the Prefab setup section. In short, a Prefab with a CoherenceSync and a custom component (PlayerConnection
in this example) must be created and placed in a Resources folder:
For the system to know which object to create for every new Client connection, we have to link our Prefab to the CoherenceMonoBridge. Simply drag the prefab to the Client field in the inspector:
From now on every new connection will be assigned an instance of this Prefab, which can be accessed through the CoherenceClientConnection.GameObject
property.
Note that there's a separate field for the Simulator Connection Prefab. It can be used to spawn a completely different object for the Simulator connection that may contain Simulator-specific commands and replicated properties. If the field is left empty, no object will be created for the Simulator connection.
The Prefab selection process can be also controlled from code using the CoherenceMonoBridge.ClientConnections.ProvidePrefab
callback:
A Prefab provided through the ProvidePrefab
callback takes precedence over Prefabs linked in the inspector.
Client Messages are commands sent between the Client connection objects. Implementing Client Messages is as simple as creating a command on the CoherenceSync used by the Client Connection Prefab in the CoherenceMonoBridge:
Don't forget to bind to the new method to define a command:
Client Messages can be sent using the CoherenceClientConnection.SendClientMessage
method:
If the ClientID
of the message recipient is known we can use the CoherenceMonoBridge.ClientConnections
directly to send a client message: