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]
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);
});
};
}
}