Esempio n. 1
0
        private void Listener()
        {
            //GetData dataDelegate = HandleData;

            while (true)
            {
                Console.WriteLine("Waiting for connection");
                TcpClient lClient = listener.AcceptTcpClient();
                Console.WriteLine("Incoming connection from {0}", ((IPEndPoint)lClient.Client.RemoteEndPoint).Address.ToString());
                connectedClients.Add(((IPEndPoint)lClient.Client.RemoteEndPoint).Address.ToString());
                NetworkStream stream = lClient.GetStream();
                byte[]        bytes  = new byte[256];
                string        data   = null;
                int           i;

                while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                {
                    data = Encoding.ASCII.GetString(bytes, 0, i);

                    ReceivedDataEventArgs e = new ReceivedDataEventArgs();
                    e.Data = data;

                    ReceivedData?.Invoke(this, e); // that's a neat shortcut tbh

                    //dataDelegate(data);
                }
                stream.Close();
                lClient.Close();
                Console.WriteLine("Connection closed");
            }
        }
Esempio n. 2
0
        static void HandleData(object s, ReceivedDataEventArgs e)
        {
            Player data = JsonConvert.DeserializeObject <Player>(e.Data);

            Console.WriteLine(data.HealthPoints);
            data.TakeDamage(20);
            Console.WriteLine(data.HealthPoints);
        }