/// <summary> /// listens to Server /// </summary> public void RecieveDataFromServer() { new Task(() => { NetworkStream stream = this.client.GetStream(); BinaryReader reader = new BinaryReader(stream); while (!stop) { try { string response = reader.ReadString(); // Wait for response from serve CommunicationProtocol msg = JsonConvert.DeserializeObject <CommunicationProtocol>(response); MsgRecievedFromServer(this, ClientServerArgsParser.Parse(msg)); //Thread.Sleep(1000); // Update information every 1 second } catch (Exception) { ConnectionIsBroken(this, null); CloseClient(); } } CloseClient(); }).Start(); }
/// <summary> /// waits for message from server /// added for synchronic comunication. /// the function also updates everyone via event about the information from server. /// </summary> /// <returns>the message from server</returns> private ServiceInfoEventArgs GetAnswer() { NetworkStream stream = this.client.GetStream(); BinaryReader reader = new BinaryReader(stream); string response = reader.ReadString(); // Wait for response from serve CommunicationProtocol msg = JsonConvert.DeserializeObject <CommunicationProtocol>(response); ServiceInfoEventArgs answer = ClientServerArgsParser.Parse(msg); return(answer); }