public void Logout() { _clientProtocol.Disconnect(); CurrentUser = null; }
private void OnApplyRegister(object sender, RoutedEventArgs e) { try { //检查注册信息 if (userHead.Source == null) { throw new Exception("请上传头像!"); } var validateResult = new AccountValidationRule().Validate(RegisteredAccount, null); if (validateResult.IsValid == false) { throw new Exception(validateResult.ErrorContent.ToString()); } if (repeatedRegisteredPasswordBox.Password != registeredPasswordBox.Password) { throw new Exception(PasswordValidattion.GetErrorMessage(repeatedRegisteredPasswordBox)); } validateResult = new PasswordValidationRule().Validate(RegisteredPassword, null); if (validateResult.IsValid == false) { throw new Exception(validateResult.ErrorContent.ToString()); } validateResult = new NameValidationRule().Validate(RegisteredName, null); if (validateResult.IsValid == false) { throw new Exception(validateResult.ErrorContent.ToString()); } //连接网络 if (_protocol.IsConnected == false) { _protocol.Connect(); } if (_protocol.IsConnected == false) { return; } //填写消息 var msg = new MessageRegisterRequest() { CallID = DateTime.Now.GetHashCode(), UserID = Account.GetHashCode(), Account = RegisteredAccount, Password = RegisteredPassword, Name = RegisteredName, Introduce = RegisteredIntroduce, User = _protocol.UserList.First().Value }; var encoder = new BmpBitmapEncoder(); var bitmapSource = this.userHead.Source as BitmapSource; encoder.Frames.Add(BitmapFrame.Create(bitmapSource)); using (var stream = new MemoryStream()) { encoder.Save(stream); msg.HeadsculptStream = stream.GetBuffer(); } //发送 string error = null; bool result = false; using (var signal = new ManualResetEventSlim()) { EventHandler <Message> waitBack = (object sd, Message respondsMsg) => { if (msg.CallID != respondsMsg.CallID) { return; } if (respondsMsg.MessageID == MessageID.MI_REGISTER_FAILED) { error = (respondsMsg as MessageRegisterFailed).Error; signal.Set(); } else if (respondsMsg.MessageID == MessageID.MI_REGISTER_SUCCESSFULLY) { var realMsg = (respondsMsg as MessageRegisterSuccessfully); result = true; signal.Set(); } }; this._protocol.MessageEvent += waitBack; this._protocol.SendBigMsg(msg); if (signal.Wait(2000) == false) { error = "服务器没有响应,请重试!"; } this._protocol.MessageEvent -= waitBack; } if (result == false) { throw new Exception(error); } MessageWindow.Show(this, "注册成功,赶紧登录去吧!"); this.OnLoginState(); } catch (Exception ex) { MessageWindow.Show(this, ex.Message); } _protocol.Disconnect(); }