Removed timeStamp from SendMessage, added param Timestamp to savechat()

This commit is contained in:
Patrick Hellebrand
2021-09-23 11:30:09 +02:00
parent 54266e52ec
commit 8121a8dbfd
3 changed files with 17 additions and 14 deletions

View File

@@ -99,12 +99,13 @@ namespace PolyChat
Debug.WriteLine($"{type} -> {ip} content: {content}"); Debug.WriteLine($"{type} -> {ip} content: {content}");
JObject json = new JObject( JObject json = new JObject(
new JProperty("type", type), new JProperty("type", type),
new JProperty("content", content), new JProperty("content", content)
new JProperty("timestamp", DateTime.Now.ToString())
); );
Debug.WriteLine($"json: {json.ToString()}"); Debug.WriteLine($"json: {json.ToString()}");
// send as json
Connections[ip].SendMessage(json.ToString()); Connections[ip].SendMessage(json.ToString());
SaveChats(ip, json.ToString()); // save to logs
SaveChats(ip, json.ToString(), DateTime.Now);
} }
private void OnMessage(string ip, JToken[] data) private void OnMessage(string ip, JToken[] data)
@@ -112,9 +113,10 @@ namespace PolyChat
Debug.WriteLine("--- Controller.OnMessage ---"); Debug.WriteLine("--- Controller.OnMessage ---");
if (data != null && data.Length > 0 && data[0] != null) if (data != null && data.Length > 0 && data[0] != null)
{ {
DateTime now = DateTime.Now;
Debug.WriteLine("RAW: " + data[0]); Debug.WriteLine("RAW: " + data[0]);
UIController.OnIncomingMessage(ip, data[0].ToString()); UIController.OnIncomingMessage(ip, data[0].ToString(), now);
SaveChats(ip, data[0].ToString()); SaveChats(ip, data[0].ToString(), now);
} }
else Debug.WriteLine("Undefined: " + data); else Debug.WriteLine("Undefined: " + data);
} }
@@ -199,10 +201,10 @@ namespace PolyChat
/// </summary> /// </summary>
/// <param name="ip"></param> /// <param name="ip"></param>
/// <param name="json"></param> /// <param name="json"></param>
public void SaveChats(String ip, String json) public void SaveChats(string ip, string json, DateTime timeStamp)
{ {
//Vielleicht noch so machen dass die mit gleicher ip nacheinander gemacht //Vielleicht noch so machen dass die mit gleicher ip nacheinander gemacht
//werden damit es ncith zu überschreibungen kommt vielleicth auch ganz oben oder am ende ne //werden damit es nicht zu überschreibungen kommt vielleicth auch ganz oben oder am ende ne
//writing flag setzen oder auch in der datei selbst ne flag setzen //writing flag setzen oder auch in der datei selbst ne flag setzen
//also save fils from myself //also save fils from myself
new Thread(() => new Thread(() =>
@@ -217,6 +219,7 @@ namespace PolyChat
{ {
Debug.WriteLine("--adding new chatmessage--"); Debug.WriteLine("--adding new chatmessage--");
//structure intact //structure intact
JObject obj = JObject.Parse(json);
//save new chat //save new chat
String saved = output.Substring(0, output.Length - 1); String saved = output.Substring(0, output.Length - 1);
output = saved + ", " + json + " ]"; output = saved + ", " + json + " ]";

View File

@@ -125,7 +125,7 @@ namespace PolyChat
/// Adds an message to the UI, based on .sender if known /// Adds an message to the UI, based on .sender if known
/// </summary> /// </summary>
/// <param name="message">ChatMessage</param> /// <param name="message">ChatMessage</param>
public async void OnIncomingMessage(string origin, string json) public async void OnIncomingMessage(string origin, string json, DateTime timeStamp)
{ {
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{ {
@@ -139,7 +139,7 @@ namespace PolyChat
sendingPartner.SetName(Name); sendingPartner.SetName(Name);
break; break;
default: default:
sendingPartner.AddMessage(new ChatMessage(origin, type, content)); sendingPartner.AddMessage(new ChatMessage(origin, type, content, timeStamp));
break; break;
} }
}); });
@@ -156,8 +156,8 @@ namespace PolyChat
new ChatMessage( new ChatMessage(
origin, origin,
item["type"].ToString(), item["type"].ToString(),
item["content"].ToString()//, item["content"].ToString(),
//DateTime.Parse(item["timestamp"].ToString()) DateTime.Parse(item["timestamp"].ToString())
) )
); );
} }

View File

@@ -33,9 +33,9 @@ namespace PolyChat.Models
/// Create Message loaded with timestamp /// Create Message loaded with timestamp
/// </summary> /// </summary>
/// <param name="origin">Origin IP</param> /// <param name="origin">Origin IP</param>
/// <param name="type">Message Type</param> /// <param name="type">Message Type, usually "message"</param>
/// <param name="content">Message Content (not JSON)</param> /// <param name="content">Message Content, usually plain text</param>
/// <param name="timeStamp">Message Content (not JSON)</param> /// <param name="timeStamp">Parsed DateTime</param>
public ChatMessage(string origin, string type, string content, DateTime timeStamp, bool foreign = false) public ChatMessage(string origin, string type, string content, DateTime timeStamp, bool foreign = false)
{ {
Origin = origin; Origin = origin;