diff --git a/Client/Client.cs b/Client/Client.cs index a86ba3e..4799d2e 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -49,6 +49,9 @@ namespace Client private void OnReadComplete(IAsyncResult ar) { + if (ar == null || (!ar.IsCompleted) || (!this.stream.CanRead) || !this.tcpClient.Client.Connected) + return; + int amountReceived = stream.EndRead(ar); if (totalBufferReceived + amountReceived > 1024) @@ -74,6 +77,7 @@ namespace Client expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0); } + ar.AsyncWaitHandle.WaitOne(); stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnReadComplete), null); } @@ -140,6 +144,13 @@ namespace Client // canvas data break; + case JSONConvert.RANDOMWORD: + //Flag byte for receiving the random word. + int lobbyId = JSONConvert.GetLobbyID(payload); + + if(data.Lobby?.ID == lobbyId) + ViewModels.ViewModelGame.HandleRandomWord(JSONConvert.GetRandomWord(payload)); + break; default: Debug.WriteLine("[CLIENT] Received weird identifier: " + id); break; diff --git a/Client/ViewModels/ViewModelGame.cs b/Client/ViewModels/ViewModelGame.cs index 661e83f..cbc6a49 100644 --- a/Client/ViewModels/ViewModelGame.cs +++ b/Client/ViewModels/ViewModelGame.cs @@ -1,11 +1,12 @@ -using Client.Views; +using Client.Views; using GalaSoft.MvvmLight.Command; using SharedClientServer; using System.Collections.ObjectModel; using System.ComponentModel; using System.Diagnostics; using System.Windows; +using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; using System.Windows.Shapes; @@ -25,6 +26,12 @@ namespace Client.ViewModels private dynamic _payload; + public static string Word + { + get; + set; + } + public string _username; public string _message; @@ -82,21 +89,10 @@ namespace Client.ViewModels colorSelected.B = window.ClrPcker_Background.SelectedColor.Value.B; color = colorSelected; } - + public ViewModelGame() { - if (_payload == null) - { - _message = ""; - - } - else - { - //_message = data.Message; - //_username = data.User.Username; - //Messages.Add($"{data.User.Username}: {Message}"); - } OnKeyDown = new RelayCommand(ChatBox_KeyDown); } @@ -121,6 +117,10 @@ namespace Client.ViewModels data.Client.SendMessage(JSONConvert.GetMessageToSend(JSONConvert.MESSAGE, _payload)); } + /* + * MISC make this a callback + * Handles the incoming chat message from another client. + */ public static void HandleIncomingMsg(string username, string message) { Application.Current.Dispatcher.Invoke(delegate @@ -128,13 +128,21 @@ namespace Client.ViewModels Messages.Add($"{username}: {message}"); }); } - public void LeaveGame(object sender, System.ComponentModel.CancelEventArgs e) + public void LeaveGame(object sender, CancelEventArgs e) { Debug.WriteLine("Leaving..."); data.Client.SendMessage(JSONConvert.ConstructLobbyLeaveMessage(data.Lobby.ID)); } - + /* + * MISC make this a callback + * Handles the random word that has been received from the server. + */ + public static void HandleRandomWord(string randomWord) + { + Debug.WriteLine("[CLIENT] Reached the handle random word method!"); + Word = "NegerPik"; + } } } - + diff --git a/Client/Views/GameWindow.xaml b/Client/Views/GameWindow.xaml index 1599e1f..dff9a66 100644 --- a/Client/Views/GameWindow.xaml +++ b/Client/Views/GameWindow.xaml @@ -36,6 +36,8 @@