Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by GunLengend · Apr 11, 2016 at 03:05 PM · networkingmultiplayerclient-server

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);

 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges