Chat

SWNetwork.Lobby has two messaging APIs to support player to player communication. Both APIs support serializable objects, so they are very flexible to use. You can use them to implement your own communication protocol.

Player to Player Message

In the LobbyDemo.cs script and add the following code to the OnPlayerSelected(string) method to send messages to the selected player.

public void OnPlayerSelected(string playerId)
{
Debug.Log("OnPlayerSelected: " + playerId);
// demonstrate player message API
GUI.ShowMessagePlayerPopup(playerId, (bool ok, string targetPlayerId, string message) =>
{
if (ok)
{
Debug.Log("Send player message " + "playerId= " + targetPlayerId + " message= " + message);
NetworkClient.Lobby.MessagePlayer(playerId, message, (bool successful, SWLobbyError error) =>
{
if (successful)
{
Debug.Log("Sent player message");
string msg = "Sent to " + targetPlayerId + ": " + message;
GUI.AddRowForMessage(msg, null, null);
}
else
{
Debug.Log("Failed to send player messagem " + error);
}
});
}
});
}

To receive the message, you need to subscribe to the OnPlayerMessageEvent lobby event. Update the Start() and onDestroy() methods to subscribe to the event.

void Start()
{
// Subscribe to Lobby events
NetworkClient.Lobby.OnNewPlayerJoinRoomEvent += Lobby_OnNewPlayerJoinRoomEvent;
NetworkClient.Lobby.OnPlayerLeaveRoomEvent += Lobby_OnPlayerLeaveRoomEvent;
NetworkClient.Lobby.OnRoomCustomDataChangeEvent += Lobby_OnRoomCustomDataChangeEvent;
NetworkClient.Lobby.OnPlayerMessageEvent += Lobby_OnPlayerMessageEvent;
}
void onDestroy()
{
// Unsubscrible to Lobby events
NetworkClient.Lobby.OnNewPlayerJoinRoomEvent -= Lobby_OnNewPlayerJoinRoomEvent;
NetworkClient.Lobby.OnPlayerLeaveRoomEvent -= Lobby_OnPlayerLeaveRoomEvent;
NetworkClient.Lobby.OnRoomCustomDataChangeEvent -= Lobby_OnRoomCustomDataChangeEvent;
NetworkClient.Lobby.OnPlayerMessageEvent -= Lobby_OnPlayerMessageEvent;
}

Implement the OnPlayerMessageEvent handler method.

void Lobby_OnPlayerMessageEvent(SWMessagePlayerEventData eventData)
{
string msg = eventData.playerId + ": " + eventData.data;
GUI.AddRowForMessage(msg, null, null);
}

Player to Room Broadcast

Add the follow code to SendRoomMessage(string) method to send message to local player's current room.

public void SendRoomMessage(string message)
{
Debug.Log("Send room message " + message);
NetworkClient.Lobby.MessageRoom(message,(bool successful, object arg2, SWLobbyError error) =>
{
if (successful)
{
Debug.Log("Sent room message");
string msg = "Sent to room: " + message;
GUI.AddRowForMessage(msg, null, null);
}
else
{
Debug.Log("Failed to send room messagem " + error);
}
});
}

To receive the message, you need to subscribe to the OnPlayerMessageEvent lobby event. Update the Start() and onDestroy() methods to subscribe to the event.

void Start()
{
// Subscribe to Lobby events
NetworkClient.Lobby.OnNewPlayerJoinRoomEvent += Lobby_OnNewPlayerJoinRoomEvent;
NetworkClient.Lobby.OnPlayerLeaveRoomEvent += Lobby_OnPlayerLeaveRoomEvent;
NetworkClient.Lobby.OnRoomCustomDataChangeEvent += Lobby_OnRoomCustomDataChangeEvent;
NetworkClient.Lobby.OnPlayerMessageEvent += Lobby_OnPlayerMessageEvent;
NetworkClient.Lobby.OnRoomMessageEvent += Lobby_OnRoomMessageEvent;
}
void onDestroy()
{
// Unsubscrible to Lobby events
NetworkClient.Lobby.OnNewPlayerJoinRoomEvent -= Lobby_OnNewPlayerJoinRoomEvent;
NetworkClient.Lobby.OnPlayerLeaveRoomEvent -= Lobby_OnPlayerLeaveRoomEvent;
NetworkClient.Lobby.OnRoomCustomDataChangeEvent -= Lobby_OnRoomCustomDataChangeEvent;
NetworkClient.Lobby.OnPlayerMessageEvent -= Lobby_OnPlayerMessageEvent;
NetworkClient.Lobby.OnRoomMessageEvent -= Lobby_OnRoomMessageEvent;
}

Implement the OnRoomMessageEvent handler method.

void Lobby_OnRoomMessageEvent(SWMessageRoomEventData eventData)
{
string msg = "Room message: " + eventData.data;
GUI.AddRowForMessage(msg, null, null);
}