保持连接活动状态
Prevent connection timeouts by regularly sending messages to the Relay server.
阅读时间2 分钟最后更新于 1 个月前
在客户端处于无活动状态一段时间后,Relay 服务器会自动与客户端断开连接。Relay 断开与客户端连接前的默认生存时间 (TTL) 为 10 秒。主机处于独立状态时(
BINDCONNECTUpdate()NetworkManagerStartClientNetworkDriver//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); } } }}