Saving and loading but still has error(Pls fix)

This commit is contained in:
SCM6WE
2021-09-23 10:50:40 +02:00
parent f2b0af08c5
commit 89059491c9
2 changed files with 96 additions and 4 deletions

View File

@@ -5,8 +5,10 @@ using System.Net;
using SocketIOSharp.Server; using SocketIOSharp.Server;
using SocketIOSharp.Server.Client; using SocketIOSharp.Server.Client;
using PolyChat.Models; using PolyChat.Models;
using System.IO;
using System.Threading;
using System;
using System.Text.Json; using System.Text.Json;
using Newtonsoft.Json;
namespace PolyChat namespace PolyChat
{ {
@@ -33,6 +35,8 @@ namespace PolyChat
{ {
UIController = uiController; UIController = uiController;
OwnIP = getIP(); OwnIP = getIP();
loadChats();
//SaveChats("10", "{das ist ein test}");
Serve(); Serve();
} }
@@ -86,10 +90,12 @@ 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()}");
Connections[ip].SendMessage(json.ToString()); Connections[ip].SendMessage(json.ToString());
SaveChats(ip, json.ToString());
} }
private void OnMessage(string ip, JToken[] data) private void OnMessage(string ip, JToken[] data)
@@ -99,6 +105,7 @@ namespace PolyChat
{ {
Debug.WriteLine("RAW: " + data[0]); Debug.WriteLine("RAW: " + data[0]);
UIController.OnIncomingMessage(ip, data[0].ToString()); UIController.OnIncomingMessage(ip, data[0].ToString());
SaveChats(ip, data[0].ToString());
} }
else Debug.WriteLine("Undefined: " + data); else Debug.WriteLine("Undefined: " + data);
} }
@@ -111,7 +118,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 +130,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;
} }
@@ -141,5 +148,89 @@ namespace PolyChat
} }
return null; return null;
} }
/// <summary>
/// sends chatlogs as json array to uiController wit corrosponding ip
/// </summary>
/// <param name="ip"></param>
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);
}
}
}
/// <summary>
/// Saves incoming chat message to
/// </summary>
/// <param name="ip"></param>
/// <param name="json"></param>
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();
}
} }
} }

View File

@@ -152,6 +152,7 @@ namespace PolyChat
}); });
} }
private void OnDeleteChat(object sender = null, RoutedEventArgs e = null) private void OnDeleteChat(object sender = null, RoutedEventArgs e = null)
{ {
Controller.CloseChat(selectedPartner.Code); Controller.CloseChat(selectedPartner.Code);