diff --git a/PolyChat/Controller.cs b/PolyChat/Controller.cs
index 8b8b1eb..5a77266 100644
--- a/PolyChat/Controller.cs
+++ b/PolyChat/Controller.cs
@@ -5,8 +5,10 @@ using System.Net;
using SocketIOSharp.Server;
using SocketIOSharp.Server.Client;
using PolyChat.Models;
+using System.IO;
+using System.Threading;
+using System;
using System.Text.Json;
-using Newtonsoft.Json;
namespace PolyChat
{
@@ -33,6 +35,8 @@ namespace PolyChat
{
UIController = uiController;
OwnIP = getIP();
+ loadChats();
+ //SaveChats("10", "{das ist ein test}");
Serve();
}
@@ -86,10 +90,12 @@ namespace PolyChat
Debug.WriteLine($"{type} -> {ip} content: {content}");
JObject json = new JObject(
new JProperty("type", type),
- new JProperty("content", content)
+ new JProperty("content", content),
+ new JProperty("timestamp", DateTime.Now.ToString())
);
Debug.WriteLine($"json: {json.ToString()}");
Connections[ip].SendMessage(json.ToString());
+ SaveChats(ip, json.ToString());
}
private void OnMessage(string ip, JToken[] data)
@@ -99,6 +105,7 @@ namespace PolyChat
{
Debug.WriteLine("RAW: " + data[0]);
UIController.OnIncomingMessage(ip, data[0].ToString());
+ SaveChats(ip, data[0].ToString());
}
else Debug.WriteLine("Undefined: " + data);
}
@@ -111,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)
@@ -123,7 +130,7 @@ namespace PolyChat
private bool isInConnections(string IP)
{
- if(Connections.ContainsKey(IP))
+ if (Connections.ContainsKey(IP))
return true;
return false;
}
@@ -141,5 +148,89 @@ namespace PolyChat
}
return null;
}
+
+ ///
+ /// sends chatlogs as json array to uiController wit corrosponding ip
+ ///
+ ///
+ public void loadChats()
+ {
+ //load dir and create if non existant
+ if (Directory.Exists("U:\\PolyChat\\Saves"))
+ {
+ Debug.WriteLine("--Path exists.--");
+ }
+ else
+ {
+ Directory.CreateDirectory("U:\\PolyChat\\Saves");
+ Debug.WriteLine("--Path Created--.");
+ }
+
+ //go through all files and send ip and json array to ui
+ String[] filepaths = Directory.GetFiles("U:\\PolyChat\\Saves");
+ if (filepaths.Length > 0)
+ {
+ foreach (String path in filepaths)
+ {
+ Debug.WriteLine("---Loading Saves---");
+ Debug.WriteLine("--" + path + "--");
+ String jsonArr = File.ReadAllText(path);
+ String ip = Path.GetFileName(path);
+ ip = ip.Substring(0, ip.Length - 4);
+ Debug.WriteLine("-" + ip + "-");
+ Debug.WriteLine(jsonArr);
+ UIController.OnIncomingMessages(ip, jsonArr);
+ }
+ }
+
+ }
+
+ ///
+ /// Saves incoming chat message to
+ ///
+ ///
+ ///
+ public void SaveChats(String ip, String json)
+ {
+ //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
+ //writing flag setzen oder auch in der datei selbst ne flag setzen
+ //also save fils from myself
+ new Thread(() =>
+ {
+ if (File.Exists($"U:\\PolyChat\\Saves\\{ip}.txt"))
+ {
+ Debug.WriteLine("--File allready exists--");
+ //check for integraty of file
+ string output = File.ReadAllText($"U:\\PolyChat\\Saves\\{ip}.txt");
+ Debug.WriteLine($"---{output}---");
+ if (output.Substring(0, 1).Equals("[") && output.Substring(output.Length - 1, 1).Equals("]"))
+ {
+ Debug.WriteLine("--adding new chatmessage--");
+ //structure intact
+ //save new chat
+ String saved = output.Substring(0, output.Length - 1);
+ output = saved + ", " + json + " ]";
+ File.Delete($"U:\\PolyChat\\Saves\\{ip}.txt");
+ File.WriteAllText($"U:\\PolyChat\\Saves\\{ip}.txt", output);
+ }
+ else
+ {
+ Debug.WriteLine("--Structure not intact--");
+ Debug.WriteLine("--redoing file--");
+ //structure not intact
+ //redo file
+ File.Delete($"U:\\PolyChat\\Saves\\{ip}.txt");
+ File.WriteAllText($"U:\\PolyChat\\Saves\\{ip}.txt", $"[ {json} ]");
+ }
+ }
+ else
+ {
+ Debug.WriteLine("--Creating new File--");
+ //setup file
+ File.WriteAllText($"U:\\PolyChat\\Saves\\{ip}.txt", $"[ {json} ]");
+ }
+ }).Start();
+ }
}
}
diff --git a/PolyChat/MainPage.xaml.cs b/PolyChat/MainPage.xaml.cs
index cfa30b5..a2f244a 100644
--- a/PolyChat/MainPage.xaml.cs
+++ b/PolyChat/MainPage.xaml.cs
@@ -152,6 +152,7 @@ namespace PolyChat
});
}
+
private void OnDeleteChat(object sender = null, RoutedEventArgs e = null)
{
Controller.CloseChat(selectedPartner.Code);