创造你的世界
多人游戏

从房间中移除特定用户

4min
您可以在运行时将某些用户踢出房间。 请创建服务器代码 index ts sandbox。 函数定义 kick(client sandboxplayer, reason? string) promise; 这是一个在接收到带有 sessionid 的 sandboxplayer 后将其踢出的函数。 \ 如果您被踢出,将会出现一个弹出窗口,通知您已被踢出,但该弹出窗口是 zepeto 应用的 ui,编辑器无法检查。 📘 请参考以下指南。 \[ zepeto multiplay(server) api https //developer zepeto me/docs/multiplay server/classes/zepeto multiplay sandbox#kick ] 首先,要获取用户信息,请定义 schema types 和 roomstate,如下所示: 使用示例 使用 userinfos 中的数据获取将被踢出的用户的 session id。 导入带有 session 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(`尝试踢出 ${player userid}`); await this kick(player); this broadcast("log", `踢出 ${player userid}`); } } 👍 默认情况下,被踢出的用户可以再次进入房间 为了防止这种情况,使用基于 userid 的 datastorage 管理功能来存储被用户踢出的房间信息。 您可以在尝试进入房间时调用踢出功能来实现。