Question by
Severos · Jan 08, 2016 at 11:56 AM ·
networkingconnectionclient-server
Network can't connect after disconnect.
I'm building a small login system for my game, using NetworkMessage to authenticate, client is given 5 sec to authenticate before disconnecting them, however I'm having a small problem, after disconnecting the client I can't seem to be able to connect again (regardless of authentication). my server code:
public class ServerNetworkManager : NetworkManager {
Dictionary<NetworkConnection, bool> DisconnectPool;
void Awake()
{
dontDestroyOnLoad = true;
runInBackground = true;
DisconnectPool = new Dictionary<NetworkConnection, bool> ();
networkAddress = "localhost";
networkPort = 9999;
Network.InitializeSecurity ();
StartServer ();
}
public override void OnStartServer ()
{
NetworkServer.RegisterHandler (SharedNetwork.CustomMeesageTypes.MsgType_AuthenticationReply, OnAuthenticationReply);
base.OnStartServer ();
}
void OnAuthenticationReply(NetworkMessage message){
lock (message.conn) {
SharedNetwork.Msg_AuthenticationState reply = new SharedNetwork.Msg_AuthenticationState ();
try {
SharedNetwork.Msg_AuthenticationReply msg = message.ReadMessage<SharedNetwork.Msg_AuthenticationReply> ();
//Authenticate here.
if (DisconnectPool.ContainsKey(message.conn) && msg.username == "Admin" && msg.password == "Admin"){
reply.ResponseCode = SharedNetwork.AuthenticationState.Authenticated;
DisconnectPool[message.conn] = true;
}
else
reply.ResponseCode = SharedNetwork.AuthenticationState.WrongUsernameOrPassword;
message.conn.Send (reply.Type, reply);
} catch (Exception e) {
Debug.LogError ("Error OnAuthenticationReply: " + e.Message);
reply.ResponseCode = SharedNetwork.AuthenticationState.UnknownError;
message.conn.Send (reply.Type, reply);
}
}
}
public override void OnServerConnect (NetworkConnection conn)
{
DisconnectPool.Add (conn, false);
StartCoroutine ("DisconnectTimer", conn);
conn.Send (SharedNetwork.CustomMeesageTypes.MsgType_AuthenticationRequest, new SharedNetwork.Msg_AuthenticationRequest ());
base.OnClientConnect (conn);
}
IEnumerator DisconnectTimer(object connection){
yield return new WaitForSeconds (5f);
NetworkConnection conn = (NetworkConnection)connection;
lock (conn) {
try{
if (DisconnectPool [conn])
DisconnectPool.Remove (conn);
else {
DisconnectPool.Remove (conn);
conn.Disconnect ();
conn.Dispose ();
}
} catch(Exception e){
Debug.LogError ("Exception DisconnectTimer: " + e.Message);
if (DisconnectPool.ContainsKey (conn))
DisconnectPool.Remove (conn);
conn.Disconnect ();
conn.Dispose ();
}
}
}
}
and my client code:
public class ClientNetworkManager : NetworkManager {
public static class AuthenticationParameters{
public static string UserName{ get; set; }
public static string Password{ get; set; }
}
public override void OnStartClient (NetworkClient client)
{
client.RegisterHandler (SharedNetwork.CustomMeesageTypes.MsgType_AuthenticationRequest, OnAuthenticationRequest);
client.RegisterHandler (SharedNetwork.CustomMeesageTypes.MsgType_AuthenticationState, OnReceiveAuthenticationState);
base.OnStartClient (client);
}
void OnAuthenticationRequest(NetworkMessage message){
SharedNetwork.Msg_AuthenticationReply reply = new SharedNetwork.Msg_AuthenticationReply ();
reply.username = AuthenticationParameters.UserName;
reply.password = AuthenticationParameters.Password;
message.conn.Send (reply.Type, reply);
}
void OnReceiveAuthenticationState(NetworkMessage message){
SharedNetwork.Msg_AuthenticationState msg = message.ReadMessage<SharedNetwork.Msg_AuthenticationState> ();
switch (msg.ResponseCode) {
case SharedNetwork.AuthenticationState.Authenticated:
Debug.Log ("Authenticated!");
return;
case SharedNetwork.AuthenticationState.WrongUsernameOrPassword:
Debug.Log ("Wrong Username or Password");
break;
case SharedNetwork.AuthenticationState.UnknownError:
Debug.Log ("Unknown Error during Authentication");
break;
default:
Debug.LogError ("Unkown AuthenticationState Recieved : " + msg.ResponseCode);
break;
}
message.conn.Disconnect ();
}
public void Button_Login(){
StopClient ();
AuthenticationParameters.UserName = GameObject.Find ("UsernameField").GetComponent<InputField> ().text;
AuthenticationParameters.Password = GameObject.Find ("PasswordField").GetComponent<InputField> ().text;
StartClient ();
}
}
Anyone knows what's wrong with the code??
Comment