added template for receiving json chat message

This commit is contained in:
Sem van der Hoeven
2020-10-13 10:56:13 +02:00
parent 514460781b
commit 3dc641680d
5 changed files with 36 additions and 2 deletions

View File

@@ -86,16 +86,23 @@ namespace Server.Models
{ {
Debug.WriteLine($"Got message from {Username} : {message}"); Debug.WriteLine($"Got message from {Username} : {message}");
byte id = message[0]; byte id = message[0];
byte[] payload = new byte[message.Length - 1];
Array.Copy(message,1,payload,0,message.Length-1);
switch(id) switch(id)
{ {
case 0x01: case 0x01:
// canvas data // canvas data
break; break;
case 0x02: case 0x02:
// message data // json message data
(string, string) combo = JSONConvert.GetUsernameAndMessage(payload);
string textUsername = combo.Item1;
string textMsg = combo.Item2;
// todo handle sending to all except this user the username and message to display in chat
break; break;
case 0x03: case 0x03:
// json data // object data
break; break;
default: default:
Debug.WriteLine("Received weird identifier: " + id); Debug.WriteLine("Received weird identifier: " + id);

View File

@@ -85,5 +85,13 @@ namespace Server.Models
sc.sendMessage(message); sc.sendMessage(message);
} }
} }
public void sendToAllExcept(string username, byte[] message)
{
foreach (ServerClient sc in serverClients)
{
if (sc.Username != username) sc.sendMessage(message);
}
}
} }
} }

View File

@@ -11,6 +11,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="AsyncAwaitBestPractices" Version="4.3.0" /> <PackageReference Include="AsyncAwaitBestPractices" Version="4.3.0" />
<PackageReference Include="MvvmLightLibs" Version="5.4.1.1" /> <PackageReference Include="MvvmLightLibs" Version="5.4.1.1" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="PropertyChanged.Fody" Version="3.2.9" /> <PackageReference Include="PropertyChanged.Fody" Version="3.2.9" />
</ItemGroup> </ItemGroup>

View File

@@ -0,0 +1,17 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;
namespace SharedClientServer
{
class JSONConvert
{
public static (string,string) GetUsernameAndMessage(byte[] json)
{
string msg = Encoding.ASCII.GetString(json);
dynamic payload = JsonConvert.DeserializeObject(msg);
return (payload.username, payload.message);
}
}
}

View File

@@ -10,6 +10,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)ClientServerUtil.cs" /> <Compile Include="$(MSBuildThisFileDirectory)ClientServerUtil.cs" />
<Compile Include="$(MSBuildThisFileDirectory)JSONConvert.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ObservableObject.cs" /> <Compile Include="$(MSBuildThisFileDirectory)ObservableObject.cs" />
</ItemGroup> </ItemGroup>
</Project> </Project>