Class NetworkMessage
The details of a network message received by a client or server on a network connection.
Namespace: UnityEngine.Networking
Syntax
[Obsolete("The high level API classes are deprecated and will be removed in the future.")]
public class NetworkMessage
Fields
channelId
The transport layer channel the message was sent on.
Declaration
public int channelId
Field Value
Type | Description |
---|---|
Int32 |
conn
The connection the message was recieved on.
Declaration
public NetworkConnection conn
Field Value
Type | Description |
---|---|
NetworkConnection |
MaxMessageSize
The size of the largest message in bytes that can be sent on a NetworkConnection.
Note that channels that are not Fragmented cannot send messages larger than the Maximum Transmission Unity (MTU) size, which is about 1400 bytes by default.
Declaration
public const int MaxMessageSize = 65535
Field Value
Type | Description |
---|---|
Int32 |
msgType
The id of the message type of the message.
Declaration
public short msgType
Field Value
Type | Description |
---|---|
Int16 |
reader
A NetworkReader object that contains the contents of the message.
For some built-in message types with no body, this can be null.
Declaration
public NetworkReader reader
Field Value
Type | Description |
---|---|
NetworkReader |
Methods
Dump(Byte[], Int32)
Returns a string with the numeric representation of each byte in the payload.
Declaration
public static string Dump(byte[] payload, int sz)
Parameters
Type | Name | Description |
---|---|---|
Byte[] | payload | Network message payload to dump. |
Int32 | sz | Length of payload in bytes. |
Returns
Type | Description |
---|---|
String | Dumped info from payload. |
ReadMessage<TMsg>()
ReadMessage is used to extract a typed network message from the NetworkReader of a NetworkMessage object.
For example in a handler for the AddPlayer message:
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.Networking.NetworkSystem;
public class MyManager : NetworkManager
{
void OnServerAddPlayerMessageInternal(NetworkMessage netMsg)
{
var msg = netMsg.ReadMessage<AddPlayerMessage>();
OnServerAddPlayer(netMsg.conn, msg.playerControllerId);
}
}
The AddPlayerMessage that is created will be populated by calling DeSerialize(). So when it is returned form ReadMessage it is ready to use.
Declaration
public TMsg ReadMessage<TMsg>()
where TMsg : MessageBase, new()
Returns
Type | Description |
---|---|
TMsg |
Type Parameters
Name | Description |
---|---|
TMsg | The type of the Network Message, must be derived from MessageBase. |
ReadMessage<TMsg>(TMsg)
Declaration
public void ReadMessage<TMsg>(TMsg msg)
where TMsg : MessageBase
Parameters
Type | Name | Description |
---|---|---|
TMsg | msg |
Type Parameters
Name | Description |
---|---|
TMsg |