- Home /
Question by
jaskile12 · Oct 03, 2017 at 07:50 PM ·
c#unity 5networking
Networking problems on connection
So i'm trying to make a authentication server and when i connect to it, it sends a message to server to test if is working but my weird problem is something it connect and dont give null reference and sometimes it doesnt and idk what may be the problem concretly because UNet docs dont explain much about it. Error that shows when ref is null : NullReferenceException: Object reference not set to an instance of an object Login.RequestSessionData
The client-side part code :
void Awake()
{
//Connect and if yes we say Loading Information if no we say error and if he click esc or something
the game goes out
_client = new NetworkClient();
_client.Connect(ip, port);
_client.RegisterHandler(MsgType.Connect, DebugConnect);
_client.RegisterHandler(MsgType.Disconnect, OnDisconnect);
Debug.Log("My handler id = " + AuthType.ID);
}
public class AccountSession : MessageBase
{
public string username;
public bool login_status;
public string md5_id;
//Temporary class for hold objects of account session json
AccountSession(string usr, bool status, string md5_id)
{
this.username = usr;
this.login_status = status;
this.md5_id = md5_id;
}
//Function to create the json account to be sent for server
public static AccountSession CreateSessionFromJSON(string json)
{
SimpleJSON.JSONNode js = SimpleJSON.JSON.Parse(json);
//since we just have 1 account, we create only 1
AccountSession temp_acc = new AccountSession(
js["username"].Value,
js["logged"].AsBool,
js["id"].Value
);
Debug.Log("Temp acc username for test : " + temp_acc.username);
return temp_acc;
}
}
private void RequestSessionData(string json_data)
{
if (!noticeWin)
{
var acc = AccountSession.CreateSessionFromJSON(json_data);
var status_text = GameObject.Find("Status").GetComponent<Text>();
status_text.text = "Retrievieng account information..";
// _client.RegisterHandler(AuthType.ID, SendAuthData);
//obj = acc;
Debug.Log("The object => " + acc.username);
// Debug.Log("Client id => " + _client.connection.connectionId);
_client.Send(AuthType.ID, acc);
//SendAuthData(acc);
//here we load the information to our local classes which will hold our information
}
}
Server-side:
void Start()
{
RegisterHandlers();
NetworkServer.Listen(ip, auth_port); //set the port to being listened
Debug.Log(string.Format("> Auth Server was been started in IP [{0}] and Port [{1}] !", ip, auth_port));
}
void RegisterHandlers()
{
NetworkServer.RegisterHandler(AuthType.ID, OnAuthentication);
NetworkServer.RegisterHandler(0xf1, OnLogout);
NetworkServer.RegisterHandler(MsgType.Connect, OnAuthConnect);
NetworkServer.RegisterHandler(MsgType.Disconnect, OnDisconnect);
Debug.Log("> Auth Handlers was been already set!");
}
class AccountSession : MessageBase
{
public string username;
public bool login_status;
public string md5_id;
}
void OnAuthentication(NetworkMessage msg)
{
var acc = msg.ReadMessage<AccountSession>();
Debug.Log(string.Format("Object name : {0} , Object logged status : {1} , Object ID: {2}", acc.username, acc.login_status, acc.md5_id));
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612140212im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Commands and ClientRPC are not running when called 0 Answers
Photon wont sync for the masterclient. 1 Answer
Is it possible to Spawn an AssetBundle in HLAPI? 3 Answers
Distribute terrain in zones 3 Answers
Switching weapons in PhotonNetwork 1 Answer