[ADD] added method for generating a message
This commit is contained in:
@@ -10,13 +10,12 @@ namespace Server.Models
|
|||||||
{
|
{
|
||||||
class ServerClient : ObservableObject
|
class ServerClient : ObservableObject
|
||||||
{
|
{
|
||||||
public string Username { get; set; }
|
|
||||||
private TcpClient tcpClient;
|
private TcpClient tcpClient;
|
||||||
private NetworkStream stream;
|
private NetworkStream stream;
|
||||||
private byte[] buffer = new byte[1024];
|
private byte[] buffer = new byte[1024];
|
||||||
private byte[] totalBuffer = new byte[1024];
|
private byte[] totalBuffer = new byte[1024];
|
||||||
private int totalBufferReceived = 0;
|
private int totalBufferReceived = 0;
|
||||||
public User user;
|
public User User { get; set; }
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -85,7 +84,7 @@ namespace Server.Models
|
|||||||
/// <param name="message">the incoming message</param>
|
/// <param name="message">the incoming message</param>
|
||||||
private void HandleIncomingMessage(byte[] 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 id = message[0];
|
||||||
byte[] payload = new byte[message.Length - 1];
|
byte[] payload = new byte[message.Length - 1];
|
||||||
Array.Copy(message,1,payload,0,message.Length-1);
|
Array.Copy(message,1,payload,0,message.Length-1);
|
||||||
@@ -97,8 +96,9 @@ namespace Server.Models
|
|||||||
string uName = JSONConvert.GetUsernameLogin(message);
|
string uName = JSONConvert.GetUsernameLogin(message);
|
||||||
if (uName != null)
|
if (uName != null)
|
||||||
{
|
{
|
||||||
Username = uName;
|
User = new User(uName);
|
||||||
Debug.WriteLine("[SERVERCLIENT] set username to " + Username);
|
User.Username = uName;
|
||||||
|
Debug.WriteLine("[SERVERCLIENT] set username to " + uName);
|
||||||
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ namespace SharedClientServer
|
|||||||
{
|
{
|
||||||
string msg = Encoding.ASCII.GetString(json);
|
string msg = Encoding.ASCII.GetString(json);
|
||||||
dynamic payload = JsonConvert.DeserializeObject(msg);
|
dynamic payload = JsonConvert.DeserializeObject(msg);
|
||||||
|
|
||||||
return (payload.username, payload.message);
|
return (payload.username, payload.message);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -19,5 +20,22 @@ namespace SharedClientServer
|
|||||||
dynamic payload = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json));
|
dynamic payload = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json));
|
||||||
return payload.username;
|
return payload.username;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// constructs a message that can be sent to the clients or server
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="identifier">the identifier for what kind of message it is</param>
|
||||||
|
/// <param name="payload">the json payload</param>
|
||||||
|
/// <returns>a byte array containing a message that can be sent to clients or server</returns>
|
||||||
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user