Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

Resolved How do I send messages from the client to the server?

Discussion in 'Netcode for GameObjects' started by TheFlyHawk, Jul 24, 2022.

  1. TheFlyHawk

    TheFlyHawk

    Joined:
    Mar 23, 2016
    Posts:
    58
    Code (CSharp):
    1.         NetworkManager.Singleton.StartServer();
    2.         NetworkManager.Singleton.CustomMessagingManager.RegisterNamedMessageHandler("myMessageName", (senderClientId, reader) =>
    3.          {
    4.              reader.ReadValueSafe(out string message);
    5.              Debug.Log("msg:" + message);
    6.          });
    Code (CSharp):
    1.  
    2. public class Player : NetworkBehaviour
    3. {
    4.  
    5.     public override void OnNetworkSpawn()
    6.     {
    7.         base.OnNetworkSpawn();
    8.         if (IsOwner)
    9.         {
    10.             using FastBufferWriter writer = new FastBufferWriter(256, Unity.Collections.Allocator.Temp);
    11.             writer.WriteValueSafe("My MSG!");
    12.             NetworkManager.Singleton.CustomMessagingManager.SendNamedMessage("myMessageName", OwnerClientId, writer, NetworkDelivery.Reliable); //NetworkDelivery is optional.
    13.         }
    14.     }
    15. }
    16.  
    17.  
    The server does not receive messages like this.
     
  2. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    656
    I did a quick test and this is working for me, are you getting any errors? I think if you're sending a message from client to server the clientid should be zero.
     
  3. TheFlyHawk

    TheFlyHawk

    Joined:
    Mar 23, 2016
    Posts:
    58
    The problem was found and the client id was changed to the ServerClientId to receive the message.
     
    Last edited: Jul 25, 2022