Skip to content

Commit

Permalink
hm yes json go brr
Browse files Browse the repository at this point in the history
  • Loading branch information
ced777ric committed Nov 29, 2020
1 parent 9d744a6 commit 66dacc4
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions SimpleConsole/TcpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,25 @@ private void ListenForData()
Array.Copy(bytes, 0, incommingData, 0, length);
// Convert byte array to string message.
string serverMessage = Encoding.ASCII.GetString(incommingData);
Dictionary<string, string> response = JsonSerializer.Deserialize<Dictionary<string, string>>(serverMessage);
response["color"] = response["color"].Replace("RGBA(", "").Replace(")", "");
string[] color = response["color"].Split(',');
Color clr = Color.FromArgb(Convert.ToInt32(color[3]), Convert.ToInt32(color[0]), Convert.ToInt32(color[1]), Convert.ToInt32(color[2]));
ConsoleColor oldcol = Console.ForegroundColor;
Console.ForegroundColor = Program.FromHex(clr.Name);
Console.WriteLine($"[{DateTime.Now.Hour}:{DateTime.Now.Minute}:{DateTime.Now.Second}] " + response["message"]);
Console.ForegroundColor = oldcol;
try
{
Dictionary<string, string> response =
JsonSerializer.Deserialize<Dictionary<string, string>>(serverMessage);
response["color"] = response["color"].Replace("RGBA(", "").Replace(")", "");
string[] color = response["color"].Split(',');
Color clr = Color.FromArgb(Convert.ToInt32(color[3]), Convert.ToInt32(color[0]), Convert.ToInt32(color[1]), Convert.ToInt32(color[2]));
ConsoleColor oldcol = Console.ForegroundColor;
Console.ForegroundColor = Program.FromHex(clr.Name);
Console.WriteLine($"[{DateTime.Now.Hour}:{DateTime.Now.Minute}:{DateTime.Now.Second}] " + response["message"]);
Console.ForegroundColor = oldcol;
}
catch (JsonException ex) //so the console doesnt crash when json breaks
{
ConsoleColor oldcol = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"[{DateTime.Now.Hour}:{DateTime.Now.Minute}:{DateTime.Now.Second}] " + serverMessage);
Console.ForegroundColor = oldcol;
}
}
}
}
Expand Down

0 comments on commit 66dacc4

Please sign in to comment.