- Home /
Confusing in Multiplayer Networking uNet Client-Server
Hello there, I just start with UNET and i really confuse with some question listed below, please help me figure out. I would like to make Client-Server, it's mean no host, no same client server project, it should doing what it does, I separet each server, client to each project. - The problem was come when I think, like a MMO i joined before, Server only for transfer and sync message, game state, i don't know why i must have same scene as Client, like all tutorial, i don't want to use NetworkManager, NetworkManager HUD, so i start a new one. - I have client and server separate, so i don't need to care about isLocalClient right ? Every client now is remote client, so, there is a way to spawn Player on Server and another player can see it without server have same scene like client ? - Why my RPC geting NullReference when calling from server ? - There is any tutorial for client-server without default unity network manager and don't have "local client" on it ? - Am i wrong to follow this to make Online game ? - Currently, i would like to understand how to sync server-client, spawn object like i said above and following code. Error at void RpcChangeMap(string mapName) with NullReference ?! Here is the code :
ZMatchServer [Code] public class ZMatchServer : MonoBehavior {
public int listenPort; public int maxConnection; public bool enableNAT;
public Text currentPlayer;
public Text currentLog;
public Text currentMsg;
private int onlinePlayer;
void Awake()
{
DontDestroyOnLoad(this);
}
void Start ()
{
NetworkServer.Listen(listenPort);
NetworkServer.RegisterHandler(MsgType.Connect,OnClientConnected);
NetworkServer.RegisterHandler(MsgType.Disconnect,OnClientDisconnected);
NetworkServer.RegisterHandler(MessageType.Login,OnPlayerLogin);
NetworkServer.RegisterHandler(MessageType.SelectCharacter,OnSelectCharacter);
}
void OnServerInitialized()
{
Debug.Log("Server Initialize completed! ");
currentLog.text = "Server Initialize completed! ";
}
void OnPlayerLogin(NetworkMessage netMsg)
{
//Read username and password from client
var loginMsg = netMsg.ReadMessage<LoginMessage>();
string username = loginMsg.username;
string password = loginMsg.password;
//Send usm and pwd to check query in database
ClientLoginCallback(username,password, netMsg.conn.connectionId);
currentMsg.text = "Player send login " + username + " " + password;
}
void ClientLoginCallback(string username, string password, int connectionId)
{
if(username == "Bao" && password == "123")
{
LoginCallbackMessage loginClbMsg = new LoginCallbackMessage();
loginClbMsg.authCode = LoginCallbackMessage.AuthCode.Sucessfull;
NetworkServer.SendToClient(connectionId,MessageType.LoginAuthCode,loginClbMsg);
}
else if (username == "NotBao" && password == "321")
{
LoginCallbackMessage loginClbMsg = new LoginCallbackMessage();
loginClbMsg.authCode = LoginCallbackMessage.AuthCode.Sucessfull;
NetworkServer.SendToClient(connectionId,MessageType.LoginAuthCode,loginClbMsg);
}
}
void OnSelectCharacter(NetworkMessage netMsg)
{
var selectCharMsg = netMsg.ReadMessage<SelectCharacterMessage>();
string characterName = selectCharMsg.characterName;
string characterId = selectCharMsg.characterID;
RpcChangeMap("Shipping Ship");
RpcSpawnCharacter(characterName,characterId);
}
[ClientRpc]
void RpcSpawnCharacter(string charNam, string charID)
{
Vector3 position = new Vector3(0,10,0);
Instantiate(ZGlobalAsset.Instance.Archer,position,ZGlobalAsset.Instance.Archer.transform.rotation);
}
[ClientRpc]
void RpcChangeMap(string mapName)
{
Application.LoadLevelAsync(mapName);
}
void OnClientConnected(NetworkMessage netMsg)
{
onlinePlayer +=1;
currentPlayer.text = onlinePlayer.ToString();
currentLog.text = "Player connected with connection ID " + netMsg.conn.connectionId;
}
void OnClientDisconnected(NetworkMessage netMsg)
{
if(onlinePlayer > 0)
{
onlinePlayer -= 1;
currentPlayer.text = onlinePlayer.ToString();
currentLog.text = "Player disconnected";
}
}
}
ZMatchClient
public class ZMatchClient : MonoBehaviour { [SerializeField]private string serverURL; [SerializeField]private string serverIP; [SerializeField]private int serverPort;
public bool isConnected;
#region test login
public string loginID;
public string loginPwd;
private NetworkIdentity mainCharNetId;
#endregion
NetworkClient m_Client;
private static ZMatchClient instance;
public static ZMatchClient Instance { get { return instance; } }
void Awake()
{
instance = this;
DontDestroyOnLoad(this);
}
void Start ()
{
ConnectToServer();
}
protected void ConnectToServer()
{
m_Client = new NetworkClient();
m_Client.Connect(serverIP,serverPort);
m_Client.RegisterHandler(MsgType.Connect, ClientConnect);
m_Client.RegisterHandler(MsgType.Disconnect, ClientDisconnect);
m_Client.RegisterHandler(MessageType.LoginAuthCode, LoginAuthCodeCallback);
}
//Login message
protected void LoginToServer(string username, string password)
{
LoginMessage loginMsg = new LoginMessage();
loginMsg.username = username;
loginMsg.password = password;
m_Client.Send(MessageType.Login,loginMsg);
}
private void LoginAuthCodeCallback(NetworkMessage netMsg)
{
var loginClbMsg = netMsg.ReadMessage<LoginCallbackMessage>();
var authCode = loginClbMsg.authCode;
switch(authCode)
{
case LoginCallbackMessage.AuthCode.Sucessfull :
Debug.Log("Login success");
isConnected = true;
SelectCharacter("Lancer","100");
break;
case LoginCallbackMessage.AuthCode.WrongUsername:
Debug.Log("Wrong username");
break;
case LoginCallbackMessage.AuthCode.WrongPassword:
Debug.Log("Wrong password");
break;
case LoginCallbackMessage.AuthCode.UnknowError :
Debug.Log("Unknown error");
break;
}
}
#region Test client sync
protected void SelectCharacter(string charName, string charID)
{
SelectCharacterMessage selectCharMsg = new SelectCharacterMessage();
selectCharMsg.characterName = charName;
selectCharMsg.characterID = charID;
m_Client.Send(MessageType.SelectCharacter,selectCharMsg);
}
#endregion
//Connected callback
void ClientConnect(NetworkMessage netMsg)
{
Debug.Log("Client connected " + netMsg.msgType.ToString() + " " + netMsg.conn.connectionId);
LoginToServer(loginID,loginPwd);
}
//Disconnected callback
void ClientDisconnect(NetworkMessage netMsg)
{
Debug.Log("ClientDisconnect " + netMsg.msgType.ToString() + " " + netMsg.conn.connectionId);
}
}
Your answer
Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
UNet | I'm having trouble assigning authority to game objects 0 Answers
Can MLAPI connect to a standalone server written in another language (Go) 0 Answers
Network command is running on client? 1 Answer
Can I create a separate server and use the Unity game objects in it? 0 Answers