Skip to content

Commit b303891

Browse files
committed
send requestId to JOIN_ROOM. closes #26
1 parent d9a80fc commit b303891

File tree

4 files changed

+43
-33
lines changed

4 files changed

+43
-33
lines changed

Assets/ColyseusClient.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ IEnumerator Start () {
3333
room.Listen ("messages/:number", this.OnMessageAdded);
3434
room.Listen (this.OnChangeFallback);
3535

36+
room.OnData += (object sender, MessageEventArgs e) => Debug.Log(e.data);
37+
3638
int i = 0;
3739

3840
while (true)
@@ -42,7 +44,7 @@ IEnumerator Start () {
4244
// string reply = client.RecvString();
4345
if (client.error != null)
4446
{
45-
Debug.LogError ("Error: "+client.error);
47+
Debug.LogError ("Error: " + client.error);
4648
break;
4749
}
4850

Assets/Plugins/Colyseus/Client.cs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ public class Client
2525
public string id;
2626
protected UriBuilder endpoint;
2727

28-
protected Room room;
2928
protected Dictionary<string, Room> rooms = new Dictionary<string, Room> ();
29+
protected Dictionary<int, Room> connectingRooms = new Dictionary<int, Room> ();
30+
protected int joinRequestId;
3031

3132
protected Connection connection;
3233

@@ -100,11 +101,15 @@ public Room Join (string roomName, Dictionary<string, object> options = null)
100101
options = new Dictionary<string, object> ();
101102
}
102103

103-
this.room = new Room (roomName);
104+
int requestId = ++this.joinRequestId;
105+
options.Add ("requestId", requestId);
106+
107+
var room = new Room (roomName);
108+
this.connectingRooms.Add (requestId, room);
104109

105110
this.connection.Send (new object[]{Protocol.JOIN_ROOM, roomName, options});
106111

107-
return this.room;
112+
return room;
108113
}
109114

110115
void ParseMessage (byte[] recv)
@@ -119,16 +124,24 @@ void ParseMessage (byte[] recv)
119124
this.OnOpen.Invoke (this, EventArgs.Empty);
120125

