Esempio n. 1
0
 private Task AcceptServerFirstMessageWork()
 {
     return(Task.Run(() =>
     {
         ServerFirstMessage firstMessage = chatClient.ServerFirstMessage;
         bool serverMessageIsOk = false;
         while (!serverMessageIsOk)
         {
             Thread.Sleep(500);
             firstMessage = chatClient.ServerFirstMessage;
             if (firstMessage != null)
             {
                 serverMessageIsOk = true;
                 if (firstMessage.Master != null && firstMessage.Master.NickName != "")
                 {
                     Dispatcher.Invoke(() => masterPlayerName.Text = firstMessage.Master.NickName);
                 }
             }
         }
         foreach (var i in firstMessage.CharacterInRoom)
         {
             Dispatcher.Invoke(() => playersListBox.Items.Add("Id: " + i.Id + " Имя: " + i.FullName));
         }
     }));
 }
Esempio n. 2
0
        public void SendServerFirstMessage(ClientObject client)
        {
            ServerFirstMessage message = new ServerFirstMessage();

            for (int i = 0; i < Clients.Count; i++)
            {
                message.Players.Add(Clients[i].Player);
            }

            string serialized = JsonConvert.SerializeObject(message);

            byte[] data = Encoding.Unicode.GetBytes(serialized);
            client.Stream.Write(data, 0, data.Length);
        }
Esempio n. 3
0
        private Task AcceptServerFirstMessageWork()
        {
            return(Task.Run(() =>
            {
                byte[] data = new byte[DATA_LENGTH]; // буфер для получаемых данных
                StringBuilder builder = new StringBuilder();
                int bytes = 0;
                do
                {
                    bytes = Stream.Read(data, 0, data.Length);
                    builder.Append(Encoding.Unicode.GetString(data, 0, bytes));
                } while (Stream.DataAvailable);

                string serialized = builder.ToString();
                ServerFirstMessage = JsonConvert.DeserializeObject <ServerFirstMessage>(serialized);
            }));
        }