connectionStatus

Property

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.

Returns ServerConnectionStatus

Examples

public Image loadingImage;
public GameObject lobbyArea;
public GameObject gameArea;
public Button joinGameButton;

void OnEnable()
{
SpatialBridge.networkingService.onConnectionStatusChanged += HandleConnectionStatusChanged;

joinGameButton.onClick.AddListener(JoinGame);
}

void OnDisable()
{
SpatialBridge.networkingService.onConnectionStatusChanged -= HandleConnectionStatusChanged;
}

void JoinGame()
{
SpatialBridge.networkingService.TeleportToBestMatchServer(
maxParticipants: 20,
serverProperties: new Dictionary<string, object> { { "isGame", true } }
);
}

private void HandleConnectionStatusChanged(ServerConnectionStatus status)
{
if (status == ServerConnectionStatus.Connected)
{
// Already connected, hide loading image
loadingImage.gameObject.SetActive(false);

// Check if we're on lobby or game server
if (SpatialBridge.networkingService.GetServerProperties().ContainsKey("isGame"))
{
HandleJoinedGameServer();
}
else
{
HandleJoinedLobbyServer();
}

}
else if (status == ServerConnectionStatus.Connecting)
{
loadingImage.gameObject.SetActive(true);
}
}

private void HandleJoinedLobbyServer()
{
lobbyArea.SetActive(true);
gameArea.SetActive(false);
}

private void HandleJoinedGameServer()
{
lobbyArea.SetActive(false);
gameArea.SetActive(true);

// Add additional game area setup here
}
public Image loadingImage;
public GameObject lobbyArea;
public GameObject gameArea;
public Button joinGameButton;

void OnEnable()
{
SpatialBridge.networkingService.onConnectionStatusChanged += HandleConnectionStatusChanged;

joinGameButton.onClick.AddListener(JoinGame);
}

void OnDisable()
{
SpatialBridge.networkingService.onConnectionStatusChanged -= HandleConnectionStatusChanged;
}

void JoinGame()
{
SpatialBridge.networkingService.TeleportToBestMatchServer(
maxParticipants: 20,
serverProperties: new Dictionary<string, object> { { "isGame", true } }
);
}

private void HandleConnectionStatusChanged(ServerConnectionStatus status)
{
if (status == ServerConnectionStatus.Connected)
{
// Already connected, hide loading image
loadingImage.gameObject.SetActive(false);

// Check if we're on lobby or game server
if (SpatialBridge.networkingService.GetServerProperties().ContainsKey("isGame"))
{
HandleJoinedGameServer();
}
else
{
HandleJoinedLobbyServer();
}

}
else if (status == ServerConnectionStatus.Connecting)
{
loadingImage.gameObject.SetActive(true);
}
}

private void HandleJoinedLobbyServer()
{
lobbyArea.SetActive(true);
gameArea.SetActive(false);
}

private void HandleJoinedGameServer()
{
lobbyArea.SetActive(false);
gameArea.SetActive(true);

// Add additional game area setup here
}