diff --git a/Client/Client.cs b/Client/Client.cs
index 17551de..5152afe 100644
--- a/Client/Client.cs
+++ b/Client/Client.cs
@@ -72,10 +72,10 @@ namespace Client
Array.Copy(message, 1, payload, 0, message.Length - 1);
switch (id)
{
- case 0x01:
+ case JSONConvert.LOGIN:
// json log in username data
break;
- case 0x02:
+ case JSONConvert.MESSAGE:
// json message data
(string, string) combo = JSONConvert.GetUsernameAndMessage(payload);
string textUsername = combo.Item1;
@@ -84,11 +84,11 @@ namespace Client
break;
- case 0x03:
+ case JSONConvert.LOBBY:
// lobby data
//TODO fill lobby with the data received
break;
- case 0x04:
+ case JSONConvert.CANVAS:
// canvas data
break;
default:
diff --git a/Client/ViewModels/ViewModel.cs b/Client/ViewModels/ViewModel.cs
index fb310bf..abb89e3 100644
--- a/Client/ViewModels/ViewModel.cs
+++ b/Client/ViewModels/ViewModel.cs
@@ -5,6 +5,7 @@ using System.ComponentModel;
using System.Text;
using System.Windows.Input;
using SharedClientServer;
+using System.Diagnostics;
namespace Client
{
@@ -43,9 +44,6 @@ namespace Client
{
get
{
- if (_model == null)
- _model = new Model();
-
return _model;
}
@@ -61,5 +59,10 @@ namespace Client
get { return _lobbies; }
set { _lobbies = value; }
}
+
+ public void OnHostButtonClick()
+ {
+ Debug.WriteLine("Click host button");
+ }
}
}
diff --git a/Client/Views/MainWindow.xaml b/Client/Views/MainWindow.xaml
index 7830f07..b3221df 100644
--- a/Client/Views/MainWindow.xaml
+++ b/Client/Views/MainWindow.xaml
@@ -12,7 +12,7 @@
-
+
@@ -34,7 +34,6 @@
-
@@ -48,6 +47,7 @@
+
@@ -68,7 +68,7 @@
-
+
diff --git a/Client/Views/MainWindow.xaml.cs b/Client/Views/MainWindow.xaml.cs
index ae7e032..1fe9334 100644
--- a/Client/Views/MainWindow.xaml.cs
+++ b/Client/Views/MainWindow.xaml.cs
@@ -37,7 +37,6 @@ namespace Client
if(lobbySelected != null)
{
testLabel.Content = lobbySelected.ID;
- usernameTextbox.IsEnabled = false;
colorSelection.IsEnabled = false;
joinButton.IsEnabled = false;
hostButton.IsEnabled = false;
diff --git a/Server/Models/ServerClient.cs b/Server/Models/ServerClient.cs
index ab8af5b..accc07e 100644
--- a/Server/Models/ServerClient.cs
+++ b/Server/Models/ServerClient.cs
@@ -10,12 +10,12 @@ namespace Server.Models
{
class ServerClient : ObservableObject
{
- public string Username { get; set; }
private TcpClient tcpClient;
private NetworkStream stream;
private byte[] buffer = new byte[1024];
private byte[] totalBuffer = new byte[1024];
private int totalBufferReceived = 0;
+ public User User { get; set; }
///
@@ -84,24 +84,25 @@ namespace Server.Models
/// the incoming message
private void HandleIncomingMessage(byte[] message)
{
- Debug.WriteLine($"Got message from {Username} : {message}");
+ Debug.WriteLine($"Got message from {User?.Username} : {message}");
byte id = message[0];
byte[] payload = new byte[message.Length - 1];
Array.Copy(message,1,payload,0,message.Length-1);
switch(id)
{
- case 0x01:
+ case JSONConvert.LOGIN:
// json log in username data
- string uName = JSONConvert.GetUsernameLogin(message);
+ string uName = JSONConvert.GetUsernameLogin(payload);
if (uName != null)
{
- Username = uName;
- Debug.WriteLine("[SERVERCLIENT] set username to " + Username);
+ User = new User(uName);
+ User.Username = uName;
+ Debug.WriteLine("[SERVERCLIENT] set username to " + uName);
}
break;
- case 0x02:
+ case JSONConvert.MESSAGE:
// json message data
(string, string) combo = JSONConvert.GetUsernameAndMessage(payload);
string textUsername = combo.Item1;
@@ -110,10 +111,10 @@ namespace Server.Models
// todo handle sending to all except this user the username and message to display in chat
break;
- case 0x03:
+ case JSONConvert.LOBBY:
// lobby data
break;
- case 0x04:
+ case JSONConvert.CANVAS:
// canvas data
// todo send canvas data to all other serverclients in lobby
break;
diff --git a/Server/Models/ServerCommunication.cs b/Server/Models/ServerCommunication.cs
index be3a8f8..96a7ace 100644
--- a/Server/Models/ServerCommunication.cs
+++ b/Server/Models/ServerCommunication.cs
@@ -18,6 +18,7 @@ namespace Server.Models
public List lobbies;
private Dictionary> serverClientsInlobbies;
public Action newClientAction;
+
///
/// use a padlock object to make sure the singleton is thread-safe
@@ -98,7 +99,7 @@ namespace Server.Models
{
foreach (ServerClient sc in serverClients)
{
- if (sc.Username != username) sc.sendMessage(message);
+ if (sc.User.Username != username) sc.sendMessage(message);
}
}
diff --git a/SharedClientServer/JSONConvert.cs b/SharedClientServer/JSONConvert.cs
index c62b9ae..dac7455 100644
--- a/SharedClientServer/JSONConvert.cs
+++ b/SharedClientServer/JSONConvert.cs
@@ -7,10 +7,15 @@ namespace SharedClientServer
{
class JSONConvert
{
+ public const byte LOGIN = 0x01;
+ public const byte MESSAGE = 0x02;
+ public const byte LOBBY = 0x03;
+ public const byte CANVAS = 0x04;
public static (string,string) GetUsernameAndMessage(byte[] json)
{
string msg = Encoding.ASCII.GetString(json);
dynamic payload = JsonConvert.DeserializeObject(msg);
+
return (payload.username, payload.message);
}
@@ -19,5 +24,30 @@ namespace SharedClientServer
dynamic payload = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json));
return payload.username;
}
+
+ public static byte[] GetUsernameMessage(string uName)
+ {
+ return GetMessageToSend(LOGIN, new
+ {
+ username = uName
+ });
+ }
+
+ ///
+ /// constructs a message that can be sent to the clients or server
+ ///
+ /// the identifier for what kind of message it is
+ /// the json payload
+ /// a byte array containing a message that can be sent to clients or server
+ public static byte[] GetMessageToSend(byte identifier, dynamic payload)
+ {
+ // convert the dynamic to bytes
+ byte[] payloadBytes = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(payload));
+ // make the array that holds the message and copy the payload into it with the first spot containing the identifier
+ byte[] res = new byte[payloadBytes.Length + 1];
+ Array.Copy(payloadBytes, 0, res, 1, payloadBytes.Length);
+ res[0] = identifier;
+ return res;
+ }
}
}
diff --git a/SharedClientServer/User.cs b/SharedClientServer/User.cs
index f25efbf..6e95f64 100644
--- a/SharedClientServer/User.cs
+++ b/SharedClientServer/User.cs
@@ -17,6 +17,13 @@ namespace SharedClientServer
_host = host;
}
+ public User(string username)
+ {
+ _username = username;
+ _score = 0;
+ _host = false;
+ }
+
public string Username
{
get { return _username; }