diff --git a/ClientApp/Utils/Client.cs b/ClientApp/Utils/Client.cs index d3f015c..a2575a7 100644 --- a/ClientApp/Utils/Client.cs +++ b/ClientApp/Utils/Client.cs @@ -175,7 +175,7 @@ namespace ClientApp.Utils /// starts sending a message to the server /// /// the message to send - private void sendMessage(byte[] message) + public void sendMessage(byte[] message) { stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null); } diff --git a/ClientApp/Utils/DataParser.cs b/ClientApp/Utils/DataParser.cs index 6c1e4b7..d9feec1 100644 --- a/ClientApp/Utils/DataParser.cs +++ b/ClientApp/Utils/DataParser.cs @@ -15,6 +15,8 @@ namespace ClientApp.Utils public const string START_SESSION = "START SESSION"; public const string STOP_SESSION = "STOP SESSION"; public const string SET_RESISTANCE = "SET RESISTANCE"; + public const string NEW_CONNECTION = "NEW CONNECTION"; + public const string DISCONNECT = "DISCONNECT"; /// /// makes the json object with LOGIN identifier and username and password /// @@ -201,6 +203,24 @@ namespace ClientApp.Utils return ((dynamic)JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json))).data.worked; } + public static byte[] getNewConnectionJson(string user) + { + dynamic data = new + { + username = user + }; + return getJsonMessage(NEW_CONNECTION, data); + } + + public static byte[] getDisconnectJson(string user) + { + dynamic data = new + { + username = user + }; + return getJsonMessage(DISCONNECT, data); + } + } } diff --git a/ClientApp/ViewModels/MainWindowViewModel.cs b/ClientApp/ViewModels/MainWindowViewModel.cs index f30bf73..5ccf3cd 100644 --- a/ClientApp/ViewModels/MainWindowViewModel.cs +++ b/ClientApp/ViewModels/MainWindowViewModel.cs @@ -2,6 +2,7 @@ using ClientApp.Utils; using System; using System.Collections.Generic; +using System.ComponentModel; using System.Text; namespace ClientApp.ViewModels @@ -13,13 +14,22 @@ namespace ClientApp.ViewModels public ObservableObject SelectedViewModel { get; set; } public Client client { get; } + LoginViewModel loginViewModel; + public MainWindowViewModel(Client client) { this.InfoModel = new Info(); this.client = client; - LoginViewModel loginViewModel = new LoginViewModel(this); + loginViewModel = new LoginViewModel(this); SelectedViewModel = loginViewModel; this.client.SetLoginViewModel(loginViewModel); + App.Current.MainWindow.Closing += new CancelEventHandler(MainWindow_Closing); + } + + void MainWindow_Closing(object sender, CancelEventArgs e) + { + this.client.sendMessage(DataParser.getDisconnectJson(loginViewModel.Username)); + } } diff --git a/DoctorApp/Utils/Client.cs b/DoctorApp/Utils/Client.cs index 9ce05b7..4239fba 100644 --- a/DoctorApp/Utils/Client.cs +++ b/DoctorApp/Utils/Client.cs @@ -125,7 +125,7 @@ namespace DoctorApp.Utils this.MainViewModel.NewConnectedUser(DataParser.getUsernameFromResponseJson(payloadbytes)); break; case DataParser.DISCONNECT: - this.MainViewModel.NewConnectedUser(DataParser.getUsernameFromResponseJson(payloadbytes)); + this.MainViewModel.DisconnectedUser(DataParser.getUsernameFromResponseJson(payloadbytes)); break; default: Console.WriteLine($"Received json with identifier {identifier}:\n{Encoding.ASCII.GetString(payloadbytes)}"); diff --git a/DoctorApp/ViewModels/MainViewModel.cs b/DoctorApp/ViewModels/MainViewModel.cs index 27c8e69..8c1f3fd 100644 --- a/DoctorApp/ViewModels/MainViewModel.cs +++ b/DoctorApp/ViewModels/MainViewModel.cs @@ -10,7 +10,7 @@ namespace DoctorApp.ViewModels { class MainViewModel : ObservableObject { - public ObservableCollection Tabs { get; set; } + public ObservableCollection Tabs { get; set; } public int Selected { get; set; } public MainWindowViewModel MainWindowViewModel { get; set; } @@ -20,7 +20,7 @@ namespace DoctorApp.ViewModels { this.MainWindowViewModel = mainWindowViewModel; client = this.MainWindowViewModel.client; - Tabs= new ObservableCollection(); + Tabs= new ObservableCollection(); } public void NewConnectedUser(string username) @@ -37,7 +37,17 @@ namespace DoctorApp.ViewModels public void DisconnectedUser(string username) { - + App.Current.Dispatcher.Invoke((Action)delegate + { + foreach (ClientInfoViewModel item in Tabs) + { + if (item.Username == username) + { + Tabs.Remove(item); + break; + } + } + }); } } diff --git a/DoctorApp/Views/MainView.xaml b/DoctorApp/Views/MainView.xaml index 62a976d..af186b3 100644 --- a/DoctorApp/Views/MainView.xaml +++ b/DoctorApp/Views/MainView.xaml @@ -8,7 +8,7 @@ d:DesignHeight="450" d:DesignWidth="800"> - + diff --git a/DoctorApp/Views/MainWindow.xaml b/DoctorApp/Views/MainWindow.xaml index 5535982..c4d87d8 100644 --- a/DoctorApp/Views/MainWindow.xaml +++ b/DoctorApp/Views/MainWindow.xaml @@ -8,7 +8,7 @@ Title="MainWindow" Height="450" Width="800" WindowState="Maximized"> - + diff --git a/Server/Client.cs b/Server/Client.cs index ee32ef8..139df82 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -127,6 +127,9 @@ namespace Server Console.WriteLine($"set resistance worked is " + worked); //set resistance on doctor GUI break; + case DataParser.DISCONNECT: + communication.Disconnect(this); + break; default: Console.WriteLine($"Received json with identifier {identifier}:\n{Encoding.ASCII.GetString(payloadbytes)}"); break; diff --git a/Server/Communication.cs b/Server/Communication.cs index 31350ce..ce40142 100644 --- a/Server/Communication.cs +++ b/Server/Communication.cs @@ -49,6 +49,7 @@ namespace Server internal void Disconnect(Client client) { clients.Remove(client); + doctor.sendMessage(DataParser.getDisconnectJson(client.username)); } public void NewLogin(Client client)