Exemplo n.º 1
0
        public async Task ConnectToServer(string name)
        {
            while (WaitingForServer)
            {
            }

            WaitingForServer = true;

            var ClientToServer = new TcpClient();
            await ClientToServer.ConnectAsync(ServerIP, Port);

            StreamToServer = ClientToServer.GetStream();
            Writer = new StreamWriter(StreamToServer);
            Reader = new StreamReader(StreamToServer);

            var registrationMessage = new Packet() { Message = name, MessageType = PacketType.Register };

            await Writer.WriteLineAsync(JsonConvert.SerializeObject(registrationMessage));
            await Writer.FlushAsync();

            await ReadServerAnswer();
        }
Exemplo n.º 2
0
        private void ClientHandler(object tcpClient)
        {
            var client = new Client((TcpClient)tcpClient);
            NetworkStream stream = null;
            StreamWriter writer = null; 
            StreamReader reader = null; 

            try
            {
                Application.Current.Dispatcher.Invoke((Action)(() =>
                {
                    ConnectedClients.Add(client);
                    UpdateDefaultStatus();

                }));

                byte[] message = new byte[2048];
                stream = client.PlayerClient.GetStream();
                writer = new StreamWriter(stream);
                reader = new StreamReader(stream);

                while (true)
                {
                    string messageData = reader.ReadLine();

                    var packet = JsonConvert.DeserializeObject<Packet>(messageData);

                    if (!client.IsRegistered && packet.MessageType == PacketType.Register)
                        HandleRegisterMessage(client, packet);

                    DisplayClientMessageInLog(client, packet);

                    var x = new Packet() { MessageType = PacketType.Action, Message = "OK" };

                    writer.WriteLine(JsonConvert.SerializeObject(x));
                    writer.Flush();
                }
            }
            catch(Exception e)
            {

            }
            finally
            {
                if(stream != null)
                    stream.Close();

                Application.Current.Dispatcher.Invoke((Action)(() =>
                {
                    ConnectedClients.Remove(client);
                }));
            }
        }
Exemplo n.º 3
0
 private void DisplayClientMessageInLog(Client client, Packet packet)
 {
     Application.Current.Dispatcher.Invoke((Action)(() =>
     {
         MessageLog.Add(string.Format("{0}: {1}", client.Name, packet.Message));
     }));
 }
Exemplo n.º 4
0
        private void HandleRegisterMessage(Client client, Packet packet)
        {
            client.Name = packet.Message;
            client.IsRegistered = true;

            if (OnClientRegistered != null)
                OnClientRegistered(client);
        }
Exemplo n.º 5
0
        public async void SendTestMessage()
        {
            while (WaitingForServer)
            {
            }

            var packet = new Packet()
            {
                Message = "Test",
                MessageType = PacketType.Action
            };

            byte[] msg = packet.GetPacketAsByte();

            WaitingForServer = true;

            await Writer.WriteLineAsync(JsonConvert.SerializeObject(packet));
            await Writer.FlushAsync();

            await ReadServerAnswer();
        }