Skip to content

Commit e36ce02

Browse files
committed
auto-reconnection: trigger on_leave() after on_drop() in case of failure to reconnect
1 parent 715b907 commit e36ce02

File tree

4 files changed

+14
-11
lines changed

4 files changed

+14
-11
lines changed

colyseus/protocol.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ return {
2525
CONSENTED = 4000,
2626
SERVER_SHUTDOWN = 4001,
2727
WITH_ERROR = 4002,
28+
FAILED_TO_RECONNECT = 4003,
2829
MAY_TRY_RECONNECT = 4010,
2930
}
3031
}

colyseus/room.lua

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ end
262262
function Room:_handle_reconnection()
263263
if (os.time() - self._joined_at_time) < (self.reconnection.min_uptime / 1000) then
264264
print(string.format("[Colyseus reconnection]: ❌ Room has not been up for long enough for automatic reconnection. (min uptime: %dms)", self.reconnection.min_uptime))
265+
self:emit("leave", { code = protocol.CLOSE_CODE.ABNORMAL_CLOSURE })
265266
return
266267
end
267268

@@ -275,6 +276,14 @@ end
275276

276277
---@private
277278
function Room:_retry_reconnection()
279+
if self.reconnection.retry_count >= self.reconnection.max_retries then
280+
-- No more retries
281+
print(string.format("[Colyseus reconnection]: ❌ Reconnection failed after %d attempts.", self.reconnection.max_retries))
282+
self.reconnection.is_reconnecting = false
283+
self:emit("leave", { code = protocol.CLOSE_CODE.FAILED_TO_RECONNECT })
284+
return
285+
end
286+
278287
self.reconnection.retry_count = self.reconnection.retry_count + 1
279288

280289
local delay_ms = math.min(self.reconnection.max_delay, math.max(self.reconnection.min_delay, self.reconnection.backoff(self.reconnection.retry_count, self.reconnection.delay)))
@@ -289,14 +298,7 @@ function Room:_retry_reconnection()
289298

290299
on_error = function(e)
291300
room.connection:off("error", on_error)
292-
293-
if room.reconnection.retry_count < room.reconnection.max_retries then
294-
room:_retry_reconnection()
295-
else
296-
print("[Colyseus reconnection]: ❌ Failed to reconnect. Is your server running? Please check server logs.")
297-
room.reconnection.is_reconnecting = false
298-
room:emit("leave", { code = protocol.CLOSE_CODE.ABNORMAL_CLOSURE, reason = "reconnection failed" })
299-
end
301+
room:_retry_reconnection()
300302
end
301303

302304
on_open = function()
@@ -305,7 +307,7 @@ function Room:_retry_reconnection()
305307
end
306308

307309
timer.delay(delay_sec, false, function()
308-
print(string.format("[Colyseus reconnection]: Re-establishing sessionId '%s' with roomId '%s'... (attempt %d of %d)", room.session_id, room.room_id, room.reconnection.retry_count, room.reconnection.max_retries))
310+
print(string.format("[Colyseus reconnection]: 🔄 Re-establishing sessionId '%s' with roomId '%s'... (attempt %d of %d)", room.session_id, room.room_id, room.reconnection.retry_count, room.reconnection.max_retries))
309311

310312
room.connection:on("error", on_error)
311313
room.connection:on("open", on_open)

example/main.script

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ local function join_my_room()
2020
my_room = room
2121

2222
-- allow the client to reconnect immediately
23-
room.reconnection.min_uptime = 0
23+
-- room.reconnection.min_uptime = 0
2424

2525
print("[MyRoom] room_id: " .. room.room_id)
2626

release.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"version": "0.17.1"
2+
"version": "0.17.2"
33
}

0 commit comments

Comments
 (0)