CREATE YOUR WORLD
Multiplay
Multiplay Room Message
9min
multiplay room message zepeto multiplay provides an interface for sending/receiving messages between clients and servers (room) the server can send messages exclusively to individual clients or globally to all connected clients the delivery message types supports primitive, schema, custom(object) types server api the server api provides the following method api description onmessage(type, callback) the zepeto multiplay server can register callbacks mapped them by type to handle messages sent by clients the type parameter can be defined as a string client send(type, message) function for sending messages to a specific client broadcast(type, message) function for sending messages to all clients connected to the room you can send it to all connected clients, or you can additionally exclude specific clients from the list if you're interested in the zepeto multiplay server api, refer to the documentation 📘 please refer to the following guide \[ zepeto multiplay(server) api https //developer zepeto me/docs/multiplay server/ ] receive a message the following is an example of using onmessage oncreate() { this onmessage("action", (client sandboxplayer, message string) => { // triggers when 'action' message is sent }); 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} sent ${type} ${message}`); }); } send message the following is an example of using client send oncreate() { this onmessage("echo", (client sandboxplayer, message string) => { // send messge to sender client send("echo", `echo message ${message}`); }); } the following is an example of using broadcast ▼ example of broadcasting a message to all clients oncreate() { this onmessage("action", (client sandboxplayer, message string) => { // broadcast a message to all clients this broadcast("action taken", "an action has been taken!"); }); } ▼ example of broadcasting a message with excluding specific clients oncreate() { 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 }); }); } client api the client api provides the following method room addmessagehandler(type, message) messages received from the server can be received by registering an addmessagehandler callback you can define the message type you want to receive when registering a message callback room send(type, message) interface used to forward messages to the server you can define the message type to forward receive a message the following is an example of using room addmessagehandler this multiplay roomjoined += (room room) => { // add server message listener type by "fire" room addmessagehandler("fire", (message string) => { // print server message console log(message); }); }; send message the following is an example of using room send room send("message", "hello"); check out examples of various types of room message server 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", "an action has been 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} sent ${type} ${message}`); }); this onmessage("echo", (client sandboxplayer, message number) => { // send message to sender client send("echo", `echo message ${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}] position (${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}] message type ${typeof (message)}, message ${message str}`); }); } } client 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] // send "action" message to server room send("action", "action message"); // send "message" message to server room send("message", "hello"); // send "echo" message to server room send("echo", 1234); // send roomdata "posdata" message to server 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()); // send custom object "messagemodel" to server let message = {str 'test', istest true} as messagemodel; room send("channel object", message); // \[room addmessagehandler] // add server message listener type by "echo" room addmessagehandler("echo", (message string) => { // print server message console log(message); }); // add server message listener type by "action taken" room addmessagehandler("action taken", (message string) => { // print server message console log(message); }); // add server message listener type by "fire" room addmessagehandler("fire", (message string) => { // print server message console log(message); }); }; } }