RoomCloseEvent: wasClean property

Available since @irtio/client 5.0BrowsersNode

The wasClean read-only property of the RoomCloseEvent interface says whether this client closed the connection itself, with close().

Value

A boolean. true on a close event after the client called close(), when error is undefined. false otherwise, and always false on a reconnecting event.

Description

Unlike the web’s CloseEvent.wasClean, it doesn’t say whether the transport closed cleanly: a room that closes the connection with a code, such as 4006 for a kick, gives false. A handoff that doesn’t keep this client closes it with close(), so its close event has wasClean true.

Examples

Going back to the menu after leaving

A Leave button closes the client, and only that close goes straight back to the menu.

// leave-button.ts
import { connect } from "@irtio/client";

const room = await connect("https://rooms.irt.io/my-game/arena");
document.querySelector("#leave")!.addEventListener("click", () => room.close());

room.addEventListener("close", (event) => {
  if (event.wasClean) console.log("back to the menu");
  else console.error(`lost the room: ${event.error?.message}`);
});

See also

Runtime availability

BrowsersNode
wasClean5.05.0