RoomCloseEvent: code property

Available since @irtio/client 5.0BrowsersNode

The 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. CloseCode names each, such as CloseCode.RoomHibernated for 4011.
  • 1000: on a close event, the client closed with close().
  • 0: on close, the first connection failed before any close: the room couldn’t be reached, timeout passed or the signal aborted.
  • Any other number: the transport’s own close code, on a reconnecting event.

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

Runtime availability

BrowsersNode
code5.05.0