It seams that my pc is to fast and i only had to add wait for 1 second and the code is fixed:
public class LoginServerSocket : Singleton
{
private int id = 0;
private NetworkClient network;
private float time;
private Dictionary<int, NetworkInterface> handlers = new Dictionary<int, NetworkInterface>();
public bool isConnected
{
get { return network == null ? false : network.isConnected; }
}
private bool isintitiaited = false;
private void Awake()
{
time = Time.time;
InvokeRepeating(“checkNetwork”, 0.0f, 5.0f);
initHandlers();
}
public void checkNetwork()
{
NetworkClient tmp = getConnection();
if ((object)tmp != null && tmp.isConnected)
{
EnterGameRequest egr = new EnterGameRequest();
egr.shipId = -1;
tmp.Send(NetworkConstants.CMD_HEART_BEAT, egr);
}
float diff = Time.time - time;
Debug.Log(“LAST PACKET:Seconds ago?:” + diff);
if (diff > 60) //not communicating for more then 60 seconds
{
StopAllCoroutines();
coRoutineStarted = false;
connectToServer();
}
}
private bool initHandlers()
{
if (!isintitiaited)
{
registerHandeler(NetworkConstants.login, gameObject.AddComponent<CMD_LOGIN>());
registerHandeler(NetworkConstants.standardResponse, gameObject.AddComponent<CMD_ERROR>());
registerHandeler(NetworkConstants.CMD_EXIT_CHARSELECTION, gameObject.AddComponent<CMD_EXIT_TO_CHAR_SCREEN>());
registerHandeler(NetworkConstants.CMD_CREATE_PLAYER, gameObject.AddComponent<CMD_CREATE_CHARACTER>());
registerHandeler(NetworkConstants.CMD_DEL_PLAYER, gameObject.AddComponent<CMD_DELETE_CHARACTER>());
registerHandeler(NetworkConstants.CMD_MATCH_INFO, gameObject.AddComponent<CMD_MATCH_INFO>());
registerHandeler(NetworkConstants.CMD_MATCH_SEARCH_UPDATE, gameObject.AddComponent<CMD_MATCH_RESPONSE>());
isintitiaited = true;
return isintitiaited;
}
return true;
}
private void registerHandeler(int id, NetworkInterface handler)
{
if (!handlers.ContainsKey(id))
{
handlers[id] = handler;
}
}
public void OnDisconnect(NetworkMessage netMsg)
{
GloabalErrorNotification.Instance.showDisconnect(“disconnected from server\nplase wait!\nReconnecting…”, “Error”);
StopAllCoroutines();
connectToServer();
}
private void doGameXX(NetworkMessage netMsg)
{
try
{
switch (SharedVars.gameState)
{
case Engine.Enums.GameState.INGAME:
case Engine.Enums.GameState.LOADING_SCREEN_TO_INGAME:
case Engine.Enums.GameState.CREATE_SCREEN:
case Engine.Enums.GameState.CHARACTER_SCREEN:
PlayerInfo player = PlayerInfo.Instance;
Login log = new Login();
log.user = player.userName;
log.pass = player.userPassword;
log.isRecoonect = true;
log.screen = (int)SharedVars.gameState;
network.Send(NetworkConstants.login, log);
break;
default:
MenuSwitch.Instance.Display_LoginMenu();
GloabalErrorNotification.Instance.closeDisconnect();
break;
}
}
catch
{
}
}
private void OnConnected(NetworkMessage netMsg)
{
id = network.connection.hostId;
StopAllCoroutines();
coRoutineStarted = false;
time = Time.time;
PrintConnectionInfo();
}
private void networkRecive(NetworkMessage netMsg)
{
int id = netMsg.msgType;
if (handlers.ContainsKey(id))
{
NetworkInterface ni = handlers[id];
if (Config.isDebug)
{
Type objectType = ni.GetType();
LogParser.Debug(objectType.Name.ToString() + " RECIVES:" + netMsg.reader.Length + " bits.");
}
ni.Execute(netMsg);
}
}
public NetworkClient getConnection()
{
if ((object)network != null && network.isConnected)
{
return network;
}
else
{
connectToServer();
return network;
}
}
private bool coRoutineStarted = false;
public void connectToServer()
{
if (!coRoutineStarted)
StartCoroutine(connectToS());
}
IEnumerator connectToS()
{
coRoutineStarted = true;
if ((object)network != null) // if comment this block, old client will be used to call Connect again
{
foreach (KeyValuePair<int, NetworkInterface> that in handlers)
{
network.UnregisterHandler((short)that.Key);
}
network.Disconnect();
network.Shutdown();
network = null;
yield return new WaitForSeconds(1);
}
if ((object)network == null)
{
network = new NetworkClient();
network.RegisterHandler(MsgType.Connect, OnConnected);
network.RegisterHandler(MsgType.Disconnect, OnDisconnect);
network.RegisterHandler(MsgType.Error, onError);
network.RegisterHandler(NetworkConstants.CMD_HEART_BEAT, heartbeat);
network.RegisterHandler(NetworkConstants.CMD_SERVER_READY, doGameXX);
foreach (KeyValuePair<int, NetworkInterface> that in handlers)
{
network.RegisterHandler((short)that.Key, networkRecive);
}
network.Connect(SharedVars.ip, 7777);
}
else
{
network.Connect(SharedVars.ip, 7777);
}
yield return new WaitForSeconds(5);
coRoutineStarted = false;
if ((object)network != null && !network.isConnected)
StartCoroutine(connectToS());
yield return true;
}
private void heartbeat(NetworkMessage netMsg)
{
time = Time.time;
}
private void PrintConnectionInfo()
{
List connections = new List(NetworkClient.allClients);
string message = "Connections: " + connections.Count + “\n[”;
for (int index = 0; index < connections.Count; index++)
{
NetworkConnection conn = connections[index].connection;
if (conn != null)
{
message += conn.connectionId;
if (index != connections.Count - 1)
{
message += ", ";
}
else
{
message += “]”;
}
}
else
{
message += “null”;
if (index != connections.Count - 1)
{
message += ", ";
}
else
{
message += “]”;
}
}
}
Debug.Log(message);
}
private void onError(NetworkMessage netMsg)
{
ErrorMessage errorMsg = netMsg.ReadMessage();
Debug.Log(errorMsg.errorCode);
}
}