remove MSG.cs, merge content from MSG.cs and ChatMessage.cs, refactor code

This commit is contained in:
Felix Hartmann (PEA3-Fe-FI)
2021-09-21 09:28:29 +02:00
parent 5d69842ec0
commit d42acbc90e
6 changed files with 29 additions and 50 deletions

View File

@@ -1,23 +1,31 @@
namespace PolyChat.Models
using System;
namespace PolyChat.Models
{
public class ChatMessage
{
public string Text;
public string Date;
public bool Foreign;
public readonly string Sender;
public readonly DateTime Timestamp = new DateTime(1970, 01, 01);
public readonly string Msg = "empty";
public readonly string Ip;
public readonly bool Foreign;
public readonly string StringTimeStamp;
public ChatMessage(string text, string date, bool foreign)
public ChatMessage(DateTime Timestamp, string Msg, bool Foreign = true, string Sender= "Unknown", string Ip = "127.0.0.1")
{
Text = text;
Date = date;
Foreign = foreign;
this.Sender = Sender;
this.Timestamp = Timestamp;
StringTimeStamp = Timestamp.ToString();
this.Msg = Msg;
this.Foreign = Foreign;
this.Ip = Ip;
}
override
public string ToString()
{
string prefix = Foreign ? "Other" : "Me";
return $"{prefix}: Text";
return $"{prefix}: {Msg}({Sender})";
}
}
}

View File

@@ -24,7 +24,7 @@ namespace PolyChat.Models
{
private SocketIOClient connection;
public Boolean isConnected = false;
private List<MSG> msgStack = new List<MSG>();
private List<ChatMessage> msgStack = new List<ChatMessage>();
private Boolean active = true;
private String ip;
@@ -49,7 +49,7 @@ namespace PolyChat.Models
new Thread(() =>
{
//create msg
MSG msg = new MSG(sender, Controller.ip, chatMessage, timestamp);
ChatMessage msg = new ChatMessage(timestamp, chatMessage, false, sender, Controller.ip);
//convert msg
String petJson = JsonNet.Serialize(msg);
@@ -78,7 +78,7 @@ namespace PolyChat.Models
{
connection.On(SendCode.Message.ToString(), (Data) =>
{
MSG pet = JsonNet.Deserialize<MSG>(BitConverter.ToString(Data[0].ToObject<byte[]>()));
ChatMessage pet = JsonNet.Deserialize<ChatMessage>(BitConverter.ToString(Data[0].ToObject<byte[]>()));
//TODO: send message to GUI
});
connection.On(SendCode.Command.ToString(), (Data) =>

View File

@@ -1,29 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace PolyChat.Models
{
/// <summary>
/// dumy class for json converter
/// </summary>
public class MSG
{
public string sender = "unknown";
public DateTime timestamp = new DateTime(2000, 01, 01);
public string msg = "empty";
public string ip;
public MSG(string sender, string ip, String msg, DateTime timestamp)
{
this.sender = sender;
this.ip = ip;
this.timestamp = timestamp;
this.msg = msg;
}
}
}