mirror of
https://github.com/MirrorNetworking/Mirror.git
synced 2024-11-18 11:00:32 +00:00
Updated Network Messages doc
This commit is contained in:
parent
84fd999433
commit
bb7602e00b
@ -1,12 +1,12 @@
|
||||
# Network Messages
|
||||
|
||||
For the most part we recommend the high level [Commands and RPC](RemoteActions.md) calls and [SyncVar](../StateSync.md), but you can also send low level network messages. This can be useful if you want clients to send messages that are not tied to gameobjects, such as logging, analytics or profiling information.
|
||||
For the most part we recommend the high level [Commands and RPC](RemoteActions.md) calls and [SyncVar](../StateSync.md), but you can also send low level network messages. This can be useful if you want clients to send messages that are not tied to game objects, such as logging, analytics or profiling information.
|
||||
|
||||
There is a class called MessageBase that you can extend to make serializable network message classes. This class has Serialize and Deserialize functions that take writer and reader objects. You can implement these functions yourself, but we recommend you let Mirror generate them for you.
|
||||
|
||||
The base class looks like this:
|
||||
|
||||
```cs
|
||||
``` cs
|
||||
public abstract class MessageBase
|
||||
{
|
||||
// Deserialize the contents of the reader into this message
|
||||
@ -23,14 +23,12 @@ To send a message, use the `Send()` method on the NetworkClient, NetworkServer,
|
||||
|
||||
To declare a custom network message class and use it:
|
||||
|
||||
```cs
|
||||
``` cs
|
||||
using UnityEngine;
|
||||
using Mirror;
|
||||
|
||||
public class Scores : MonoBehaviour
|
||||
{
|
||||
NetworkClient myClient;
|
||||
|
||||
public class ScoreMessage : MessageBase
|
||||
{
|
||||
public int score;
|
||||
@ -40,25 +38,23 @@ public class Scores : MonoBehaviour
|
||||
|
||||
public void SendScore(int score, Vector3 scorePos, int lives)
|
||||
{
|
||||
ScoreMessage msg = new ScoreMessage()
|
||||
ScoreMessage msg = new ScoreMessage()
|
||||
{
|
||||
score = score,
|
||||
scorePos = scorePos,
|
||||
lives = lives
|
||||
};
|
||||
|
||||
|
||||
NetworkServer.SendToAll(msg);
|
||||
}
|
||||
|
||||
// Create a client and connect to the server port
|
||||
public void SetupClient()
|
||||
{
|
||||
myClient = new NetworkClient();
|
||||
myClient.RegisterHandler<ScoreMessage>(OnScore);
|
||||
myClient.Connect("127.0.0.1", 4444);
|
||||
NetworkClient.RegisterHandler<ScoreMessage>(OnScore);
|
||||
NetworkClient.Connect("localhost");
|
||||
}
|
||||
|
||||
public void OnScore(ScoreMessage msg)
|
||||
public void OnScore(NetworkConnection conn, ScoreMessage msg)
|
||||
{
|
||||
Debug.Log("OnScoreMessage " + msg.score);
|
||||
}
|
||||
@ -66,27 +62,3 @@ public class Scores : MonoBehaviour
|
||||
```
|
||||
|
||||
Note that there is no serialization code for the `ScoreMessage` class in this source code example. The body of the serialization functions is automatically generated for this class by Mirror.
|
||||
|
||||
## ErrorMessage Class
|
||||
|
||||
There is also an ErrorMessage class that is derived from `MessageBase`. This class is passed to error callbacks on clients and servers.
|
||||
|
||||
The errorCode in the ErrorMessage class corresponds to the Networking.NetworkError enumeration.
|
||||
|
||||
```cs
|
||||
class MyClient
|
||||
{
|
||||
NetworkClient client;
|
||||
|
||||
void Start()
|
||||
{
|
||||
client = new NetworkClient();
|
||||
client.RegisterHandler<ErrorMessage>(OnError);
|
||||
}
|
||||
|
||||
void OnError(ErrorMessage errorMsg)
|
||||
{
|
||||
Debug.Log("Error:" + errorMsg.errorCode);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
Loading…
Reference in New Issue
Block a user