← Cloudflare Realtime / realtime / realtimekit / core
Waiting Room
Funkce čekárny umožňuje hostitelům řídit, kdo se může připojit ke schůzce. Pokud je povolena, musí účastníci před vstupem do schůzky počkat na schválení.
Jak funguje čekárna
Poté, co zavoláte meeting.join(), nastane jedna ze dvou událostí:
roomJoined- Můžete se ke schůzce připojit okamžitěwaitlisted- Jste umístěni do čekárny a musíte počkat na schválení hostitelem
Použijte meeting.self.roomState ke sledování stavu uživatele ve schůzce.
Stavy čekárny
State Flow
join()
↓
[waitlisted] ←------ (host rejects)
↓ ↓
(host accepts) [rejected]
↓
[joined]Naslouchání změnám stavu
Událost Joined
Spouští se, když se místní uživatel úspěšně připojí ke schůzce.
Sledujte, kdy se místní uživatel připojí ke schůzce:
import { useRealtimeKitSelector } from "@cloudflare/realtimekit-react";
import { useEffect } from "react";
function MeetingStatus() {
const roomState = useRealtimeKitSelector((m) => m.self.roomState);
const joined = roomState === "joined";
useEffect(() => {
if (joined) {
console.log("Successfully joined the meeting");
}
}, [joined]);
return joined ? <div>You are in the meeting</div> : null;
}Alternativně použijte event listenery:
import { useRealtimeKitClient } from "@cloudflare/realtimekit-react";
useEffect(() => {
if (!meeting) return;
const handleRoomJoined = () => {
console.log("Successfully joined the meeting");
};
meeting.self.on("roomJoined", handleRoomJoined);
return () => {
meeting.self.off("roomJoined", handleRoomJoined);
};
}, [meeting]);meeting.self.on("roomJoined", () => {
// Local user is in the meeting
console.log("Successfully joined the meeting");
});meeting.addMeetingRoomEventListener(object : RtkMeetingRoomEventListener {
override fun onMeetingRoomJoinCompleted(meeting: RealtimeKitClient) {
// Local user is in the meeting
}
})extension MeetingViewModel: RtkMeetingRoomEventListener {
func onMeetingRoomJoinCompleted(meeting: RealtimeKitClient) {
// Local user is in the meeting
}
}Sledujte, kdy se místní uživatel připojí ke schůzce:
import { useRealtimeKitSelector } from "@cloudflare/realtimekit-react-native";
import { useEffect } from "react";
function MeetingStatus() {
const roomState = useRealtimeKitSelector((m) => m.self.roomState);
const joined = roomState === "joined";
useEffect(() => {
if (joined) {
console.log("Successfully joined the meeting");
}
}, [joined]);
return joined ? <Text>You are in the meeting</Text> : null;
}Alternativně použijte event listenery:
import { useRealtimeKitClient } from "@cloudflare/realtimekit-react-native";
useEffect(() => {
if (!meeting) return;
const handleRoomJoined = () => {
console.log("Successfully joined the meeting");
};
meeting.self.on("roomJoined", handleRoomJoined);
return () => {
meeting.self.off("roomJoined", handleRoomJoined);
};
}, [meeting]);Událost Waitlisted
Spouští se, když je místní uživatel přesunut do čekárny.
Sledujte, kdy je místní uživatel v čekárně:
function WaitingRoomStatus() {
const roomState = useRealtimeKitSelector((m) => m.self.roomState);
const isWaitlisted = roomState === "waitlisted";
useEffect(() => {
if (isWaitlisted) {
console.log("You are in the waiting room");
}
}, [isWaitlisted]);
return isWaitlisted ? <div>Waiting for host approval...</div> : null;
}Alternativně použijte event listenery:
useEffect(() => {
if (!meeting) return;
const handleWaitlisted = () => {
console.log("You are in the waiting room");
};
meeting.self.on("waitlisted", handleWaitlisted);
return () => {
meeting.self.off("waitlisted", handleWaitlisted);
};
}, [meeting]);meeting.self.on("waitlisted", () => {
// Local user is waitlisted
console.log("You are in the waiting room. Waiting for host approval...");
});meeting.addSelfEventListener(object : RtkSelfEventListener {
override fun onWaitListStatusUpdate(waitListStatus: WaitListStatus) {
when (waitListStatus) {
WAITING -> {
// Local user is in the waiting room
}
REJECTED -> {
// Local user's join room request was rejected by the host
}
NONE, ACCEPTED -> {
// Local user is not in the wait list or was already accepted
}
}
}
})extension MeetingViewModel: RtkSelfEventListener {
func onWaitListStatusUpdate(waitListStatus: WaitListStatus) {
switch waitListStatus {
case .accepted:
// Local user's join room request was accepted by the host
case .waiting:
// Local user is in the waiting room
case .rejected:
// Local user's join room request was rejected by the host
default:
return .none
}
}
}Sledujte, kdy je místní uživatel v čekárně:
function WaitingRoomStatus() {
const roomState = useRealtimeKitSelector((m) => m.self.roomState);
const isWaitlisted = roomState === "waitlisted";
useEffect(() => {
if (isWaitlisted) {
console.log("You are in the waiting room");
}
}, [isWaitlisted]);
return isWaitlisted ? <Text>Waiting for host approval...</Text> : null;
}Alternativně použijte event listenery:
useEffect(() => {
if (!meeting) return;
const handleWaitlisted = () => {
console.log("You are in the waiting room");
};
meeting.self.on("waitlisted", handleWaitlisted);
return () => {
meeting.self.off("waitlisted", handleWaitlisted);
};
}, [meeting]);Událost Rejected
Spouští se, když hostitel odmítne žádost o vstup.
Sledujte, kdy hostitel odmítne žádost o vstup:
function RejectionStatus() {
const roomState = useRealtimeKitSelector((m) => m.self.roomState);
const rejected = roomState === "rejected";
useEffect(() => {
if (rejected) {
console.log("Your entry request was rejected");
}
}, [rejected]);
return rejected ? <div>Your entry was rejected by the host</div> : null;
}Alternativně použijte event listenery:
useEffect(() => {
if (!meeting) return;
const handleRoomLeft = ({ state }) => {
if (state === "rejected") {
console.log("Your entry request was rejected");
}
};
meeting.self.on("roomLeft", handleRoomLeft);
return () => {
meeting.self.off("roomLeft", handleRoomLeft);
};
}, [meeting]);meeting.self.on("roomLeft", ({ state }) => {
if (state === "rejected") {
// Host rejected the entry
console.log("Your entry request was rejected");
}
});Když hostitel zamítne žádost o vstup, onWaitListStatusUpdate callback se aktivuje s WaitListStatus.REJECTED:
meeting.addSelfEventListener(object : RtkSelfEventListener {
override fun onWaitListStatusUpdate(waitListStatus: WaitListStatus) {
when (waitListStatus) {
WaitListStatus.REJECTED -> {
// Local user's join room request was rejected by the host
Log.d("WaitingRoom", "Your entry request was rejected")
}
WaitListStatus.WAITING -> {
// Local user is in the waiting room
}
WaitListStatus.ACCEPTED, WaitListStatus.NONE -> {
// Local user was accepted or not in waitlist
}
}
}
})Když hostitel zamítne žádost o vstup, onWaitListStatusUpdate callback se aktivuje s WaitListStatus.rejected:
extension MeetingViewModel: RtkSelfEventListener {
func onWaitListStatusUpdate(waitListStatus: WaitListStatus) {
switch waitListStatus {
case .rejected:
// Local user's join room request was rejected by the host
print("Your entry request was rejected")
case .waiting:
// Local user is in the waiting room
break
case .accepted:
// Local user's join room request was accepted by the host
break
default:
break
}
}
}Sledujte, kdy hostitel odmítne žádost o vstup:
function RejectionStatus() {
const roomState = useRealtimeKitSelector((m) => m.self.roomState);
const rejected = roomState === "rejected";
useEffect(() => {
if (rejected) {
console.log("Your entry request was rejected");
}
}, [rejected]);
return rejected ? <Text>Your entry was rejected by the host</Text> : null;
}Alternativně použijte event listenery:
useEffect(() => {
if (!meeting) return;
const handleRoomLeft = ({ state }) => {
if (state === "rejected") {
console.log("Your entry request was rejected");
}
};
meeting.self.on("roomLeft", handleRoomLeft);
return () => {
meeting.self.off("roomLeft", handleRoomLeft);
};
}, [meeting]);Sledování stavu pomocí roomState
Aktuální stav místnosti můžete zkontrolovat i přímo.
Zpracování všech stavů čekárny v jedné komponentě:
function WaitingRoomManager() {
const roomState = useRealtimeKitSelector((m) => m.self.roomState);
switch (roomState) {
case "init":
return <div>Connecting...</div>;
case "waitlisted":
return <div>Waiting for host approval...</div>;
case "joined":
return <div>You are in the meeting</div>;
case "rejected":
return <div>Your entry was rejected</div>;
case "left":
return <div>You left the meeting</div>;
case "kicked":
return <div>You were removed from the meeting</div>;
case "ended":
return <div>The meeting has ended</div>;
case "disconnected":
return <div>Connection lost</div>;
default:
return null;
}
}const currentState = meeting.self.roomState;
if (currentState === "waitlisted") {
console.log("Waiting for approval");
} else if (currentState === "joined") {
console.log("In the meeting");
} else if (currentState === "rejected") {
console.log("Entry was rejected");
}Ke sledování změn stavu použijte posluchače událostí uvedené výše.
Ke sledování změn stavu použijte posluchače událostí uvedené výše.
Zpracování všech stavů čekárny v jedné komponentě:
function WaitingRoomManager() {
const roomState = useRealtimeKitSelector((m) => m.self.roomState);
switch (roomState) {
case "init":
return <Text>Connecting...</Text>;
case "waitlisted":
return <Text>Waiting for host approval...</Text>;
case "joined":
return <Text>You are in the meeting</Text>;
case "rejected":
return <Text>Your entry was rejected</Text>;
case "left":
return <Text>You left the meeting</Text>;
case "kicked":
return <Text>You were removed from the meeting</Text>;
case "ended":
return <Text>The meeting has ended</Text>;
case "disconnected":
return <Text>Connection lost</Text>;
default:
return null;
}
}Akce hostitele
Hostitelé mohou spravovat žádosti o vstup z čekárny pomocí metod pro správu účastníků. Viz Vzdálení účastníci podrobnosti o:
acceptWaitingRoomRequest(participantId)- Přijme účastníka z čekárnyrejectWaitingRoomRequest(participantId)- Zamítnout žádost účastníka o vstup
Příklad: Hostitel přijímá účastníky
import {
useRealtimeKitClient,
useRealtimeKitSelector,
} from "@cloudflare/realtimekit-react";
function WaitingRoomHost() {
const [meeting] = useRealtimeKitClient();
const waitlistedParticipants = useRealtimeKitSelector((m) =>
m.participants.waitlisted.toArray(),
);
const acceptParticipant = async (participantId) => {
await meeting.participants.acceptWaitingRoomRequest(participantId);
};
const rejectParticipant = async (participantId) => {
await meeting.participants.rejectWaitingRoomRequest(participantId);
};
return (
<div>
<h3>Waiting Room ({waitlistedParticipants.length})</h3>
{waitlistedParticipants.map((participant) => (
<div key={participant.id}>
<span>{participant.name}</span>
<button onClick={() => acceptParticipant(participant.id)}>
Accept
</button>
<button onClick={() => rejectParticipant(participant.id)}>
Reject
</button>
</div>
))}
</div>
);
}// Get waitlisted participants
const waitlistedParticipants = meeting.participants.waitlisted.toArray();
// Accept the first waitlisted participant
if (waitlistedParticipants.length > 0) {
const participantId = waitlistedParticipants[0].id;
await meeting.participants.acceptWaitingRoomRequest(participantId);
}// Get waitlisted participants
val waitlistedParticipants = meeting.participants.waitlisted
// Accept a participant from the waiting room
if (waitlistedParticipants.isNotEmpty()) {
val participant = waitlistedParticipants[0]
meeting.participants.acceptWaitingRoomRequest(participant.id)
}
// Reject a participant's entry request
if (waitlistedParticipants.isNotEmpty()) {
val participant = waitlistedParticipants[0]
meeting.participants.rejectWaitingRoomRequest(participant.id)
}
// Listen for waiting room events
meeting.addWaitlistEventListener(object : RtkWaitlistEventListener {
override fun onWaitListParticipantJoined(participant: RtkRemoteParticipant) {
// Called when a new participant joins the waiting room
}
override fun onWaitListParticipantAccepted(participant: RtkRemoteParticipant) {
// Called when a waitlisted participant is accepted into the meeting
}
override fun onWaitListParticipantRejected(participant: RtkRemoteParticipant) {
// Called when a waitlisted participant is denied entry
}
override fun onWaitListParticipantClosed(participant: RtkRemoteParticipant) {
// Called when a waitlisted participant leaves the waiting room
}
})// Get waitlisted participants
let waitlistedParticipants = meeting.participants.waitlisted
// Accept a participant from the waiting room
if let participant = waitlistedParticipants.first {
meeting.participants.acceptWaitingRoomRequest(id: participant.id)
}
// Reject a participant's entry request
if let participant = waitlistedParticipants.first {
meeting.participants.rejectWaitingRoomRequest(participant.id)
}
// Listen for waiting room events
extension MeetingViewModel: RtkWaitlistEventListener {
func onWaitListParticipantJoined(participant: RtkRemoteParticipant) {
// Called when a new participant joins the waiting room
}
func onWaitListParticipantAccepted(participant: RtkRemoteParticipant) {
// Called when a waitlisted participant is accepted into the meeting
}
func onWaitListParticipantRejected(participant: RtkRemoteParticipant) {
// Called when a waitlisted participant is denied entry
}
func onWaitListParticipantClosed(participant: RtkRemoteParticipant) {
// Called when a waitlisted participant leaves the waiting room
}
}import {
useRealtimeKitClient,
useRealtimeKitSelector,
} from "@cloudflare/realtimekit-react-native";
import { View, Text, Button } from "react-native";
function WaitingRoomHost() {
const [meeting] = useRealtimeKitClient();
const waitlistedParticipants = useRealtimeKitSelector((m) =>
m.participants.waitlisted.toArray(),
);
const acceptParticipant = async (participantId) => {
await meeting.participants.acceptWaitingRoomRequest(participantId);
};
const rejectParticipant = async (participantId) => {
await meeting.participants.rejectWaitingRoomRequest(participantId);
};
return (
<View>
<Text>Waiting Room ({waitlistedParticipants.length})</Text>
{waitlistedParticipants.map((participant) => (
<View key={participant.id}>
<Text>{participant.name}</Text>
<Button
title="Accept"
onPress={() => acceptParticipant(participant.id)}
/>
<Button
title="Reject"
onPress={() => rejectParticipant(participant.id)}
/>
</View>
))}
</View>
);
}Osvědčené postupy
- Poskytujte jasnou zpětnou vazbu - Ukázat uživatelům, že jsou v čekárně a čekají na schválení
- Stanovení očekávání - Informovat uživatele, že se jejich žádost posuzuje
- Elegantní zpracování odmítnutí - Zobrazit přívětivou zprávu při zamítnutí vstupu
- Sledování změn stavu - Přihlásit se k odběru změn stavu místnosti a podle nich aktualizovat své uživatelské rozhraní
- Kontrola oprávnění - Zajistěte, aby vaše aplikace měla v předvolbě nastavena příslušná oprávnění pro používání funkcí čekárny