RoomCloseEvent
Available since
@irtio/client 5.0BrowsersNodeThe 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,wasCleananderror- The event’s properties of the same names.
errordefaults toundefined.
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, theIrtErrorthat 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
closeeventreconnectingevent- Errors and close codes, for every code
Runtime availability
| Browsers | Node | |
|---|---|---|
RoomCloseEvent | 5.0 | 5.0 |