1
\$\begingroup\$

I'm try to implement client-server communication base on UNET. The problem i got now is send custom message to server, but the Client.Send always show error without my understanding. So what i'm wrong? I jus try to implement from scratch, not inherit from NetworkManager. Here is the code:

using UnityEngine;
using System.Collections;
using UnityEngine.Networking;

public class ZMessageTransfer : NetworkBehaviour
{   
    private static ZMessageTransfer instance;
    public static ZMessageTransfer Instance { get { return instance; } }
    NetworkClient m_Client;
    public class MessageType
    {
        public static short Login = 1001;
    };
    public class LoginMessage : MessageBase
    {
        public string username;
        public string password;
    };
    void Awake ()
    {
        instance = this;
    }       
    public void SendLoginMessage(string username, string password)
    {
        LoginMessage loginMsg = new LoginMessage();
        loginMsg.username = username;
        loginMsg.password = password;
        m_Client.Send(MessageType.Login,loginMsg);
    }
}

Server:

using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
using UnityEngine.UI;

public class ZMatchServer : NetworkBehaviour 
{
    public int listenPort;
    public int maxConnection;
    public bool enableNAT;

    public Text currentPlayer;
    public Text currentLog;

    private int onlinePlayer;

    void Start () 
    {
        Network.InitializeSecurity();
        Network.InitializeServer(maxConnection,listenPort,enableNAT);
        NetworkServer.RegisterHandler(ZMessageTransfer.MessageType.Login,OnPlayerLogin);
    }

    void OnPlayerLogin(NetworkMessage netMsg)
    {
        var loginMsg = netMsg.ReadMessage<ZMessageTransfer.LoginMessage>();
        string username = loginMsg.username;
        string password = loginMsg.password;
        currentLog.text = "Player send login " + username + " " + password;
    }

    void OnServerInitialized()
    {
        Debug.Log("Server Initialize completed! ");
        currentLog.text = "Server Initialize completed! ";
    }

    void OnPlayerConnected(NetworkPlayer player)
    {
        onlinePlayer += 1;
        currentPlayer.text = "Current player online : " + onlinePlayer.ToString();
        currentLog.text = "Player connected";
        Debug.Log("Player connected " );
    }

    void OnPlayerDisconnected(NetworkPlayer player)
    {
        onlinePlayer -= 1;
        currentPlayer.text = "Current player online : " + onlinePlayer.ToString();
        currentLog.text = "Player disconnected";
    }
}

Client:

using UnityEngine;
using System.Net;
using System.Collections;
using UnityEngine.Networking;
using UnityEngine.Networking.Types;

public class ZNetworkConnection : NetworkBehaviour 
{
    [SerializeField]private string serverURL;
    [SerializeField]private string serverIP;
    [SerializeField]private int serverPort;

    NetworkClient m_Client;

    private static ZNetworkConnection instance;

    public static ZNetworkConnection Instance { get { return instance; } }

    void Awake()
    {
        instance = this;
    }

    void Start () 
    {
        ConnectToServer();
    }
    protected void ConnectToServer()
    {
        Network.Connect(serverIP,serverPort);
    }

    public void LoginToServer(string username,string password)
    {
        ZMessageTransfer.Instance.SendLoginMessage(username,password);
    }

    protected void OnConnectedToServer()
    {
        Debug.Log("Connected to server complete ");
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.