Added BoardcastMessage, Username Changed event via Broadcast, remove json parsing from ChatMessage
This commit is contained in:
@@ -80,6 +80,15 @@ namespace PolyChat
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void SendBroadcastMessage(string type, string content)
|
||||||
|
{
|
||||||
|
Debug.WriteLine("--- Controller.Broadcast ---");
|
||||||
|
foreach (KeyValuePair<string, Connection> entry in Connections)
|
||||||
|
{
|
||||||
|
SendMessage(entry.Key, type, content);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void SendMessage(string ip, string type, string content)
|
public void SendMessage(string ip, string type, string content)
|
||||||
{
|
{
|
||||||
Debug.WriteLine("--- Controller.SendMessage ---");
|
Debug.WriteLine("--- Controller.SendMessage ---");
|
||||||
@@ -111,7 +120,7 @@ namespace PolyChat
|
|||||||
Connections[IP].Close();
|
Connections[IP].Close();
|
||||||
Connections.Remove(IP);
|
Connections.Remove(IP);
|
||||||
}
|
}
|
||||||
CloseChatUI(IP,wasConnected);
|
CloseChatUI(IP, wasConnected);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void CloseChatUI(string IP, bool wasConnected = true)
|
private void CloseChatUI(string IP, bool wasConnected = true)
|
||||||
@@ -123,7 +132,7 @@ namespace PolyChat
|
|||||||
|
|
||||||
private bool isInConnections(string IP)
|
private bool isInConnections(string IP)
|
||||||
{
|
{
|
||||||
if(Connections.ContainsKey(IP))
|
if (Connections.ContainsKey(IP))
|
||||||
return true;
|
return true;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ using System;
|
|||||||
using System.Collections.ObjectModel;
|
using System.Collections.ObjectModel;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Text.Json;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Windows.UI.Core;
|
using Windows.UI.Core;
|
||||||
using Windows.UI.Xaml;
|
using Windows.UI.Xaml;
|
||||||
@@ -99,8 +100,8 @@ namespace PolyChat
|
|||||||
if (result == ContentDialogResult.Primary)
|
if (result == ContentDialogResult.Primary)
|
||||||
{
|
{
|
||||||
username = dialog.getValue();
|
username = dialog.getValue();
|
||||||
if (username.Length == 0) textUsername.Text = "Unknown";
|
textUsername.Text = username;
|
||||||
else textUsername.Text = username;
|
Controller.SendBroadcastMessage("username", username);
|
||||||
}
|
}
|
||||||
updateNoUsernamePlaceholder();
|
updateNoUsernamePlaceholder();
|
||||||
}
|
}
|
||||||
@@ -128,8 +129,19 @@ namespace PolyChat
|
|||||||
{
|
{
|
||||||
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
|
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);
|
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)
|
public async void OnIncomingMessages(string origin, string json)
|
||||||
@@ -145,7 +157,7 @@ namespace PolyChat
|
|||||||
origin,
|
origin,
|
||||||
item["type"].ToString(),
|
item["type"].ToString(),
|
||||||
item["content"].ToString()//,
|
item["content"].ToString()//,
|
||||||
//DateTime.Parse(item["timestamp"].ToString())
|
//DateTime.Parse(item["timestamp"].ToString())
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ namespace PolyChat.Models
|
|||||||
TimeStamp = DateTime.Now;
|
TimeStamp = DateTime.Now;
|
||||||
Type = type;
|
Type = type;
|
||||||
Content = content;
|
Content = content;
|
||||||
// no json = my messages
|
// TODO
|
||||||
Foreign = false;
|
Foreign = false;
|
||||||
Debug.WriteLine("Created Message: " + ToString());
|
Debug.WriteLine("Created Message: " + ToString());
|
||||||
}
|
}
|
||||||
@@ -46,24 +46,6 @@ namespace PolyChat.Models
|
|||||||
Debug.WriteLine("Created Loaded Message: " + ToString());
|
Debug.WriteLine("Created Loaded Message: " + ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Create foreign Message (directly incoming)
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="origin">Foreign IP</param>
|
|
||||||
/// <param name="json">Message Content as JSON with type and content</param>
|
|
||||||
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
|
override
|
||||||
public string ToString()
|
public string ToString()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -24,5 +24,10 @@ namespace PolyChat.Models
|
|||||||
{
|
{
|
||||||
Messages.Add(message);
|
Messages.Add(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void SetName(string name)
|
||||||
|
{
|
||||||
|
Name = name;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user