Unet Problem: Unknown Message ID error

I know it’s deprecated, but I’m still using it. I don’t understand why I keep getting unknown message error. It seems to me that I am registering the message id numbers in my protocol class. If you can point out my error, I would appreciate it. This is for a dedicated login server. I’ve seen a few other posts like this in the past, but I’m afraid I can’t identify my mistake. Here is the code:

Protocol:

public class Protocols : NetworkBehaviour
{
    public class CharDataNetworkProtocol
    {
        public static readonly short RequestCreateAccount = 401;
        public static readonly short ResponseCreateAccount = 403;
    }
       
    public class RequestCreateAccountMessage : MessageBase
    {
        public string Account_Name;
        public string email;
        public string password;

    }

    public class ResponseCreateAccountMessage : MessageBase
    {
        public bool success;
        public string error;
    }
}

Client Side:

public class ClientSide
{
    NetworkClient handlerclient = new NetworkClient();

    protected override void RegisterNetworkHandlers()
    {
        handlerclient.RegisterHandler(CharDataNetworkProtocol.ResponseCreateAccount, OnResponseCreateAccount);
    }

    protected override void UnregisterNetworkHandlers()
    {
        handlerclient.UnregisterHandler(CharDataNetworkProtocol.ResponseCreateAccount);
    }

    private void OnEnable()
    {
        RegisterNetworkHandlers();
    }

    private void OnDisable()
    {
        UnregisterNetworkHandlers();
    }

    public void Send_Create_Account(string email, string acct_name, string pswd)
    {
        var msg = new RequestCreateAccountMessage();
        msg.Account_Name = acct_name;
        msg.email = email;
        msg.password = pswd;
        clientApi.masterServerClient.client.Send(CharDataNetworkProtocol.RequestCreateAccount, msg);
    }
}

Server Side:

public class ServerSide
{

    protected override void RegisterNetworkHandlers()
    {
        NetworkServer.RegisterHandler(CharDataNetworkProtocol.RequestCreateAccount, OnRequestCreateAccount);
    }

    protected override void UnregisterNetworkHandlers()
    {
        NetworkServer.UnregisterHandler(CharDataNetworkProtocol.RequestCreateAccount);
    }

    public override void OnEnable()
    {
        RegisterNetworkHandlers();
    }

    public override void OnDisable()
    {
        UnregisterNetworkHandlers();
    }

    protected virtual void OnRequestCreateAccount(NetworkMessage netMsg)
    {
        var msg = netMsg.ReadMessage<RequestCreateAccountMessage>();
        if (msg != null)
        {
            StartCoroutine(npgsql_db.Register_New_Account(msg.email, msg.Account_Name, msg.password, token =>
            {
                var responseMsg = new ResponseCreateAccountMessage();
                responseMsg.success = true;
                netMsg.conn.Send(CharDataNetworkProtocol.ResponseCreateAccount, responseMsg);
            },
            error =>
            {
                var responseMsg = new ResponseCreateAccountMessage();
                responseMsg.success = false;
                responseMsg.error = error;
                netMsg.conn.Send(CharDataNetworkProtocol.ResponseCreateAccount, responseMsg);
            }));
        }
    }
}

Just to be clear, I’m calling Send_Create_Account(401, msg) from the client, and then the server does not recognize the protocol number 401 in OnRequestCreateAccount, even though I have registered it in OnEnable. The only thing I can think is that Send_Create_Account is not registered in a handler. However, a handler delegate has to be passed a NetworkMessage variable, but Send_Create_Account is obviously not receiving anything, so I’m a little confused.

You need to actually call RegisterNetworkHandlers() on the server. If you’re calling it somewhere else, make sure you are also not calling UnregisterNetworkHandlers().

Probably not important, but why is the Protocols class a NetworkBehaviour?

It didn’t occur to me that it wasn’t being called until you said that. I just realized OnEnable is a Monobehaviour, so I guess it’s not being called at all… I’ll need to think of a way to call it without the usual Monobehaviour functions like Start, OnEnable, etc. Thanks!

I thought I saw that it was required in the documentation. I guess I misunderstood. I’ll change it.

Edit: Actually, the real Serverside class inherits from another class which inherits from Monobehaviour, so that’s not it, unfortunately. I’m at my wits end here.

But the ServerSide class isn’t a MonoBehaviour, so OnEnable isn’t getting called automatically. You’d have to call OnEnable manually or make ServerSide a MonoBehaviour and attach it to a GameObject. Add a Debug.Log statement to RegisterNetworkHandlers() in the ServerSide script and verify it is being called.

I meant the real one really does inherit. It just doesn’t say so in what I posted, due to my effort to simplify the problem. I left out a key bit of information there. In any case, you helped me figure it out, because it turns out that for another reason entirely which is not clear from the code shown, Registerhandler was not getting called. Thanks! This was driving me nuts.

1 Like