Added json support with type+content to ChatMessage
This commit is contained in:
@@ -59,7 +59,6 @@ namespace PolyChat
|
|||||||
{
|
{
|
||||||
IPHostEntry ipEntry = Dns.GetHostEntry(Dns.GetHostName());
|
IPHostEntry ipEntry = Dns.GetHostEntry(Dns.GetHostName());
|
||||||
IPAddress[] addrList = ipEntry.AddressList;
|
IPAddress[] addrList = ipEntry.AddressList;
|
||||||
|
|
||||||
for (short i = 0; i < addrList.Length; i++)
|
for (short i = 0; i < addrList.Length; i++)
|
||||||
{
|
{
|
||||||
if (addrList[i].ToString().Substring(0, 3).Equals("10."))
|
if (addrList[i].ToString().Substring(0, 3).Equals("10."))
|
||||||
|
|||||||
@@ -1,29 +1,48 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
namespace PolyChat.Models
|
namespace PolyChat.Models
|
||||||
{
|
{
|
||||||
public class ChatMessage
|
public class ChatMessage
|
||||||
{
|
{
|
||||||
public readonly string Sender;
|
private string Origin;
|
||||||
public readonly DateTime Timestamp = new DateTime(1970, 01, 01);
|
private string Type;
|
||||||
public readonly string Content;
|
private string Content;
|
||||||
public readonly string Ip;
|
private DateTime TimeStamp;
|
||||||
public readonly bool Foreign;
|
public readonly bool Foreign;
|
||||||
|
//
|
||||||
|
public readonly string Ip;
|
||||||
|
|
||||||
public ChatMessage(string Content = "", bool Foreign = true, string Sender= "Unknown", string Ip = "127.0.0.1")
|
public ChatMessage(string content = "", string origin = "Unknown", string ip = "127.0.0.1")
|
||||||
{
|
{
|
||||||
this.Sender = Sender;
|
Origin = origin;
|
||||||
this.Timestamp = DateTime.Now;
|
TimeStamp = DateTime.Now;
|
||||||
this.Content = Content;
|
Content = content;
|
||||||
this.Foreign = Foreign;
|
Ip = ip;
|
||||||
this.Ip = Ip;
|
// no json = my messages
|
||||||
|
Foreign = false;
|
||||||
|
Debug.WriteLine("Created Message: " + ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
public ChatMessage(string origin, string json)
|
||||||
|
{
|
||||||
|
Origin = origin;
|
||||||
|
// parse and save to object
|
||||||
|
var obj = JsonDocument.Parse(json).RootElement;
|
||||||
|
Type = obj.GetProperty("type").GetString();
|
||||||
|
Content = obj.GetProperty("content").GetString();
|
||||||
|
TimeStamp = DateTime.Now;
|
||||||
|
// json = foreign
|
||||||
|
Foreign = true;
|
||||||
|
Debug.WriteLine("Created Message: " + ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
override
|
override
|
||||||
public string ToString()
|
public string ToString()
|
||||||
{
|
{
|
||||||
string prefix = Foreign ? "Other" : "Me";
|
string prefix = Foreign ? "Other" : "Me";
|
||||||
return $"{prefix}: {Content}({Sender})";
|
return $"{Type} from {prefix}: {Content}({Origin})";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -124,6 +124,7 @@
|
|||||||
<Compile Include="MainPage.xaml.cs">
|
<Compile Include="MainPage.xaml.cs">
|
||||||
<DependentUpon>MainPage.xaml</DependentUpon>
|
<DependentUpon>MainPage.xaml</DependentUpon>
|
||||||
</Compile>
|
</Compile>
|
||||||
|
<Compile Include="Models\ChatMessage.cs" />
|
||||||
<Compile Include="Models\Message.cs" />
|
<Compile Include="Models\Message.cs" />
|
||||||
<Compile Include="Models\ChatPartner.cs" />
|
<Compile Include="Models\ChatPartner.cs" />
|
||||||
<Compile Include="Models\Exceptions\MessageTimedOutException.cs" />
|
<Compile Include="Models\Exceptions\MessageTimedOutException.cs" />
|
||||||
@@ -193,6 +194,9 @@
|
|||||||
<PackageReference Include="SocketIOSharp">
|
<PackageReference Include="SocketIOSharp">
|
||||||
<Version>2.0.3</Version>
|
<Version>2.0.3</Version>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
|
<PackageReference Include="System.Text.Json">
|
||||||
|
<Version>5.0.2</Version>
|
||||||
|
</PackageReference>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<PropertyGroup Condition=" '$(VisualStudioVersion)' == '' or '$(VisualStudioVersion)' < '14.0' ">
|
<PropertyGroup Condition=" '$(VisualStudioVersion)' == '' or '$(VisualStudioVersion)' < '14.0' ">
|
||||||
<VisualStudioVersion>14.0</VisualStudioVersion>
|
<VisualStudioVersion>14.0</VisualStudioVersion>
|
||||||
|
|||||||
Reference in New Issue
Block a user