diff --git a/Client/Client.cs b/Client/Client.cs index 88278ad..bc7b2b5 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -25,6 +25,7 @@ namespace Client public Callback OnLobbiesReceivedAndWaitingForHost; public LobbyCallback OnLobbyCreated; public LobbyCallback OnLobbyLeave; + private ClientData data = ClientData.Instance; public Lobby[] Lobbies { get; set; } public Client(string username) @@ -82,6 +83,7 @@ namespace Client byte[] payload = new byte[message.Length - 5]; Array.Copy(message, 5, payload, 0, message.Length - 5); + Debug.WriteLine("[CLIENT] GOT STRING" + Encoding.ASCII.GetString(payload)); switch (id) { case JSONConvert.LOGIN: @@ -93,6 +95,11 @@ namespace Client string textUsername = combo.Item1; string textMsg = combo.Item2; + if(textUsername != data.User.Username) + { + ViewModels.ViewModelGame.HandleIncomingMsg(textUsername, textMsg); + } + //TODO display username and message in chat window Debug.WriteLine("[CLIENT] INCOMING MESSAGE!"); Debug.WriteLine("[CLIENT] User name: {0}\t User message: {1}", textUsername, textMsg); diff --git a/Client/ViewModels/ViewModelGame.cs b/Client/ViewModels/ViewModelGame.cs index d82df09..661e83f 100644 --- a/Client/ViewModels/ViewModelGame.cs +++ b/Client/ViewModels/ViewModelGame.cs @@ -21,13 +21,13 @@ namespace Client.ViewModels private Point currentPoint = new Point(); private Color color; - public ObservableCollection Messages { get; } = new ObservableCollection(); + public static ObservableCollection Messages { get; } = new ObservableCollection(); private dynamic _payload; - private string _username; + public string _username; - private string _message; + public string _message; public string Message { get @@ -121,6 +121,13 @@ namespace Client.ViewModels data.Client.SendMessage(JSONConvert.GetMessageToSend(JSONConvert.MESSAGE, _payload)); } + public static void HandleIncomingMsg(string username, string message) + { + Application.Current.Dispatcher.Invoke(delegate + { + Messages.Add($"{username}: {message}"); + }); + } public void LeaveGame(object sender, System.ComponentModel.CancelEventArgs e) { Debug.WriteLine("Leaving..."); diff --git a/Server/Models/ServerClient.cs b/Server/Models/ServerClient.cs index 843db9c..cfd2483 100644 --- a/Server/Models/ServerClient.cs +++ b/Server/Models/ServerClient.cs @@ -122,11 +122,15 @@ namespace Server.Models string textUsername = combo.Item1; string textMsg = combo.Item2; - Debug.WriteLine("[SERVERCLIENT] User name: {0}\t User message: {1}", textUsername, textMsg); + //Takes the data sent from the client, and then sets it in a data packet to be sent. + dynamic packet = new + { + username = textUsername, + message = textMsg + }; - // todo handle sending to all except this user the username and message to display in chat - serverCom.SendToLobby(ServerCommunication.INSTANCE.GetLobbyForUser(User),payload); - Debug.WriteLine("Payload has been sent!"); + //Sends the incomming message to be broadcast to all of the clients inside the current lobby. + serverCom.SendToLobby(serverCom.GetLobbyForUser(User), JSONConvert.GetMessageToSend(JSONConvert.MESSAGE, packet)); break; case JSONConvert.LOBBY: