INetworkingService

Networking Service

Interface

This service provides access to all the networking functionality in Spatial: connectivity, server management, matchmaking, remove events (RPCs/Messaging), etc.

Properties

connectionStatus

The connection status to the current server. Most networking service functionality can only be performed if fully connected. This state is useful to be able to communicate connection status to the user.

isConnected

Is connected to server (this means the client is ready to receive and send messages). Shortcut for connectionStatus == ServerConnectionStatus.Connected

isMasterClient

Is the local client the master client? The master client is in charge of various server simulation tasks and is a good candidate for running code that should only run once per server instance.

isServerInstancingEnabled

Is the server instancing feature enabled for this space? If this is true, then multiple servers can exist for the space, if false, only one single server instance will ever exist (all participants in single server). This can be configured in the Space Package Config settings in Unity.

isServerOpen

Is the current server open? Open means that others can join the server. When closed others will be prevented from joining. Returns false if not connected.

isServerVisible

Is the current server visible to others? Invisible servers don't show up in the UI and are "hidden" to the matchmaking service. Returns false if not connected.

masterClientActorNumber

The actor number of the master client. The master client is in charge of various server simulation tasks and is a good candidate for running code that should only run once per server instance.

networkTime

Time in seconds that is synced with the server and all actors connected to the current server instance. The value is guaranteed to be the same across all actors and the server. Returns 0 if not connected.

remoteEvents

Interface for sending and receiving custom network messages

serverCount

Total number of active servers for the current space Returns 0 if not connected.

serverMaxParticipantCount

Total number of participants (actors) allowed to be in this server Returns 0 if not connected.

serverParticipantCount

Total number of participants in the current server Returns 0 if not connected.

serverParticipantCountUnique

Total number of unique participants in the current server Returns 0 if not connected.

spaceParticipantCount

Total number of participants for the current space, totalling all participants from all servers for this space. This is roughly accurate and is updated every few seconds.

Methods

GetServerProperties()

Get the current server properties. Server properties persist with the server instance until the server is closed, which happens when the last actor disconnects from the server. This only works if actively connected to the server. Check for isConnected before calling this.

SetServerMaxParticipantCount(int)

Set the max number of participants for the current server. You can only set this to a value that is less than or equal to the current participant (actor) count.

SetServerOpen(bool)

Set the current server open state. This is only allowed if connection state is "Connected". This option is only available if the server instancing feature is enabled for the current space.

SetServerProperties(IReadOnlyCollection<KeyValuePair<string, object>>)

Set properties for the current server. Server properties persist with the server instance until the server is closed, which happens when the last actor disconnects from the server. This only works if actively connected to the server. Check for isConnected before calling this.

SetServerVisible(bool)

Set the current server visibility state. This is only allowed if connection state is "Connected". This option is only available if the server instancing feature is enabled for the current space.

TeleportActorsToNewServer(IReadOnlyCollection<int>, int, bool, IReadOnlyCollection<KeyValuePair<string, object>>, IReadOnlyCollection<string>)

Similar to TeleportToNewServer, but allows specifying a list of actors to teleport to the new server. To check if the teleport was successful, listen to the onConnectionStatusChanged event.

TeleportToBestMatchServer(int, IReadOnlyCollection<KeyValuePair<string, object>>, IReadOnlyCollection<string>)

Teleport local actor to the best match server instance based on the specified properties. If no match can be found, a new server instance will be created with the specified properties.

For example:

  • A server exists that has a property "gameMode" with value "deathmatch"
  • You can call this method with serverProperties={"gameMode": "deathmatch"} and serverPropertiesToMatch={"gameMode"}
  • This will find the existing server and teleport the actor to that server
  • If no server exists with the specified properties, a new server will be created with the specified properties
TeleportToNewServer(int, bool, bool, IReadOnlyCollection<KeyValuePair<string, object>>, IReadOnlyCollection<string>)

Teleport local actor to a new server instance. This will disconnect the actor from the current server and connect to a new server instance, which will have the specified properties. To check if the teleport was successful, listen to the onConnectionStatusChanged event.

Events

onConnectionStatusChanged

Event that triggers when the server connection status changes

onMasterClientChanged

Event that triggers when the master client changes to a different actor. This will also get triggered after a reconnect or server change; You may reconnect or switch to a new server, where the master client actor is the same as before, but the associated user is different.

onServerParticipantCountChanged

Event that triggers when the participant count (actor count) for the current server changes. This is the total number of participants for the current server only, not for the space globally. This is also triggered when not connected and will have a value of 0.

onSpaceParticipantCountChanged

Event that triggers when the participant count (actor count) for the current space changes. This is the total number of participants for the current space, totalling all participants from all servers

Examples

void LogServerProperties()
{
INetworkingService ns = SpatialBridge.networkingService;
Debug.Log($"connectionStatus: {ns.connectionStatus}");
Debug.Log($"isConnected: {ns.isConnected}");
Debug.Log($"networkTime: {ns.networkTime}");
Debug.Log($"spaceParticipantCount: {ns.spaceParticipantCount}");
Debug.Log($"serverParticipantCount: {ns.serverParticipantCount}");
Debug.Log($"serverParticipantCountUnique: {ns.serverParticipantCountUnique}");
Debug.Log($"serverMaxParticipantCount: {ns.serverMaxParticipantCount}");
Debug.Log($"serverCount: {ns.serverCount}");
Debug.Log($"isServerOpen: {ns.isServerOpen}");
Debug.Log($"isServerVisible: {ns.isServerVisible}");
Debug.Log($"isServerInstancingEnabled: {ns.isServerInstancingEnabled}");
}
void LogServerProperties()
{
INetworkingService ns = SpatialBridge.networkingService;
Debug.Log($"connectionStatus: {ns.connectionStatus}");
Debug.Log($"isConnected: {ns.isConnected}");
Debug.Log($"networkTime: {ns.networkTime}");
Debug.Log($"spaceParticipantCount: {ns.spaceParticipantCount}");
Debug.Log($"serverParticipantCount: {ns.serverParticipantCount}");
Debug.Log($"serverParticipantCountUnique: {ns.serverParticipantCountUnique}");
Debug.Log($"serverMaxParticipantCount: {ns.serverMaxParticipantCount}");
Debug.Log($"serverCount: {ns.serverCount}");
Debug.Log($"isServerOpen: {ns.isServerOpen}");
Debug.Log($"isServerVisible: {ns.isServerVisible}");
Debug.Log($"isServerInstancingEnabled: {ns.isServerInstancingEnabled}");
}