예제 #1
0
파일: EaseHelper.cs 프로젝트: wuearl/imPC
        /// <summary>
        /// 环信登录
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="password">密码</param>
        /// <param name="callError">报错信息</param>
        public void login(string username, string password, EMCallError callError)
        {
            Console.WriteLine("进来了");
            Task t = new Task(() =>
            {
                relogin     = false;
                EMError err = client.login(username, password);
                callError(err);
            });

            t.Start();
        }
예제 #2
0
파일: EaseHelper.cs 프로젝트: wuearl/imPC
 /// <summary>
 /// 与环信服务器断开连接
 /// </summary>
 /// <param name="error"></param>
 private void onDisconnect(EMError error)
 {
     ThreadPool.QueueUserWorkItem((o) =>
     {
         Console.WriteLine("em----------------------------------------Discont");
         if (error.errorCode == EMErrorCode.SERVER_UNKNOWN_ERROR)
         {
             EMError err = client.login(SettingMenager.shard.userID, SettingMenager.shard.userID);
             if (err.errorCode == EMErrorCode.EM_NO_ERROR)
             {
             }
             else
             {
                 MainFrm main = (MainFrm)Application.OpenForms["MainFrm"];
                 if (main == null)
                 {
                     if (relogin)
                     {
                         return;
                     }
                     relogin = true;
                     MessageBox.Show("未知错误,请重新登录");
                     return;
                 }
                 else
                 {
                     if (relogin)
                     {
                         return;
                     }
                     relogin = true;
                     main.BeginInvoke(new EventHandler((s, e) =>
                     {
                         MessageBox.Show("未知错误,请重新登录");
                     }));
                 }
             }
         }
     });
 }
        protected override void ProcessRequest(ref EMessage msg)
        {
            if (server.BlockMessage != null)
            {
                msg = new EMError(msg, server.BlockMessage, ErrorCode.ServerBlocked);
            }
            else if (msg.HasFlag(StatusCode.Login))
            {
                var  login = msg as EMLogin;
                bool hasLoggedIn;

                hasLoggedIn = server.MailCenter.IsOnline(login.Username);

                if (hasLoggedIn)
                {
                    server.PushOut(login.Username, $"You have been signed out because your account is logged in elsewhere.");
                }

                if (server.MailCenter.Contains(login.Username))
                {
                    var user = server.MailCenter.GetUser(login.Username, login.Password);

                    if (user != null)
                    {
                        TeleClientName = login.Username;
                        this.logger.SetOwner(TeleClientName);  // reset owner of logger
                        User        = user;
                        Token       = server.GenToken(TeleClientName);
                        msg         = new EMLoggedin(login, ClientName, user, Token);
                        msg.Status |= StatusCode.Duplex;  // sync time command
                        logger.SetOwner(TeleClientName);

                        Phase = ConnectionPhase.P2LoggedIn;
                        user.PostOffice.Activate(this);  // activate mailbox
                        user.IsOnline = true;

                        (msg as EMLoggedin).ServerTime = server.Now;  // set sync time
                    }
                    else
                    {
                        msg = new EMError(msg, $"incorrect username/password.", ErrorCode.IncorrectUsernameOrPassword);
                    }
                }
                else
                {
                    msg = new EMError(msg, $"user '{login.Username}' is not registered.", ErrorCode.UnregisteredUser);
                }
            }
            else if (User == null || !User.IsOnline)
            {
                msg = new EMError(msg, "please login first.");
            }
            else if (msg.HasFlag(StatusCode.Logout))
            {
                Logout();

                msg = new EMessage(msg, StatusCode.Ok);
            }
            else if (msg.HasFlag(StatusCode.Letter))
            {
                var letter = msg as EMLetter;

                try
                {
                    var result = server.MailCenter.Deliver(letter);
                    if (result == null)
                    {
                        msg = new EMessage(msg, StatusCode.Ok);  // Post, or SafePost
                    }
                    else
                    {
                        result.SetEnvelope(new Envelope(msg.ID));
                        msg = result;
                    }
                }
                catch (Exception ex)
                {
                    msg = new EMError(msg, ex.Message);
                }
            }
            else if (msg.HasFlag(StatusCode.Register))
            {
                if (msg.HasFlag(StatusCode.Entity))
                {
                    var entityNames = (msg as EMText).Text.Split(',');

                    foreach (var name in entityNames)
                    {
                        User.PostOffice.Register(name);
                    }

                    msg = new EMessage(msg, StatusCode.Ok);
                }
            }
            else
            {
                msg = new EMError(msg, $"current request is not supported by this server.", ErrorCode.InvalidOperation);
            }
        }
예제 #4
0
        private void dSkinButton1_Click(object sender, EventArgs e)
        {
            if (needClose)
            {
                this.BeginInvoke(new EventHandler((s, ee) =>
                {
                    this.Close();
                }));
                return;
            }
            ApplyForFriendSendModel model = new ApplyForFriendSendModel();

            model.target_user_id = userData.user_id;
            model.remark         = dSkinTextBox1.Text;
            HttpUitls.Instance.get <BaseReciveModel>("contacts/applyFor", model, (json) =>
            {
                if (json.code == 200)
                {
                    needClose = true;
                    this.BeginInvoke(new EventHandler((s, ee) =>
                    {
                        dSkinLabel3.Text      = "好友请求已发送";
                        dSkinTextBox1.Visible = false;
                        dSkinButton1.Text     = "关闭";
                    }));
                    EMError error = new EMError();
                    EaseHelper.shard.client.getContactManager().inviteContact(userData.user_id, null, error);
                }
                else
                {
                    MessageBox.Show(json.message);
                    if (json.message.Contains("请重新登录"))
                    {
                        MainFrm frm = (MainFrm)Application.OpenForms["MainFrm"];
                        if (frm != null)
                        {
                            frm.gotoLogin();
                            this.BeginInvoke(new EventHandler((s, ee) =>
                            {
                                this.Close();
                            }));
                        }
                    }
                }
            }, (code) =>
            {
                if (code > 500 && code < 503)
                {
                    MessageBox.Show("请重新登录");
                    MainFrm frm = (MainFrm)Application.OpenForms["MainFrm"];
                    if (frm != null)
                    {
                        frm.gotoLogin();
                        this.BeginInvoke(new EventHandler((s, ee) =>
                        {
                            this.Close();
                        }));
                    }
                }
            });
        }