From f2202e6aa7a2d10707354030ea7685ea4674275a Mon Sep 17 00:00:00 2001 From: Patrick Hellebrand Date: Wed, 22 Sep 2021 13:50:36 +0200 Subject: [PATCH] Added json support with type+content to ChatMessage --- PolyChat/Controller.cs | 1 - PolyChat/Models/ChatMessage.cs | 41 +++++++++++++++++++++++++--------- PolyChat/PolyChat.csproj | 4 ++++ 3 files changed, 34 insertions(+), 12 deletions(-) diff --git a/PolyChat/Controller.cs b/PolyChat/Controller.cs index a26cdbd..d377df8 100644 --- a/PolyChat/Controller.cs +++ b/PolyChat/Controller.cs @@ -59,7 +59,6 @@ namespace PolyChat { IPHostEntry ipEntry = Dns.GetHostEntry(Dns.GetHostName()); IPAddress[] addrList = ipEntry.AddressList; - for (short i = 0; i < addrList.Length; i++) { if (addrList[i].ToString().Substring(0, 3).Equals("10.")) diff --git a/PolyChat/Models/ChatMessage.cs b/PolyChat/Models/ChatMessage.cs index 28cf47c..4c9f48f 100644 --- a/PolyChat/Models/ChatMessage.cs +++ b/PolyChat/Models/ChatMessage.cs @@ -1,29 +1,48 @@ using System; +using System.Diagnostics; +using System.Text.Json; namespace PolyChat.Models { public class ChatMessage { - public readonly string Sender; - public readonly DateTime Timestamp = new DateTime(1970, 01, 01); - public readonly string Content; - public readonly string Ip; + private string Origin; + private string Type; + private string Content; + private DateTime TimeStamp; public readonly bool Foreign; + // + public readonly string Ip; - public ChatMessage(string Content = "", bool Foreign = true, string Sender= "Unknown", string Ip = "127.0.0.1") + public ChatMessage(string content = "", string origin = "Unknown", string ip = "127.0.0.1") { - this.Sender = Sender; - this.Timestamp = DateTime.Now; - this.Content = Content; - this.Foreign = Foreign; - this.Ip = Ip; + Origin = origin; + TimeStamp = DateTime.Now; + Content = content; + Ip = ip; + // no json = my messages + Foreign = false; + Debug.WriteLine("Created Message: " + ToString()); + } + + public ChatMessage(string origin, string json) + { + Origin = origin; + // parse and save to object + var obj = JsonDocument.Parse(json).RootElement; + Type = obj.GetProperty("type").GetString(); + Content = obj.GetProperty("content").GetString(); + TimeStamp = DateTime.Now; + // json = foreign + Foreign = true; + Debug.WriteLine("Created Message: " + ToString()); } override public string ToString() { string prefix = Foreign ? "Other" : "Me"; - return $"{prefix}: {Content}({Sender})"; + return $"{Type} from {prefix}: {Content}({Origin})"; } } } \ No newline at end of file diff --git a/PolyChat/PolyChat.csproj b/PolyChat/PolyChat.csproj index 5b6c905..41da7e3 100644 --- a/PolyChat/PolyChat.csproj +++ b/PolyChat/PolyChat.csproj @@ -124,6 +124,7 @@ MainPage.xaml + @@ -193,6 +194,9 @@ 2.0.3 + + 5.0.2 + 14.0