网络消息

除了高级命令和RPC调用功能外,还可以发送原始网络消息。

有一个名为MessageBase的类可以扩展为可串行化的网络消息类。该类具有序列化和反序列化功能,可以使用Writer和Reader对象。开发人员可以自己实现这些功能,也可以依赖由网络系统自动创建的代码生成的实现。基类看起来像这样:

  1. public abstract class MessageBase
  2. {
  3. // De-serialize the contents of the reader into this message
  4. public virtual void Deserialize(NetworkReader reader) {}
  5. // Serialize the contents of this message into the writer
  6. public virtual void Serialize(NetworkWriter writer) {}
  7. }

消息类可以包含基本类型,结构体,数组和大多数常见Unity Engine类型(如Vector3)的成员。他们不能包含复杂类或泛型容器的成员。

有一些常用类型的网络消息的内置消息类别:

  1. EmptyMessage
  2. StringMessage
  3. IntegerMessage

要发送消息,NetworkClientNetworkServerNetworkConnection类上都有Send()函数,它们的工作方式相同。他们接收消息ID和从MessageBase派生的消息对象。下面的代码展示了如何使用其中一个内置的消息类来发送和处理消息:

  1. using UnityEngine;
  2. using UnityEngine.Networking;
  3. using UnityEngine.Networking.NetworkSystem;
  4. public class Begin : NetworkBehaviour
  5. {
  6. const short MyBeginMsg = 1002;
  7. NetworkClient m_client;
  8. public void SendReadyToBeginMessage(int myId)
  9. {
  10. var msg = new IntegerMessage(myId);
  11. m_client.Send(MyBeginMsg, msg);
  12. }
  13. public void Init(NetworkClient client)
  14. {
  15. m_client = client;
  16. NetworkServer.RegisterHandler(MyBeginMsg, OnServerReadyToBeginMessage);
  17. }
  18. void OnServerReadyToBeginMessage(NetworkMessage netMsg)
  19. {
  20. var beginMessage = netMsg.ReadMessage<IntegerMessage>();
  21. Debug.Log("received OnServerReadyToBeginMessage " + beginMessage.value);
  22. }
  23. }

声明自定义网络消息类并使用它:

  1. using UnityEngine;
  2. using UnityEngine.Networking;
  3. public class Scores : MonoBehaviour
  4. {
  5. NetworkClient myClient;
  6. public class MyMsgType {
  7. public static short Score = MsgType.Highest + 1;
  8. }
  9. public class ScoreMessage : MessageBase
  10. {
  11. public int score;
  12. public Vector3 scorePos;
  13. public int lives;
  14. }
  15. public void SendScore(int score, Vector3 scorePos, int lives)
  16. {
  17. ScoreMessage msg = new ScoreMessage();
  18. msg.score = score;
  19. msg.scorePos = scorePos;
  20. msg.lives = lives;
  21. NetworkServer.SendToAll(MyMsgType.Score, msg);
  22. }
  23. // Create a client and connect to the server port
  24. public void SetupClient()
  25. {
  26. myClient = new NetworkClient();
  27. myClient.RegisterHandler(MsgType.Connect, OnConnected);
  28. myClient.RegisterHandler(MyMsgType.Score, OnScore);
  29. myClient.Connect("127.0.0.1", 4444);
  30. }
  31. public void OnScore(NetworkMessage netMsg)
  32. {
  33. ScoreMessage msg = netMsg.ReadMessage<ScoreMessage>();
  34. Debug.Log("OnScoreMessage " + msg.score);
  35. }
  36. public void OnConnected(NetworkMessage netMsg)
  37. {
  38. Debug.Log("Connected to server");
  39. }
  40. }

请注意,例如,此源代码中没有ScoreMessage类的序列化代码。序列化函数的主体是为这个类自动生成的。

错误消息类

Thre也是一个派生自MessageBase的ErrorMessage类。该类传递给客户端和服务器上的错误回调。

ErrorMessage类中的errorCode对应于Networking.NetworkError枚举。

  1. class MyClient
  2. {
  3. NetworkClient client;
  4. void Start()
  5. {
  6. client = new NetworkClient();
  7. client.RegisterHandler(MsgType.Error, OnError);
  8. }
  9. void OnError(NetworkMessage netMsg)
  10. {
  11. var errorMsg = netMsg.ReadMessage<ErrorMessage>();
  12. Debug.Log("Error:" + errorMsg.errorCode);
  13. }
  14. }

?