RoomCloseEvent: code property
Available since
@irtio/client 5.0BrowsersNodeThe code read-only property of the RoomCloseEvent interface is the close code the connection ended or dropped with.
Value
A number.
- 4000 to 4099: the router or the room closed the connection, with one of IRT’s codes.
CloseCodenames each, such asCloseCode.RoomHibernatedfor 4011. - 1000: on a
closeevent, the client closed withclose(). - 0: on
close, the first connection failed before any close: the room couldn’t be reached,timeoutpassed or thesignalaborted. - Any other number: the transport’s own close code, on a
reconnectingevent.
Description
On close, code is also the code of the event’s error. Errors and close codes lists every code, what causes it and whether the client reconnects after it.
Examples
Offering a rejoin after the room hibernates
A room that hibernates on its own request closes its connections with 4011, and the client doesn’t reconnect by itself. The game offers the player a button instead.
// rejoin.ts
import { CloseCode, connect } from "@irtio/client";
const room = await connect("https://rooms.irt.io/my-game/garden");
const rejoin = document.querySelector<HTMLButtonElement>("#rejoin")!;
room.addEventListener("close", (event) => {
if (event.code !== CloseCode.RoomHibernated) return;
rejoin.hidden = false;
rejoin.onclick = () => location.reload();
});
See also
RoomCloseEvent.reasonRoom.hibernate(), which closes with 4011- Errors and close codes
Runtime availability
| Browsers | Node | |
|---|---|---|
code | 5.0 | 5.0 |