added methods to send to all in lobby

This commit is contained in:
Sem van der Hoeven
2020-10-13 13:28:18 +02:00
parent 52e83f111a
commit 1ae1b628f3
11 changed files with 193 additions and 6 deletions

View File

@@ -90,22 +90,30 @@ namespace Server.Models
Array.Copy(message,1,payload,0,message.Length-1);
switch(id)
{
case 0x01:
// canvas data
// json log in username data
string uName = JSONConvert.GetUsernameLogin(message);
if (uName != null) Username = uName;
Debug.WriteLine("[SERVERCLIENT] set username to " + Username);
break;
case 0x02:
// 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;
case 0x03:
// object data
// lobby data
break;
case 0x04:
// canvas data
break;
default:
Debug.WriteLine("Received weird identifier: " + id);
Debug.WriteLine("[SERVER] Received weird identifier: " + id);
break;
}
//TODO implement ways to handle the message

View File

@@ -1,4 +1,5 @@

using Client;
using SharedClientServer;
using System;
using System.Collections.Generic;
@@ -15,6 +16,8 @@ namespace Server.Models
private List<ServerClient> serverClients;
public bool Started = false;
public int ClientsConnected { get { return serverClients.Count; } }
public List<Lobby> lobbies;
private Dictionary<Lobby, List<ServerClient>> serverClientsInlobbies;
/// <summary>
/// use a padlock object to make sure the singleton is thread-safe
@@ -28,6 +31,8 @@ namespace Server.Models
{
listener = new TcpListener(IPAddress.Any, port);
serverClients = new List<ServerClient>();
lobbies = new List<Lobby>();
serverClientsInlobbies = new Dictionary<Lobby, List<ServerClient>>();
}
/// <summary>
@@ -87,12 +92,44 @@ namespace Server.Models
}
}
public void sendToAllExcept(string username, byte[] message)
public void SendToAllExcept(string username, byte[] message)
{
foreach (ServerClient sc in serverClients)
{
if (sc.Username != username) sc.sendMessage(message);
}
}
public void SendToLobby(Lobby lobby, byte[] message)
{
foreach (Lobby l in lobbies)
{
if (l == lobby)
{
foreach (ServerClient sc in serverClientsInlobbies[l])
{
sc.sendMessage(message);
}
break;
}
}
}
public void AddToLobby(Lobby lobby, string username)
{
foreach (Lobby l in lobbies)
{
if (l == lobby)
{
bool succ;
l.AddUsername(username, out succ);
if (!succ)
{
// TODO send lobby full message
}
break;
}
}
}
}
}

View File

@@ -6,8 +6,6 @@
<UseWPF>true</UseWPF>
</PropertyGroup>
<Import Project="..\SharedClientServer\SharedClientServer.projitems" Label="Shared" />
<ItemGroup>
<PackageReference Include="AsyncAwaitBestPractices" Version="4.3.0" />
<PackageReference Include="MvvmLightLibs" Version="5.4.1.1" />
@@ -15,4 +13,6 @@
<PackageReference Include="PropertyChanged.Fody" Version="3.2.9" />
</ItemGroup>
<Import Project="..\SharedClientServer\SharedClientServer.projitems" Label="Shared" />
</Project>