Broadcasting in PurrNet is useful, as it allows us to do some basic network functionality without needing a Network Behaviour on our object. For things that are trivial, such as game chat, we don't necessarily need all the functionality of a Network Behaviour.
The idea is as follows:
Create a ChatMessage struct to store data such as a name and message.
Send a ChatMessage with our desired name and message to the Server.
Receive ChatMessage on the Server, then broadcast the ChatMessage to all Clients.
Receive ChatMessage on the Clients, then print the ChatMessage out.
Creating the ChatMessage struct:
To get our chat message to the Server, we need to first create a struct to hold our data. As previously mentioned, this struct will hold a name, and a message. This struct will need to implement the IPackedAuto interface, which will automatically handle the reading and writing of the data to the network. If this is not your style, take a look at the IPacked and IPackedSimple interfaces.
For our case, let's create a ChatManager script, that inherits from PurrMonoBehaviour and subscribe to our chat events, as well as Creating an OnChatMessage function to pass in as our callback:
publicclassChatManager:PurrMonoBehaviour{ // Subscribe to ChatMessage events as either the Server, Client, or bothpublicoverridevoidSubscribe(NetworkManager manager,bool asServer) {manager.Subscribe<ChatMessage>(OnChatMessage, asServer); } // Unsubscribe to ChatMessage events as either the Server, Client, or bothpublicoverridevoidUnsubscribe(NetworkManager manager,bool asServer) {manager.Unsubscribe<ChatMessage>(OnChatMessage, asServer); } // Called when a ChatMessage broadcast is sent from either the Server or a ClientprivatevoidOnChatMessage(PlayerID player,ChatMessage data,bool asServer) { // TODO: Make this work }}
Now that we've subscribed to the events required, we can actually send a ChatMessage to the Server! You can do this however you'd like, for testing, something like this will be more than sufficient for our needs:
Receiving a ChatMessage on the Server, and sending it to all Clients
Now that we are sending messages from the Client, let's update our OnChatMessage function to handle receiving a ChatMessage broadcast on the Server. As mentioned, if we are the Server receiving the broadcast, we want to relay this information and broadcast it back to all of our Clients, and we can do it very simply with NetworkManager.SendToAll<ChatMessage>(ChatMessage):
// Called when a ChatMessage broadcast is sent from either the Server or a ClientprivatevoidOnChatMessage(PlayerID player,ChatMessage data,bool asServer){if (asServer) // The broadcast was sent to the Server from a Client { // Send the broadcast down to the ClientsInstanceHandler.NetworkManager.SendToAll<ChatMessage>(data); }}
Receiving a ChatMessage on the Client
Now that we are receiving messages from the Server, we can use our same OnChatMessage function to handle the data from the Server. For now, let's just debug the message:
// Called when a ChatMessage broadcast is sent from either the Server or a ClientprivatevoidOnChatMessage(PlayerID player,ChatMessage data,bool asServer){if (asServer) // The broadcast was sent to the Server from a Client { // Send the broadcast down to the ClientsInstanceHandler.NetworkManager.SendToAll<ChatMessage>(data); }else// The broadcast was sent to the Clients from the Server {Debug.Log($"Received {data.message} from {data.name}!"); }}
Wrap Up
With what we have, our final script should look like such:
publicstructChatMessage:IPackedAuto{publicstring name;publicstring message;}publicclassChatManager:PurrMonoBehaviour{voidUpdate() {if (Keyboard.current.enterKey.wasPressedThisFrame) {ChatMessage message =newChatMessage { name =InstanceHandler.NetworkManager.localPlayer.ToString(), message ="Hello World!" };InstanceHandler.NetworkManager.SendToServer<ChatMessage>(message); } } // Subscribe to ChatMessage events as either the Server, Client, or bothpublicoverridevoidSubscribe(NetworkManager manager,bool asServer) {manager.Subscribe<ChatMessage>(OnChatMessage, asServer); } // Unsubscribe to ChatMessage events as either the Server, Client, or bothpublicoverridevoidUnsubscribe(NetworkManager manager,bool asServer) {manager.Unsubscribe<ChatMessage>(OnChatMessage, asServer); } // Called when a ChatMessage broadcast is sent from either the Server or a ClientprivatevoidOnChatMessage(PlayerID player,ChatMessage data,bool asServer) {if (asServer) // The broadcast was sent to the Server from a Client { // Send the broadcast down to the ClientsInstanceHandler.NetworkManager.SendToAll<ChatMessage>(data); }else// The broadcast was sent to the Clients from the Server {Debug.Log($"Received {data.message} from {data.name}!"); } }}