Message Player

Overview

Sends messages to another player of your game. Players have to be in the same Lobby region to receive the messages.

SocketWeaver does not have services for managing user profiles. You need to use services like Google Firebase to store SocketWeaver playerIds to use this API. The overview of the workflow is demonstrated in the picture below.

Methods

Called by players to begin an asynchronous operation to send message to another player using the specified targetPlayerId and string messageData.

Called by players to begin an asynchronous operation to send message to another player using the specified targetPlayerId and serializable object messageData.

Class

The error that occurred when a lobby API operation failed.

Maximum size of a message data is 4.5KB.

Example

In this example, we are sending a simple string message to the target player.

NetworkClient.Lobby.MessagePlayer(playerId, "Hello", (bool successful, SWLobbyError error) =>{
    if (successful){
        Debug.Log("Sent player message");
    }
    else{
        Debug.Log("Failed to send player message " + error);
    }
});

In this example, we are sending serializable object messages to the target player. You can implemented your own messaging protocol.

[Serializable]
public class CustomMessageData{
    public string method;
    public string data;
}

Sending a chat message.

CustomMessageData customMessageData = new CustomMessageData();
customMessageData.method = "chat";
customMessageData.data = "Hello!";

NetworkClient.Lobby.MessagePlayer(playerId, customMessageData, (bool successful, SWLobbyError error) =>{
    if (successful)
    {
        Debug.Log("Sent player message");
    }
    else
    {
        Debug.Log("Failed to send player message " + error);
    }
});

Sending a room invite

CustomMessageData customMessageData = new CustomMessageData();
customMessageData.method = "room-invite";
customMessageData.data = roomId;

NetworkClient.Lobby.MessagePlayer(playerId, customMessageData, (bool successful, SWLobbyError error) =>{
    if (successful){
        Debug.Log("Sent player message");
    }
    else{
        Debug.Log("Failed to send player message " + error);
    }
});

Last updated