custom messages can be sent by anyone on server
parent
8148843a6c
commit
4a6e271051
|
|
@ -469,7 +469,8 @@ namespace VelNet
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Debug.LogError("Received message from player that doesn't exist ");
|
LocalPlayer?.HandleMessage(dm, true);
|
||||||
|
//Debug.LogError("Received message from player that doesn't exist ");
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
|
||||||
|
|
@ -68,7 +68,7 @@ namespace VelNet
|
||||||
///
|
///
|
||||||
/// The length of the byte[] for message is fixed according to the message type
|
/// The length of the byte[] for message is fixed according to the message type
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void HandleMessage(VelNetManager.DataMessage m)
|
public void HandleMessage(VelNetManager.DataMessage m, bool unknown_sender = false)
|
||||||
{
|
{
|
||||||
using MemoryStream mem = new MemoryStream(m.data);
|
using MemoryStream mem = new MemoryStream(m.data);
|
||||||
using BinaryReader reader = new BinaryReader(mem);
|
using BinaryReader reader = new BinaryReader(mem);
|
||||||
|
|
@ -76,6 +76,30 @@ namespace VelNet
|
||||||
//individual message parameters separated by comma
|
//individual message parameters separated by comma
|
||||||
VelNetManager.MessageType messageType = (VelNetManager.MessageType)reader.ReadByte();
|
VelNetManager.MessageType messageType = (VelNetManager.MessageType)reader.ReadByte();
|
||||||
|
|
||||||
|
if(messageType == VelNetManager.MessageType.Custom)
|
||||||
|
{
|
||||||
|
// Custom packets. These are global data that can be sent from anywhere.
|
||||||
|
// Any script can subscribe to the callback to receive the message data.
|
||||||
|
// todo: strange hack that any player can handle the custom message, which then simply calls velnetmanager.
|
||||||
|
|
||||||
|
int len = reader.ReadInt32();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
VelNetManager.CustomMessageReceived?.Invoke(m.senderId, reader.ReadBytes(len));
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Debug.LogError(e);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (unknown_sender)
|
||||||
|
{
|
||||||
|
Debug.LogError("Received non-custom message from player that doesn't exist ");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
switch (messageType)
|
switch (messageType)
|
||||||
{
|
{
|
||||||
// sync update for an object "I" may own
|
// sync update for an object "I" may own
|
||||||
|
|
@ -147,22 +171,7 @@ namespace VelNet
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// Custom packets. These are global data that can be sent from anywhere.
|
|
||||||
// Any script can subscribe to the callback to receive the message data.
|
|
||||||
case VelNetManager.MessageType.Custom:
|
|
||||||
{
|
|
||||||
int len = reader.ReadInt32();
|
|
||||||
try
|
|
||||||
{
|
|
||||||
VelNetManager.CustomMessageReceived?.Invoke(m.senderId, reader.ReadBytes(len));
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
Debug.LogError(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
default:
|
default:
|
||||||
throw new ArgumentOutOfRangeException();
|
throw new ArgumentOutOfRangeException();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue