RoomCloseEvent: error property

Available since @irtio/client 5.0BrowsersNode

The error read-only property of the RoomCloseEvent interface is the IrtError that ended the connection, unless the client closed it itself.

Value

An IrtError or undefined.

  • On a close event, it’s the IrtError that ended the connection, with the close code as its code, or 0 when the first connection failed before any close. It’s undefined when the client closed with close(), when wasClean is true.
  • On a reconnecting event, it’s always undefined.

Description

The error’s name is one of the web’s error names, such as VersionError for 4004 or TimeoutError when the first connection took longer than timeout. When the client closes before it was ready, connect() and ready reject with the same error.

Examples

Reloading for a new build

A room build whose schema differs from the client’s closes the connection with 4004, a VersionError. Reloading the page fetches the client that matches it.

// new-build.ts
import { connect, reloadForNewBuild } from "@irtio/client";

const room = await connect("https://rooms.irt.io/my-game/arena");

room.addEventListener("close", ({ error }) => {
  if (error?.name === "VersionError") reloadForNewBuild();
  else if (error) console.error(`${error.name}: ${error.message}`);
});

Reload only on VersionError. The client already reconnects after the codes that allow it.

See also

Runtime availability

BrowsersNode
error5.05.0