From 33574a7b23a854e51a5bf07dd56c7acbc6281dc3 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Tue, 20 Oct 2020 17:43:28 +0200 Subject: [PATCH] [FIX] fixed server client message structure --- Client/Client.cs | 9 ++++++--- Client/ViewModels/ViewModel.cs | 2 +- Client/Views/LoginScreen.xaml.cs | 4 ++-- Client/Views/MainWindow.xaml | 2 +- Client/Views/MainWindow.xaml.cs | 2 +- Server/Models/ServerClient.cs | 16 ++++++++++++---- Server/Models/ServerCommunication.cs | 2 +- SharedClientServer/JSONConvert.cs | 23 ++++++++++++++++++----- 8 files changed, 42 insertions(+), 18 deletions(-) diff --git a/Client/Client.cs b/Client/Client.cs index 5152afe..d6e1220 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -16,11 +16,11 @@ namespace Client private int totalBufferReceived = 0; public int Port = 5555; public bool Connected = false; - //TODO send login packet to server with ClientServerUtil.createpayload(0x01,dynamic json with username) - public string Username { get; } + private string username; - public Client() + public Client(string username) { + this.username = username; this.tcpClient = new TcpClient(); Debug.WriteLine("Starting connect to server"); tcpClient.BeginConnect("localhost", Port, new AsyncCallback(OnConnect), null); @@ -31,6 +31,7 @@ namespace Client Debug.Write("finished connecting to server"); this.tcpClient.EndConnect(ar); this.stream = tcpClient.GetStream(); + SendMessage(JSONConvert.ConstructUsernameMessage(username)); this.stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnReadComplete),null); } @@ -100,11 +101,13 @@ namespace Client public void SendMessage(byte[] message) { + Debug.WriteLine("[CLIENT] sending message " + Encoding.ASCII.GetString(message)); stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWriteComplete), null); } private void OnWriteComplete(IAsyncResult ar) { + Debug.WriteLine("[CLIENT] finished writing"); stream.EndWrite(ar); } } diff --git a/Client/ViewModels/ViewModel.cs b/Client/ViewModels/ViewModel.cs index abb89e3..c66fd15 100644 --- a/Client/ViewModels/ViewModel.cs +++ b/Client/ViewModels/ViewModel.cs @@ -18,7 +18,7 @@ namespace Client _model = new Model(); ButtonCommand = new RelayCommand(() => { - Client client = new Client(); + }, true); _lobbies = new List(); diff --git a/Client/Views/LoginScreen.xaml.cs b/Client/Views/LoginScreen.xaml.cs index 76a20f0..7dfe537 100644 --- a/Client/Views/LoginScreen.xaml.cs +++ b/Client/Views/LoginScreen.xaml.cs @@ -26,8 +26,8 @@ namespace Client.Views private void Button_EnterUsername(object sender, RoutedEventArgs e) { - User user = new User(usernameTextbox.Text, 0, false); - Client client = new Client(); + User user = new User(usernameTextbox.Text); + Client client = new Client(user.Username); data.User = user; data.Client = client; diff --git a/Client/Views/MainWindow.xaml b/Client/Views/MainWindow.xaml index b3221df..68bf586 100644 --- a/Client/Views/MainWindow.xaml +++ b/Client/Views/MainWindow.xaml @@ -47,7 +47,7 @@