Saving and loading but still has error(Pls fix)
This commit is contained in:
committed by
Felix Hartmann (PEA3-Fe-FI)
parent
7e5883d761
commit
eeea238398
@@ -5,6 +5,9 @@ using System.Net;
|
|||||||
using PolyChat.Models;
|
using PolyChat.Models;
|
||||||
using SocketIOSharp.Server;
|
using SocketIOSharp.Server;
|
||||||
using SocketIOSharp.Server.Client;
|
using SocketIOSharp.Server.Client;
|
||||||
|
using System.IO;
|
||||||
|
using System.Threading;
|
||||||
|
using System;
|
||||||
|
|
||||||
namespace PolyChat
|
namespace PolyChat
|
||||||
{
|
{
|
||||||
@@ -31,6 +34,8 @@ namespace PolyChat
|
|||||||
{
|
{
|
||||||
UIController = uiController;
|
UIController = uiController;
|
||||||
OwnIP = getIP();
|
OwnIP = getIP();
|
||||||
|
loadChats();
|
||||||
|
//SaveChats("10", "{das ist ein test}");
|
||||||
Serve();
|
Serve();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,10 +98,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)
|
||||||
@@ -106,6 +113,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);
|
||||||
}
|
}
|
||||||
@@ -149,5 +157,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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -164,6 +164,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);
|
||||||
|
|||||||
Reference in New Issue
Block a user