示例#1
0
        async Task <bool> DetectUser()
        {
            string strError = "";

            EnableControls(false);
            try
            {
                using (MessageConnectionCollection _channels = new MessageConnectionCollection())
                {
                    _channels.Login += _channels_Login;

                    MessageConnection connection = await _channels.GetConnectionAsyncLite(
                        this.textBox_url.Text,
                        "");

                    CancellationToken cancel_token = _cancel.Token;

                    string            id      = Guid.NewGuid().ToString();
                    GetMessageRequest request = new GetMessageRequest(id,
                                                                      "",
                                                                      "gn:<default>", // "" 表示默认群组
                                                                      "",
                                                                      "",
                                                                      "",
                                                                      "",
                                                                      "",
                                                                      0,
                                                                      1);
                    GetMessageResult result = await connection.GetMessageAsyncLite(
                        request,
                        new TimeSpan(0, 1, 0),
                        cancel_token);

                    if (result.Value == -1)
                    {
                        strError = "检测用户时出错: " + result.ErrorInfo;
                        goto ERROR1;
                    }

                    return(true);
                }
            }
            catch (MessageException ex)
            {
                if (ex.ErrorCode == "Unauthorized")
                {
                    strError = "以用户名 '" + ex.UserName + "' 登录时, 用户名或密码不正确";
                    goto ERROR1;
                }
                if (ex.ErrorCode == "HttpRequestException")
                {
                    strError = "dp2MServer URL 不正确,或 dp2MServer 尚未启动";
                    goto ERROR1;
                }
                strError = ex.Message;
                goto ERROR1;
            }
            catch (AggregateException ex)
            {
                strError = MessageConnection.GetExceptionText(ex);
                goto ERROR1;
            }
            catch (Exception ex)
            {
                strError = ex.Message;
                goto ERROR1;
            }
            finally
            {
                EnableControls(true);
            }
ERROR1:
            this.Invoke(new Action(() =>
            {
                MessageBox.Show(this, strError);
            }));
            return(false);
        }