コード例 #1
0
    public void ConnectButton()
    {
        m_client = new ClientConnectionManager();
        bool connected = m_client.AttemptConnection(m_ipAddress, m_port);

        if (connected)
        {
            // load new scene
            SceneManager.LoadScene("Scenes/CardGame");
        }
    }
コード例 #2
0
    public bool ConnectLocal(ClientConnectionManager client, int id)
    {
        Debug.Log("Connecting local player " + id);

        object locker = new object();

        // Make server listen for clients on separate thread
        Thread connectToClient = new Thread(() =>
        {
            Thread.CurrentThread.IsBackground = true;
            lock (locker)
            {
                m_connection.ConnectToClient(id);
            }
        });

        connectToClient.Start();

        // Make client attempt to connect
        int attempts = 100;

        while (true)
        {
            if (client.AttemptConnection("127.0.0.1", m_connection.m_port))
            {
                break;
            }
            if (attempts < 0)
            {
                Debug.Log("Failed to connect local player " + id);
                return(false);
            }
            attempts--;
            // Wait a bit before trying again
            Thread.Sleep(10);
        }

        lock (locker)
        {
            return(true);
        }
    }