Skip to content

Commit

Permalink
add GetRoomsAvailable button on example. workaround #66
Browse files Browse the repository at this point in the history
  • Loading branch information
endel committed May 21, 2019
1 parent b2115f0 commit 2bfc3f6
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
18 changes: 17 additions & 1 deletion Assets/ColyseusClient.cs
Expand Up @@ -12,7 +12,7 @@
public class ColyseusClient : MonoBehaviour {

// UI Buttons are attached through Unity Inspector
public Button m_ConnectButton, m_JoinButton, m_ReJoinButton, m_SendMessageButton, m_LeaveButton;
public Button m_ConnectButton, m_JoinButton, m_ReJoinButton, m_SendMessageButton, m_LeaveButton, m_GetAvailableRoomsButton;
public InputField m_EndpointField;
public Text m_IdText, m_SessionIdText;

Expand All @@ -32,6 +32,7 @@ public class ColyseusClient : MonoBehaviour {
m_ReJoinButton.onClick.AddListener(ReJoinRoom);
m_SendMessageButton.onClick.AddListener(SendMessage);
m_LeaveButton.onClick.AddListener(LeaveRoom);
m_GetAvailableRoomsButton.onClick.AddListener(GetAvailableRooms);

/* Always call Recv if Colyseus connection is open */
while (true)
Expand Down Expand Up @@ -139,6 +140,21 @@ void LeaveRoom()
entities.Clear();
}

void GetAvailableRooms()
{
client.GetAvailableRooms(roomName, (RoomAvailable[] roomsAvailable) =>
{
Debug.Log("Available rooms (" + roomsAvailable.Length + ")");
for (var i=0; i< roomsAvailable.Length;i++)
{
Debug.Log("roomId: " + roomsAvailable[i].roomId);
Debug.Log("maxClients: " + roomsAvailable[i].maxClients);
Debug.Log("clients: " + roomsAvailable[i].clients);
Debug.Log("metadata: " + roomsAvailable[i].metadata);
}
});
}

void SendMessage()
{
if (room != null)
Expand Down
Binary file modified Assets/ExampleScene.unity
Binary file not shown.
2 changes: 0 additions & 2 deletions Assets/Plugins/Colyseus/Room.cs
Expand Up @@ -2,8 +2,6 @@
using System.IO;
using System.Collections;
using System.Collections.Generic;

using UnityEngine;
using GameDevWare.Serialization;

namespace Colyseus
Expand Down
12 changes: 9 additions & 3 deletions Assets/Plugins/Colyseus/Utils/ObjectExtensions.cs
Expand Up @@ -14,9 +14,15 @@ public static T ToObject<T>(object source) where T : class, new()
var someObjectType = someObject.GetType();

foreach (var item in (IDictionary<string, object>)source) {
someObjectType
.GetProperty(item.Key)
.SetValue(someObject, item.Value, null);
var prop = someObjectType.GetProperty(item.Key);
try
{
prop.SetValue(someObject, Convert.ChangeType(item.Value, prop.PropertyType), null);

} catch (OverflowException e) {
// workaround for parsing Infinity on RoomAvailable.maxClients
prop.SetValue(someObject, uint.MaxValue, null);
}
}

return someObject;
Expand Down

0 comments on commit 2bfc3f6

Please sign in to comment.