RoomCloseEvent

Available since @irtio/client 5.0BrowsersNode

The RoomCloseEvent interface of the Client API is the event a RoomClient fires as its close event, when its connection ends for good, and as its reconnecting event, when the connection drops and the client reconnects. It extends Event, and has the fields of the web’s CloseEvent, with an error added.

Constructor

new RoomCloseEvent(type, code, reason, wasClean)
new RoomCloseEvent(type, code, reason, wasClean, error)
type
"close" or "reconnecting".
code, reason, wasClean and error
The event’s properties of the same names. error defaults to undefined.

The client creates the events it fires.

Instance properties

All are read only. It also has Event’s properties, such as type.

RoomCloseEvent.code
The close code, or 0 when connecting failed before any close.
RoomCloseEvent.reason
The close reason, or "".
RoomCloseEvent.wasClean
Whether this client closed with close().
RoomCloseEvent.error
On close, the IrtError that ended the connection, unless the client closed it itself.

Description

RoomCloseEvent is IRT’s own class rather than a subclass of CloseEvent, which not every runtime has.

Examples

Logging every drop and close

A game sends one listener both events and tells them apart by type.

// connection-log.ts
import { connect, type RoomCloseEvent } from "@irtio/client";

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

function ended(event: RoomCloseEvent): void {
  const what = event.type === "close" ? "closed" : "dropped, reconnecting";
  console.warn(`${what}: ${event.code} ${event.reason}`);
}
room.addEventListener("reconnecting", ended);
room.addEventListener("close", ended);

See also

Runtime availability

BrowsersNode
RoomCloseEvent5.05.0