/// <summary> /// Sends "ping" message to current room to help measure latency to the server. /// </summary> /// <param name="roomToPing">The <see cref="ColyseusRoom{T}" /> to ping.</param> private void RunPingThread(object roomToPing) { ColyseusRoom <ExampleRoomState> currentRoom = (ColyseusRoom <ExampleRoomState>)roomToPing; const float pingInterval = 0.5f; // seconds const float pingTimeout = 15f; //seconds int timeoutMilliseconds = Mathf.FloorToInt(pingTimeout * 1000); int intervalMilliseconds = Mathf.FloorToInt(pingInterval * 1000); DateTime pingStart; while (currentRoom != null) { _waitForPong = true; pingStart = DateTime.Now; _lastPing = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); _ = currentRoom.Send("ping"); while (currentRoom != null && _waitForPong && DateTime.Now.Subtract(pingStart).TotalSeconds < timeoutMilliseconds) { Thread.Sleep(200); } if (_waitForPong) { LSLog.LogError("Ping Timed out"); } Thread.Sleep(intervalMilliseconds); } }
/// <summary> /// Creates a new <see cref="ExampleNetworkedEntity"/> attributes and <see cref="ColyseusNetworkedEntityView"/>. /// </summary> /// <param name="room">The room the entity will be added to</param> /// <param name="attributes">Position for the new entity</param> /// <param name="viewToAssign">The provided view that will be assigned to the new <see cref="ExampleNetworkedEntity"/></param> /// <param name="callback">Callback that will be invoked with the newly created <see cref="ExampleNetworkedEntity"/></param> public void CreateNetworkedEntity(ColyseusRoom <ExampleRoomState> room, Dictionary <string, object> attributes = null, ColyseusNetworkedEntityView viewToAssign = null, Action <ExampleNetworkedEntity> callback = null) { try { string creationId = null; if (viewToAssign != null || callback != null) { creationId = Guid.NewGuid().ToString(); if (callback != null) { if (viewToAssign != null) { _creationCallbacks.Add(creationId, (newEntity) => { RegisterNetworkedEntityView(newEntity, viewToAssign); callback.Invoke(newEntity); }); } else { _creationCallbacks.Add(creationId, callback); } } else { _creationCallbacks.Add(creationId, (newEntity) => { RegisterNetworkedEntityView(newEntity, viewToAssign); }); } } _ = room.Send("createEntity", new EntityCreationMessage() { creationId = creationId, attributes = attributes }); } catch (System.Exception err) { LSLog.LogError(err.Message + err.StackTrace); } }