/// <summary> /// /// </summary> public void Login() { /// <summary> /// when the user is logging-in he get all the names of the registreted users, and their status of connection (available, on-game, on-chat...). any update of the status of other user, or of new user that registreted will appear /// </summary> /// <param name="usersWithStatus"></param> ChatHubProxy.On("UpdateAllUsersNamesAndConnectionStatus", (List <UserWithStatus> usersWithStatus) => { Application.Current.Dispatcher.BeginInvoke(new Action(() => { usersWithStatus = usersWithStatus.Where(x => x.UserName != UserName).ToList(); UsersNamesWithStatus = ConvertListToObservableCollection(usersWithStatus); })); }); try { if (string.IsNullOrWhiteSpace(UserName) || string.IsNullOrWhiteSpace(Password)) { MessageBox.Show("You must enter a User Name and a Password, do not leave the fields empty"); return; } User user = new User { UserName = UserName, Password = Password }; Response response = ChatHubProxy.Invoke <Response>("ValidateLogin", user).Result; if (response.IsOk1) { EnableButtonLogin = false; // after login, can't do another login EnableButtonRegister = false; // after login, can't do registreting MessageBox.Show(response.Message1); } else if (!response.IsOk1) { MessageBox.Show(response.Message1); } } catch (Exception e) { Logger.LOG.WriteToLog("Failed login in ChatViewModel" + Environment.NewLine + DateTime.Now.ToString() + Environment.NewLine + "Exception details: " + e.ToString() + "," + e.GetType().FullName + Environment.NewLine); } }
/// <summary> /// when a client connect to the server, he is registering to events from the server by "ChatHubProxy.On..." /// </summary> public void ConnectToServer() { /// <summary> /// getting a request from other user to chat with him /// </summary> /// <param name="senderName"></param> ChatHubProxy.On("ChatRequest", (string senderName) => { Application.Current.Dispatcher.BeginInvoke(new Action(() => { bool approve = ApproveChatInvitation(senderName); ChatHubProxy.Invoke("ApproveToChat", approve, UserName, senderName); // send a bolean to the server and than to the other user, if he wants to chat with him or not })); }); /// <summary> /// getting a request from other user to play backgammon with him /// </summary> /// <param name="senderName"></param> ChatHubProxy.On("GameRequest", (string senderName) => { Application.Current.Dispatcher.BeginInvoke(new Action(() => { bool approve = ApproveGameInvitation(senderName); ChatHubProxy.Invoke("ApproveToGame", approve, UserName, senderName); // // send a bolean to the server and than to the other user, if he wants to play bacgammon with him or not })); }); /// <summary> /// get an approve or disapprove to his own chat request from the server (the server get it from the other user) /// </summary> /// <param name="approve">boolean, that approve the chat request from the other user</param> /// <param name="otherUser">string, the name of the other user on the chat</param> ChatHubProxy.On("responseOfChatRequestToSender", (bool approve, string otherUser) => { Application.Current.Dispatcher.BeginInvoke(new Action(() => { if (approve) { MessageBox.Show("Chat will start!"); ChatRoom chatRoom = new ChatRoom(UserName, otherUser, ChatHubProxy); chatRoom.Show(); } else { MessageBox.Show("Some other time..."); } })); }); /// <summary> /// get an approve or disapprove to his own game request from the server (the server get it from the other user) /// </summary> /// <param name="approve">boolean, that approve the game request from the other user</param> /// <param name="otherUser">string, the name of the other user on the game</param> /// <param name="startGameUser">string, the user that sent the request, he will be the one that starts the game -> the "Red"</param> ChatHubProxy.On("responseOfGameRequestToSender", (bool approve, string otherUser, string startGameUser) => { Application.Current.Dispatcher.BeginInvoke(new Action(() => { if (approve) { string msg = "Game will start!\nPlease click the disc and then the cube in order to move a disc,\nTo get an eaten cube back to the board click only on the cube,\n"; if (UserName.Equals(startGameUser)) { msg += "You are the Red,\nYou start!"; } else if (otherUser.Equals(startGameUser)) { msg += "You are the Black,\nThe Red starts"; } MessageBox.Show(msg); Application.Current.MainWindow.Content = new Game(UserName, otherUser, ChatHubProxy, startGameUser); } else { MessageBox.Show("Some other time..."); } })); }); HubConnection.Start().Wait(); }