特定のユーザーをルームから削除
4 分
特定のユーザーをランタイム中にルームからキックすることができます。
- サーバーコード index.ts Sandbox を作成してください。
関数定義
kick(client: SandboxPlayer, reason?: string): Promise; | これは、SessionIDを持つSandboxPlayerを受け取った後にSandboxPlayerをキックするための関数です。 - キックされると、キックされたことを知らせるポップアップが表示されますが、そのポップアップはZEPETOアプリのUIであり、エディターでは確認できません。 |
|---|
📘 次のガイドを参照してください。 [ZEPETO.Multiplay(Server) API]
まず、ユーザー情報を取得するために、Schema TypesとRoomStateを次のように定義します:

使用例
- UserInfosのデータを使用して、ユーザーIDから追い出されるユーザーのセッションIDを取得します。
- セッションIDを持つサンドボックスプレイヤーをインポートした後、キック関数を呼び出します。
- 誰が追い出されたかをブロードキャストを通じてお知らせできます。
import { Sandbox, SandboxOptions, SandboxPlayer } from 'ZEPETO.Multiplay';
import { DataStorage, loadDataStorage } from 'ZEPETO.Multiplay.DataStorage';
import { UserInfo } from 'ZEPETO.Multiplay.Schema';
export default class extends Sandbox {
onCreate(options: SandboxOptions) {
this.onMessage("Kick", (client: SandboxPlayer, message: string) => {
this.tryKick(client, message);
});
}
onJoin(client: SandboxPlayer) {
const user = new UserInfo();
user.sessionId = client.sessionId;
user.userId = client.userId;
this.state.UserInfos.set(client.userId, user);
}
async tryKick(client: SandboxPlayer, userId: string) {
let player: SandboxPlayer;
if (userId == null) {
player = client;
} else {
const kickPlayerSessionId: string = this.state.UserInfos.get(userId).sessionId;
player = this.loadPlayer(kickPlayerSessionId);
}
console.log(`try kick : ${player.userId}`);
await this.kick(player);
this.broadcast("Log", `kick : ${player.userId}`);
}
}👍 デフォルトでは、追い出されたユーザーは再度部屋に入ることができます
- これを防ぐために、UserIdに基づいてデータストレージ管理機能を使用して、ユーザーによって追い出された部屋の情報を保存します。
- 部屋に入ろうとする際にキックを呼び出すことで実装できます。