121126
} else if (code == Protocol.JOIN_ROOM) {
122-
var room = this.room;
123-
room.id = (string) message [1];
127+
var requestId = (byte) message [2];
124128

125-
this.endpoint.Path = "/" + room.id;
126-
this.endpoint.Query = "colyseusid=" + this.id;
129+
Room room;
130+
if (this.connectingRooms.TryGetValue (requestId, out room)) {
131+
room.id = (string) message [1];
132+
133+
this.endpoint.Path = "/" + room.id;
134+
this.endpoint.Query = "colyseusid=" + this.id;
135+
136+
room.SetConnection (new Connection (this.endpoint.Uri));
137+
room.OnLeave += OnLeaveRoom;
127138

128-
room.SetConnection (new Connection (this.endpoint.Uri));
129-
room.OnLeave += OnLeaveRoom;
139+
this.rooms.Add (room.id, room);
140+
this.connectingRooms.Remove (requestId);
130141

131-
this.rooms.Add (room.id, room);
142+
} else {
143+
throw new Exception ("can't join room using requestId " + requestId.ToString());
144+
}
132145

133146
} else if (code == Protocol.JOIN_ERROR) {
134147
if (this.OnError != null)

Server/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"node": ">= 5.x"
1313
},
1414
"dependencies": {
15-
"colyseus": "^0.5.0",
15+
"colyseus": "^0.5.7",
1616
"express": "^4.13.3"
1717
},
1818
"devDependencies": {

Server/yarn.lock

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
# yarn lockfile v1
33

44

5+
"@gamestdio/timeline@^0.3.5":
6+
version "0.3.5"
7+
resolved "https://registry.yarnpkg.com/@gamestdio/timeline/-/timeline-0.3.5.tgz#35ffbe1621535b41dfb5d5009c4c52301bbb1fc6"
8+
dependencies:
9+
harmony-proxy "^1.0.1"
10+
511
"@gamestdio/timer@^1.1.7":
612
version "1.1.8"
713
resolved "https://registry.npmjs.org/@gamestdio/timer/-/timer-1.1.8.tgz#6e5424f6e137aa88935571628f790433429a2f14"
@@ -32,11 +38,7 @@
3238
dependencies:
3339
"@types/node" "*"
3440

35-
"@types/node@*":
36-
version "7.0.5"
37-
resolved "https://registry.yarnpkg.com/@types/node/-/node-7.0.5.tgz#96a0f0a618b7b606f1ec547403c00650210bfbb7"
38-
39-
"@types/node@^7.0.18":
41+
"@types/node@*", "@types/node@^7.0.18":
4042
version "7.0.39"
4143
resolved "https://registry.yarnpkg.com/@types/node/-/node-7.0.39.tgz#8aced4196387038113f6f9aa4014ab4c51edab3c"
4244

@@ -211,18 +213,19 @@ chokidar@^1.4.3:
211213
optionalDependencies:
212214
fsevents "^1.0.0"
213215

214-
clock.js@*, clock.js@^1.1.5:
216+
clock.js@^1.1.5:
215217
version "1.1.5"
216218
resolved "https://registry.yarnpkg.com/clock.js/-/clock.js-1.1.5.tgz#c1186cd7e328cc838006d1d2a74eac72070b8f45"
217219

218220
code-point-at@^1.0.0:
219221
version "1.1.0"
220222
resolved "https://registry.yarnpkg.com/code-point-at/-/code-point-at-1.1.0.tgz#0d070b4d043a5bea33a2f1a40e2edb3d9a4ccf77"
221223

222-
colyseus@^0.5.0:
223-
version "0.5.0"
224-
resolved "https://registry.npmjs.org/colyseus/-/colyseus-0.5.0.tgz#143552bddf6662d80ab3192e496ed952e88cd6b0"
224+
colyseus@^0.5.7:
225+
version "0.5.7"
226+
resolved "https://registry.yarnpkg.com/colyseus/-/colyseus-0.5.7.tgz#5766b28faf14302c139eac3c3f440d9bc0ef4fd9"
225227
dependencies:
228+
"@gamestdio/timeline" "^0.3.5"
226229
"@gamestdio/timer" "^1.1.7"
227230
"@types/debug" "^0.0.29"
228231
"@types/fossil-delta" "^0.2.1"
@@ -235,11 +238,10 @@ colyseus@^0.5.0:
235238
debug "^2.6.8"
236239
fossil-delta "^0.2.5"
237240
ip "^1.1.5"
238-
memshared "^0.9.14"
241+
memshared "^0.9.15"
239242
msgpack-lite "^0.1.26"
240243
nonenumerable "^1.0.1"
241244
shortid "^2.2.6"
242-
timeframe "^0.3.5"
243245
url-parse "^1.1.9"
244246
uws "8.14.1"
245247

@@ -946,9 +948,9 @@ [email protected]:
946948
version "0.3.0"
947949
resolved "https://registry.yarnpkg.com/media-typer/-/media-typer-0.3.0.tgz#8710d7af0aa626f8fffa1ce00168545263255748"
948950

949-
memshared@^0.9.14:
950-
version "0.9.14"
951-
resolved "https://registry.yarnpkg.com/memshared/-/memshared-0.9.14.tgz#e41024153d1e7761b2dae7c60bf1448df9ba0448"
951+
memshared@^0.9.15:
952+
version "0.9.15"
953+
resolved "https://registry.yarnpkg.com/memshared/-/memshared-0.9.15.tgz#1d93d07a15958fafe867523a9804f31917cbca34"
952954

953955
954956
version "1.0.1"
@@ -1527,13 +1529,6 @@ timed-out@^2.0.0:
15271529
version "2.0.0"
15281530
resolved "https://registry.yarnpkg.com/timed-out/-/timed-out-2.0.0.tgz#f38b0ae81d3747d628001f41dafc652ace671c0a"
15291531

1530-
timeframe@^0.3.5:
1531-
version "0.3.5"
1532-
resolved "https://registry.yarnpkg.com/timeframe/-/timeframe-0.3.5.tgz#c8cc612d22faacdd9b31e33e6072c85f21ab208c"
1533-
dependencies:
1534-
clock.js "*"
1535-
harmony-proxy "^1.0.1"
1536-
15371532
15381533
version "1.0.0"
15391534
resolved "https://registry.yarnpkg.com/touch/-/touch-1.0.0.tgz#449cbe2dbae5a8c8038e30d71fa0ff464947c4de"

0 commit comments

Comments
 (0)