Exemple #1
0
    private void AcceptTCPClient(IAsyncResult ar)
    {
        ThreadUtil.CallInMainThread(() =>
        {
            TcpListener listener = (TcpListener)ar.AsyncState;

            var serverClient = new ServerClient(listener.EndAcceptTcpClient(ar));
            clients.Add(serverClient);

            //send a message to everyone, say someone has connected;
            Broadcast("%NAME", serverClient);
            Broadcast(clients.Count.ToString(), serverClient);

            if (clients.Count == 1)
            {
                Broadcast("%ADDPLAYER|0", clients);
            }
            else if (clients.Count == 2)
            {
                Broadcast("%ADDPLAYER|1", clients);
            }

            Debug.Log("SIZE>>>Client accepted " + clients.Count);

            Debug.LogFormat("main:{0} my:{1}", mainThread.ManagedThreadId, Thread.CurrentThread.ManagedThreadId);

            if (clients.Count == 2)
            {
                WaitingPlayersPopUp.SetActive(false);

                Debug.Log("STARTED, BROADCAST!!!!");

                _gameStarted = true;

                Broadcast("%STARTGAME|0", clients);
            }

            StartListening();
        });
    }