diff --git a/PolyChat/Controller.cs b/PolyChat/Controller.cs index 4d3959a..2792c66 100644 --- a/PolyChat/Controller.cs +++ b/PolyChat/Controller.cs @@ -78,6 +78,15 @@ namespace PolyChat }); } + public void SendBroadcastMessage(string type, string content) + { + Debug.WriteLine("--- Controller.Broadcast ---"); + foreach (KeyValuePair entry in Connections) + { + SendMessage(entry.Key, type, content); + } + } + public void SendMessage(string ip, string type, string content) { Debug.WriteLine("--- Controller.SendMessage ---"); @@ -109,7 +118,7 @@ namespace PolyChat Connections[IP].Close(); Connections.Remove(IP); } - CloseChatUI(IP,wasConnected); + CloseChatUI(IP, wasConnected); } private void CloseChatUI(string IP, bool wasConnected = true) @@ -122,7 +131,7 @@ namespace PolyChat private bool isInConnections(string IP) { - if(Connections.ContainsKey(IP)) + if (Connections.ContainsKey(IP)) return true; return false; } diff --git a/PolyChat/MainPage.xaml.cs b/PolyChat/MainPage.xaml.cs index cfa30b5..9f1fded 100644 --- a/PolyChat/MainPage.xaml.cs +++ b/PolyChat/MainPage.xaml.cs @@ -6,6 +6,7 @@ using System; using System.Collections.ObjectModel; using System.Diagnostics; using System.Linq; +using System.Text.Json; using System.Threading.Tasks; using Windows.UI.Core; using Windows.UI.Xaml; @@ -99,8 +100,8 @@ namespace PolyChat if (result == ContentDialogResult.Primary) { username = dialog.getValue(); - if (username.Length == 0) textUsername.Text = "Unknown"; - else textUsername.Text = username; + textUsername.Text = username; + Controller.SendBroadcastMessage("username", username); } updateNoUsernamePlaceholder(); } @@ -128,8 +129,19 @@ namespace PolyChat { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { + var doc = JsonDocument.Parse(json).RootElement; + string type = doc.GetProperty("type").GetString(); + string content = doc.GetProperty("content").GetString(); ChatPartner sendingPartner = Partners.FirstOrDefault(p => p.Code == origin); - sendingPartner.AddMessage(new ChatMessage(origin, json)); + switch (type) + { + case "username": + sendingPartner.SetName(Name); + break; + default: + sendingPartner.AddMessage(new ChatMessage(origin, type, content)); + break; + } }); } public async void OnIncomingMessages(string origin, string json) @@ -145,7 +157,7 @@ namespace PolyChat origin, item["type"].ToString(), item["content"].ToString()//, - //DateTime.Parse(item["timestamp"].ToString()) + //DateTime.Parse(item["timestamp"].ToString()) ) ); } diff --git a/PolyChat/Models/ChatMessage.cs b/PolyChat/Models/ChatMessage.cs index 9fd22c2..5757702 100644 --- a/PolyChat/Models/ChatMessage.cs +++ b/PolyChat/Models/ChatMessage.cs @@ -24,7 +24,7 @@ namespace PolyChat.Models TimeStamp = DateTime.Now; Type = type; Content = content; - // no json = my messages + // TODO Foreign = false; Debug.WriteLine("Created Message: " + ToString()); } @@ -46,24 +46,6 @@ namespace PolyChat.Models Debug.WriteLine("Created Loaded Message: " + ToString()); } - /// - /// Create foreign Message (directly incoming) - /// - /// Foreign IP - /// Message Content as JSON with type and content - 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() { diff --git a/PolyChat/Models/ChatPartner.cs b/PolyChat/Models/ChatPartner.cs index 020433d..118b7af 100644 --- a/PolyChat/Models/ChatPartner.cs +++ b/PolyChat/Models/ChatPartner.cs @@ -24,5 +24,10 @@ namespace PolyChat.Models { Messages.Add(message); } + + public void SetName(string name) + { + Name = name; + } } } \ No newline at end of file