RoomCloseEvent: reason property

Available since @irtio/client 5.0BrowsersNode

The reason read-only property of the RoomCloseEvent interface is the close reason the connection ended or dropped with.

Value

A string: the router’s or the room’s close reason, such as the one the room passed to room.kick(), or "" for none. It’s "" when the client closed with close(). When the first connection was aborted by its signal, it’s the signal’s reason as a string.

Description

reason is for showing to the player or logging. Branch on code, not on reason. On a close event, it’s also the reason of the event’s error.

Examples

Telling a kicked player why

A moderator’s kick carries a reason, which the client shows.

// kicked.ts
import { CloseCode, connect } from "@irtio/client";

const room = await connect("https://rooms.irt.io/my-game/plaza");
const notice = document.querySelector("#notice")!;

room.addEventListener("close", (event) => {
  if (event.code === CloseCode.Kicked) notice.textContent = `Removed from the room: ${event.reason || "no reason given"}`;
});

See also

Runtime availability

BrowsersNode
reason5.05.0