CREATE YOUR WORLD
멀티플레이
Multiplay Room Message
9분
멀티플레이 룸 메시지 zepeto 멀티플레이는 클라이언트와 서버(룸) 간의 메시지를 송수신하는 인터페이스를 제공합니다 서버는 개별 클라이언트에게 독점적으로 메시지를 전송하거나 모든 연결된 클라이언트에게 전송할 수 있습니다 전송 메시지 유형은 원시, 스키마, 사용자 정의(객체) 유형을 지원합니다 서버 api 서버 api는 다음 메서드를 제공합니다 api 설명 onmessage(type, callback) zepeto 멀티플레이 서버는 클라이언트가 보낸 메시지를 처리하기 위해 유형별로 매핑된 콜백을 등록할 수 있습니다 유형 매개변수는 문자열로 정의할 수 있습니다 client send(type, message) 특정 클라이언트에게 메시지를 보내는 기능입니다 broadcast(type, message) 방에 연결된 모든 클라이언트에게 메시지를 보내는 기능입니다 모든 연결된 클라이언트에게 보낼 수 있으며, 특정 클라이언트를 목록에서 제외할 수도 있습니다 zepeto multiplay 서버 api에 관심이 있으시면 문서를 참조하세요 📘 다음 가이드를 참조하세요 \[ 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'을 제외한 다른 모든 유형의 메시지가 전송될 때 트리거됩니다, // 'action'은 위에 정의된 특정 핸들러가 있습니다 console log(`${client sessionid}가 ${type} ${message}를 보냈습니다 `); }); } 메시지 보내기 다음은 client send를 사용하는 예입니다 oncreate() { this onmessage("echo", (client sandboxplayer, message string) => { // 발신자에게 메시지를 보냅니다 client send("echo", `에코 메시지 ${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) 서버에서 수신된 메시지는 addmessagehandler 콜백을 등록하여 수신할 수 있습니다 메시지 콜백을 등록할 때 수신할 메시지 유형을 정의할 수 있습니다 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"); 다양한 유형의 room 메시지 예제를 확인하세요 서버 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) => { // 'action' 메시지가 전송될 때 트리거됩니다 // 모든 클라이언트에게 메시지를 방송합니다 this broadcast("action taken", "행동이 취해졌습니다!"); }); this onmessage(" ", (client sandboxplayer, type, message string) => { // 다른 모든 유형의 메시지가 전송될 때 트리거됩니다, // 위에 정의된 "action"을 제외합니다 console log(`${client sessionid}가 ${type} ${message}를 보냈습니다 `); }); this onmessage("echo", (client sandboxplayer, message number) => { // 발신자에게 메시지를 보냅니다 client send("echo", `에코 메시지 ${message}`); }); this onmessage("fire", (client sandboxplayer, message string) => { // "fire" 이벤트를 트리거한 클라이언트를 제외한 모든 클라이언트에게 보냅니다 this broadcast("fire", message, { except client }); }); this onmessage("position", (client sandboxplayer, message) => { console log(`\[${client sessionid}] 위치 (${message x}, ${message y}, ${message z})`); }); // 사용자 정의 객체 "messagemodel" 유형의 메시지가 전송될 때 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", "action message"); // 서버에 "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 object "messagemodel" 전송 let message = {str 'test', 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); }); }; } }