Esempio n. 1
0
        /// <summary>
        /// Attempts to connect to the given hostname using the given nickname.
        /// </summary>
        /// <param name="hostname">The server address, excluding the port.</param>
        /// <param name="nickname">The nickname to use for the player connecting.</param>
        private void Connect(string hostname, string nickname)
        {
            // Connect to the server.
            ClientNetworking.ConnectToServer(
                hostname,
                11000,
                state =>
            {
                _socketState = state;

                // Listen for when data is received on the socket.
                _socketState.DataReceived += DataReceived;

                // Listen for when the socket disconnects.
                _socketState.Disconnected += () => { Disconnected?.Invoke(); };

                // Send the nickname of the user.
                AbstractNetworking.Send(state, nickname + '\n');

                // Wait for data.
                AbstractNetworking.GetData(state);
            },
                reason => _connectionFailedCallback(reason)
                );
        }
Esempio n. 2
0
        /// <summary>
        /// This attempts to connect to the server at the ip provided.
        /// </summary>
        /// <param name="server">String ip of the server.</param>
        /// <param name="name">Name to send.</param>
        public void ConnectToServer(String server, String name = "Tarun")
        {
            // This is where we connect to the server for the first time. After the setup is done we
            // want our callback to be FirstContact.
            ClientNetworking.ConnectToServer(server,
                                             state =>
            {
                SuccessfulConnection?.Invoke("Connected to host: " + server);

                _socketState = state;

                // Listen for when data is received on the socket.
                _socketState.DataReceived += DataReceived;

                // Listen for when the socket disconnects.
                _socketState.Disconnected += () => { Disconnected?.Invoke(); };

                // Send the register message with the server.
                Debug.WriteLine(REGISTER, "sending register message");
                AbstractNetworking.Send(state, REGISTER);

                // Wait for data.
                AbstractNetworking.GetData(state);
            },
                                             reason => ErrorCallback?.Invoke(reason));
        }