RoomClient: handoff event
@irtio/client 5.0BrowsersNodeThe handoff event of the RoomClient interface fires when a handoff connected the player to another room, such as a match a lobby started, or failed to.
Syntax
addEventListener("handoff", (event) => {})
Event type
A CustomEvent whose detail is an object with these properties:
room- A string. The new room’s ID.
url- A string. The new room’s URL.
type- A string. The new room’s type.
client- The
RoomClientconnected to the new room, which holds its state. Its kinds are typedany, since the room type is known only at runtime. Absent when the handoff failed. error- The
IrtErrorthat kept the client from connecting. Absent when the handoff succeeded. as(schema)- A method that returns
clienttyped forschema, the schematargetsnames fortype, so its state and calls are checked. It throws anErrorwhen the room’s schema hash isn’tschema’s, and throwserrorwhen the handoff failed.
Description
When the router hands the player off, this client connects to the new room itself, with the same token or guest, and the schema connect()’s targets option names for the room’s type, or the empty schema for a type targets doesn’t name.
Once the new client holds the room’s state, this client waits up to 3 seconds for the replies to its calls in flight, closes and fires handoff. Its close event fires first, with wasClean true. A handoff that keeps the old room’s connection, as a party’s does, leaves this client open.
If the new client fails for good, handoff carries its error, and this client stays connected to the room it was in.
Examples
Following a lobby to its match
The lobby’s client passes the match’s schema in targets, and the handoff’s client is typed with it.
// follow-match.ts
import { connect, defineSchema, entity } from "@irtio/client";
const Race = entity("race", { lap: "u8" });
const matchSchema = defineSchema({ state: Race });
const lobby = await connect("https://rooms.irt.io/my-game/lobby", { type: "lobby", targets: { match: matchSchema } });
lobby.addEventListener("handoff", ({ detail: handoff }) => {
if (handoff.error) {
console.error(`couldn't join the match: ${handoff.error.message}`);
return;
}
const match = handoff.as(matchSchema);
console.log(`racing in ${handoff.room}, lap ${match.state.lap}`);
});
To connect to the match again later by URL, such as after a page reload, pass its type: connect(handoff.url, { type: "match" }).
See also
lobbyeventconnect(), for thetargetsoptionRoom.handoff(), the room side- Lobbies and parties, the guide
Runtime availability
| Browsers | Node | |
|---|---|---|
handoff event | 5.0 | 5.0 |