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.
1
NetworkClient.Lobby.MessagePlayer(playerId, "Hello", (bool successful, SWLobbyError error) =>{
2
if (successful){
3
Debug.Log("Sent player message");
4
}
5
else{
6
Debug.Log("Failed to send player message " + error);
7
}
8
});
Copied!
In this example, we are sending serializable object messages to the target player. You can implemented your own messaging protocol.
1
[Serializable]
2
public class CustomMessageData{
3
public string method;
4
public string data;
5
}
Copied!
Sending a chat message.
1
CustomMessageData customMessageData = new CustomMessageData();
2
customMessageData.method = "chat";
3
customMessageData.data = "Hello!";
4
5
NetworkClient.Lobby.MessagePlayer(playerId, customMessageData, (bool successful, SWLobbyError error) =>{
6
if (successful)
7
{
8
Debug.Log("Sent player message");
9
}
10
else
11
{
12
Debug.Log("Failed to send player message " + error);
13
}
14
});
Copied!
Sending a room invite
1
CustomMessageData customMessageData = new CustomMessageData();
2
customMessageData.method = "room-invite";
3
customMessageData.data = roomId;
4
5
NetworkClient.Lobby.MessagePlayer(playerId, customMessageData, (bool successful, SWLobbyError error) =>{
6
if (successful){
7
Debug.Log("Sent player message");
8
}
9
else{
10
Debug.Log("Failed to send player message " + error);
11
}
12
});
Copied!
Copy link