สร้างโลกของคุณเอง
มัลติเพลย์
ข้อความในห้องเล่นหลายคน
8 นาที
ข้อความห้องหลายผู้เล่น zepeto multiplay ให้ส่วนติดต่อสำหรับการส่ง/รับข้อความระหว่างลูกค้าและเซิร์ฟเวอร์ (ห้อง) เซิร์ฟเวอร์สามารถส่งข้อความเฉพาะไปยังลูกค้าแต่ละรายหรือทั่วถึงลูกค้าทั้งหมดที่เชื่อมต่ออยู่ ประเภทข้อความที่ส่งสนับสนุนประเภท primitive, schema, custom(object) api เซิร์ฟเวอร์ api ของเซิร์ฟเวอร์มีวิธีการดังต่อไปนี้ api คำอธิบาย onmessage(type, callback) เซิร์ฟเวอร์ zepeto multiplay สามารถลงทะเบียน callback ที่แมพตามประเภทเพื่อจัดการข้อความที่ส่งโดยลูกค้า พารามิเตอร์ประเภทสามารถกำหนดเป็นสตริงได้ client send(type, message) ฟังก์ชันสำหรับส่งข้อความไปยังลูกค้าเฉพาะ broadcast(type, message) ฟังก์ชันสำหรับส่งข้อความไปยังลูกค้าทั้งหมดที่เชื่อมต่อกับห้อง คุณสามารถส่งไปยังลูกค้าทั้งหมดที่เชื่อมต่อ หรือคุณสามารถยกเว้นลูกค้าเฉพาะจากรายการได้ หากคุณสนใจใน api ของ zepeto multiplay server โปรดดูเอกสารประกอบ 📘 โปรดดูคู่มือดังต่อไปนี้ \[ zepeto multiplay(server) api https //developer zepeto me/docs/multiplay server/ ] รับข้อความ ต่อไปนี้คือตัวอย่างการใช้ onmessage oncreate() { this onmessage("action", (client sandboxplayer, message string) => { // จะถูกเรียกเมื่อส่งข้อความ 'action' }); this onmessage(" ", (client sandboxplayer, type, message string) => { // จะถูกเรียกเมื่อส่งข้อความประเภทอื่น, // ยกเว้น "action" ซึ่งมีตัวจัดการเฉพาะที่กำหนดไว้ข้างต้น console log(`${client sessionid} ส่ง ${type} ${message}`); }); } ส่งข้อความ ต่อไปนี้คือตัวอย่างการใช้ client send oncreate() { this onmessage("echo", (client sandboxplayer, message string) => { // ส่งข้อความไปยังผู้ส่ง client send("echo", `echo message ${message}`); }); } ต่อไปนี้คือตัวอย่างการใช้ broadcast ▼ ตัวอย่างการส่งข้อความไปยังลูกค้าทั้งหมด oncreate() { this onmessage("action", (client sandboxplayer, message string) => { // ส่งข้อความไปยังลูกค้าทั้งหมด this broadcast("action taken", "มีการดำเนินการแล้ว!"); }); } ▼ ตัวอย่างการส่งข้อความโดยไม่รวมลูกค้าบางราย oncreate() { this onmessage("fire", (client sandboxplayer, message string) => { // ส่งเหตุการณ์ "fire" ไปยังลูกค้าทุกคน ยกเว้นคนที่กระตุ้นมัน this broadcast("fire", message, { except client }); }); } api ของลูกค้า api ของลูกค้าให้วิธีการดังต่อไปนี้ room addmessagehandler(type, message) ข้อความที่ได้รับจากเซิร์ฟเวอร์สามารถรับได้โดยการลงทะเบียน callback ของ addmessagehandler คุณสามารถกำหนดประเภทข้อความที่คุณต้องการรับเมื่อทำการลงทะเบียน callback ข้อความ room send(type, message) อินเตอร์เฟซที่ใช้ในการส่งต่อข้อความไปยังเซิร์ฟเวอร์ คุณสามารถกำหนดประเภทข้อความที่จะส่งต่อได้ รับข้อความ ตัวอย่างต่อไปนี้คือการใช้ room addmessagehandler this multiplay roomjoined += (room room) => { // เพิ่มตัวฟังข้อความจากเซิร์ฟเวอร์ประเภท "fire" room addmessagehandler("fire", (message string) => { // พิมพ์ข้อความจากเซิร์ฟเวอร์ console log(message); }); }; ส่งข้อความ ตัวอย่างต่อไปนี้คือการใช้ room send room send("message", "hello"); ดูตัวอย่างของประเภทต่างๆ ของข้อความในห้อง เซิร์ฟเวอร์ import { sandbox, sandboxoptions, sandboxplayer } from 'zepeto multiplay'; interface messagemodel { str string, istest boolean } export default class extends sandbox { oncreate(options sandboxoptions) { this onmessage("action", (client sandboxplayer, message string) => { // triggers when 'action' message is sent // broadcast a message to all clients this broadcast("action taken", "มีการดำเนินการแล้ว!"); }); this onmessage(" ", (client sandboxplayer, type, message string) => { // triggers when any other type of message is sent, // excluding "action", which has its own specific handler defined above console log(`${client sessionid} ส่ง ${type} ${message}`); }); this onmessage("echo", (client sandboxplayer, message number) => { // send message to sender client send("echo", `ข้อความ echo ${message}`); }); this onmessage("fire", (client sandboxplayer, message string) => { // sends "fire" event to every client, except the one who triggered it this broadcast("fire", message, { except client }); }); this onmessage("position", (client sandboxplayer, message) => { console log(`\[${client sessionid}] ตำแหน่ง (${message x}, ${message y}, ${message z})`); }); // when custom object "messagemodel" type of message is sent this onmessage\<messagemodel>('channel object', (client sandboxplayer, message messagemodel) => { console log(`\[${client sessionid}] ประเภทข้อความ ${typeof (message)}, ข้อความ ${message str}`); }); } } ไคลเอนต์ import { room, roomdata } from 'zepeto multiplay'; import { zepetoscriptbehaviour } from 'zepeto script'; import { zepetoworldmultiplay } from 'zepeto world'; interface messagemodel { str string, istest boolean } export default class clientlogic extends zepetoscriptbehaviour { private multiplay zepetoworldmultiplay; start() { this multiplay = this gameobject getcomponent\<zepetoworldmultiplay>(); this multiplay roomjoined += (room room) => { // \[room send] // ส่งข้อความ "action" ไปยังเซิร์ฟเวอร์ room send("action", "ข้อความการกระทำ"); // ส่งข้อความ "message" ไปยังเซิร์ฟเวอร์ room send("message", "สวัสดี"); // ส่งข้อความ "echo" ไปยังเซิร์ฟเวอร์ room send("echo", 1234); // ส่งข้อมูล roomdata "posdata" ไปยังเซิร์ฟเวอร์ const posdata = new roomdata(); posdata add("x", this transform position x); posdata add("y", this transform position y); posdata add("z", this transform position z); room send("position", posdata getobject()); // ส่งวัตถุ custom "messagemodel" ไปยังเซิร์ฟเวอร์ let message = {str 'ทดสอบ', istest true} as messagemodel; room send("channel object", message); // \[room addmessagehandler] // เพิ่มตัวฟังข้อความจากเซิร์ฟเวอร์ประเภท "echo" room addmessagehandler("echo", (message string) => { // พิมพ์ข้อความจากเซิร์ฟเวอร์ console log(message); }); // เพิ่มตัวฟังข้อความจากเซิร์ฟเวอร์ประเภท "action taken" room addmessagehandler("action taken", (message string) => { // พิมพ์ข้อความจากเซิร์ฟเวอร์ console log(message); }); // เพิ่มตัวฟังข้อความจากเซิร์ฟเวอร์ประเภท "fire" room addmessagehandler("fire", (message string) => { // พิมพ์ข้อความจากเซิร์ฟเวอร์ console log(message); }); }; } }