Keep a connection alive
Prevent connection timeouts by regularly sending messages to the Relay server.
Read time 1 minuteLast updated 14 hours ago
Relay servers automatically disconnect clients after a period of inactivity. The default time to live (TTL) before Relay disconnects a client is 10 seconds. The disconnect TTL is 60 seconds when the host in alone (after the
BINDCONNECTUpdate()NetworkManagerStartClientStartHostNetworkDriver//Call the below regularly, e.g., in Monobehaviour.Update()void Example_KeepingConnectionAlive(){ // Update the NetworkDrivers regularly to ensure the host/player is kept online. if (HostDriver.IsCreated && isRelayServerConnected) { HostDriver.ScheduleUpdate().Complete(); //Accept incoming client connections while (HostDriver.Accept() != default(NetworkConnection)) { Debug.Log("Accepted an incoming connection."); } } if (PlayerDriver.IsCreated && clientConnection.IsCreated) { PlayerDriver.ScheduleUpdate().Complete(); //Resolve event queue NetworkEvent.Type eventType; while ((eventType = clientConnection.PopEvent(PlayerDriver, out _)) != NetworkEvent.Type.Empty) { if (eventType == NetworkEvent.Type.Connect) { Debug.Log("Client connected to the server"); } else if (eventType == NetworkEvent.Type.Disconnect) { Debug.Log("Client got disconnected from server"); clientConnection = default(NetworkConnection); } } }}