Skip to content

Commit 38e7b9f

Browse files
authored
Merge pull request #44 from binbytes/master
Fix GetAvailableRooms method
2 parents 9d78610 + 4559cc9 commit 38e7b9f

File tree

3 files changed

+65
-31
lines changed

3 files changed

+65
-31
lines changed

Assets/Plugins/Colyseus/Client.cs

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -125,28 +125,28 @@ public Room ReJoin (string roomName, string sessionId)
125125
return this.Join(roomName, options);
126126
}
127127

128-
// /// <summary>
129-
// /// Request <see cref="Client"/> to join in a <see cref="Room"/>.
130-
// /// </summary>
131-
// /// <param name="roomName">The name of the Room to join.</param>
132-
// /// <param name="callback">Callback to receive list of available rooms</param>
133-
// public void GetAvailableRooms (string roomName, Action<RoomAvailable[]> callback)
134-
// {
135-
// int requestId = ++this.requestId;
136-
// this.connection.Send (new object[]{Protocol.ROOM_LIST, requestId, roomName});
137-
//
138-
// this.roomsAvailableRequests.Add (requestId, callback);
139-
//
140-
// // // USAGE
141-
// // this.client.GetAvailableRooms ("chat", (RoomAvailable[] obj) => {
142-
// // for (int i = 0; i < obj.Length; i++) {
143-
// // Debug.Log (obj [i].roomId);
144-
// // Debug.Log (obj [i].clients);
145-
// // Debug.Log (obj [i].maxClients);
146-
// // Debug.Log (obj [i].metadata);
147-
// // }
148-
// });
149-
// }
128+
/// <summary>
129+
/// Request <see cref="Client"/> to join in a <see cref="Room"/>.
130+
/// </summary>
131+
/// <param name="roomName">The name of the Room to join.</param>
132+
/// <param name="callback">Callback to receive list of available rooms</param>
133+
public void GetAvailableRooms (string roomName, Action<RoomAvailable[]> callback)
134+
{
135+
int requestId = ++this.requestId;
136+
this.connection.Send (new object[]{Protocol.ROOM_LIST, requestId, roomName});
137+
138+
this.roomsAvailableRequests.Add (requestId, callback);
139+
140+
// // USAGE
141+
// this.client.GetAvailableRooms ("chat", (RoomAvailable[] obj) => {
142+
// for (int i = 0; i < obj.Length; i++) {
143+
// Debug.Log (obj [i].roomId);
144+
// Debug.Log (obj [i].clients);
145+
// Debug.Log (obj [i].maxClients);
146+
// Debug.Log (obj [i].metadata);
147+
// }
148+
//});
149+
}
150150

151151
/// <summary>
152152
/// Close <see cref="Client"/> connection and leave all joined rooms.
@@ -214,11 +214,20 @@ private void ParseMessage (byte[] recv)
214214
if (this.OnError != null)
215215
this.OnError.Invoke (this, new ErrorEventArgs ((string) message [2]));
216216

217-
// } else if (code == Protocol.ROOM_LIST) {
218-
// var requestId = Convert.ToInt32(message[1]);
219-
// var rooms = (RoomAvailable[]) Convert.ChangeType(message[2], roomsAvailableResponse.GetType());
220-
// this.roomsAvailableRequests [requestId].Invoke (rooms);
221-
// this.roomsAvailableRequests.Remove (requestId);
217+
} else if (code == Protocol.ROOM_LIST) {
218+
219+
var requestId = Convert.ToInt32(message[1]);
220+
List<object> _rooms = (List<object>)message[2];
221+
RoomAvailable[] rooms = new RoomAvailable[_rooms.Count];
222+
223+
for (int i = 0; i < _rooms.Count; i++) {
224+
IDictionary<string, object> room = (IDictionary<string, object>)_rooms[i];
225+
RoomAvailable _room = ObjectExtensions.ToObject<RoomAvailable>(_rooms[i]);
226+
rooms[i] = _room;
227+
}
228+
229+
this.roomsAvailableRequests[requestId].Invoke(rooms);
230+
this.roomsAvailableRequests.Remove(requestId);
222231

223232
} else {
224233
if (this.OnMessage != null)

Assets/Plugins/Colyseus/Room.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
namespace Colyseus
1212
{
1313
public class RoomAvailable {
14-
public string roomId;
15-
public uint clients;
16-
public uint maxClients;
17-
public object metadata;
14+
public string roomId { get; set; }
15+
public uint clients { get; set; }
16+
public uint maxClients { get; set; }
17+
public object metadata { get; set; }
1818
}
1919

2020
/// <summary>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Reflection;
5+
using System.Text;
6+
7+
namespace Colyseus
8+
{
9+
public static class ObjectExtensions
10+
{
11+
public static T ToObject<T>(object source) where T : class, new()
12+
{
13+
var someObject = new T();
14+
var someObjectType = someObject.GetType();
15+
16+
foreach (var item in (IDictionary<string, object>)source) {
17+
someObjectType
18+
.GetProperty(item.Key)
19+
.SetValue(someObject, item.Value, null);
20+
}
21+
22+
return someObject;
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)