Example #1
0
        public ISubscription SubscribeAsync <T>(Func <T, MessageContext, Task> subscribeMethod, Action <ISubscriptionConfigurationBuilder> configuration = null)
        {
            var subscribeWrapper = new Func <T, MessageContext, Task>((arg1, context) =>
            {
                var subscribeTask = subscribeMethod(arg1, context);

                subscribeTask.ContinueWith(task =>
                {
                    if (task.IsFaulted == false)
                    {
                        MessageProcessedEvent?.Invoke();
                    }
                });

                return(subscribeTask);
            });

            return(_client.SubscribeAsync(subscribeWrapper, configuration));
        }
Example #2
0
        public void RefreshMsg(object sender, TEventArgs <List <AddMsg> > e)
        {
            try
            {
                List <AddMsg>                msg      = e.Result;
                string                       dicitems = "FromUserName,FromUserNikeName,FromMemberName,FromMemerNikeName,ToUserName,ToUserNikeName,AtNikeNames,Message,MsgTime";
                string[]                     itemArr  = dicitems.Split(',');
                List <wxMessageClass>        messages = new List <wxMessageClass>();
                Dictionary <string, Contact> AllUsers = contactDict;
                //List<string> msgs = tbox.Lines.ToList();
                //List<ListViewItem> msgs = new List<ListViewItem>();
                for (int i = 0; i < msg.Count; i++)
                {
                    AddMsg wxmsg = msg[i];
                    if (wxmsg.MsgType != MsgType.MM_DATA_TEXT)
                    {
                        continue;
                    }
                    string fromName = wxmsg.FromUserName;
                    string NickName = fromName;
                    if (AllUsers.ContainsKey(fromName))//联系人发来的
                    {
                        ////if(fromName.StartsWith("@@"))
                        ////{
                        ////    client.GetBatchGetContactAsync(string.Join(",",AllUsers[fromName].MemberDict.Keys.ToArray()),fromName);
                        ////}
                        NickName = AllUsers[fromName].NickName;
                        if (AllUsers[fromName].MemberList.Count > 0) //是群
                        {
                            if (!AllUsers[fromName].DisplayNikeName) //所有的群都显示昵称
                            {
                                client.GetBatchGetContactAsync(string.Join(",", AllUsers[fromName].MemberList.Select(a => a.UserName)), fromName);
                                AllUsers[fromName].DisplayNikeName = true;
                            }
                        }
                    }
                    else
                    {
                        client.GetBatchGetContactAsync(fromName);
                        continue;
                    }
                    string ToName     = wxmsg.ToUserName;
                    string ToNameNike = null;
                    if (AllUsers.ContainsKey(ToName))
                    {
                        ToNameNike = AllUsers[ToName].NickName;
                    }

                    //string[] strMsg = new string[5];
                    wxMessageClass wmsg = new wxMessageClass();

                    if (wxmsg.MsgType == MsgType.MM_DATA_IMG)
                    {
                        string strImg = wxmsg.Content;
                    }

                    if (fromName.StartsWith("@@"))
                    {
                        string[] content = wxmsg.Content.Split(new string[] { ":<br/>" }, StringSplitOptions.RemoveEmptyEntries);
                        content[1] = content[1].Replace("<br/>", "\r\n");
                        string memName = "";
                        if (1 == 2 && AllUsers.ContainsKey(content[0]))
                        {
                            memName = AllUsers[content[0]].DisplayName;
                            if (string.IsNullOrEmpty(memName))
                            {
                                memName = AllUsers[content[0]].NickName;
                            }
                        }
                        else
                        {
                            if (AllUsers[fromName].MemberDict.ContainsKey(content[0]))
                            {
                                memName = AllUsers[fromName].MemberDict[content[0]].DisplayName;
                            }
                            if (string.IsNullOrEmpty(memName) && AllUsers.ContainsKey(fromName) && AllUsers[fromName].MemberDict.ContainsKey(content[0]))
                            {
                                memName = AllUsers[fromName].MemberDict[content[0]].NickName;
                            }
                        }

                        //strMsg = string.Format("[{0}]{1}:{2}", NickName, memName, string.Join("",content.Skip(1).ToArray()));
                        //strMsg = new string[] { DateTime.Now.ToShortTimeString(), NickName, memName, "", string.Join("", content.Skip(1).ToArray()) };
                        wmsg.FromUserNam        = fromName;
                        wmsg.FromNikeName       = NickName;
                        wmsg.FromMemberUserName = content[0];
                        wmsg.FromMemberNikeName = memName;
                        wmsg.CreateTime         = wxmsg.CreateTime;
                        wmsg.Msg    = content[1];
                        wmsg.OrgMsg = wxmsg.Content;
                        Regex           regTr  = new Regex(@"@(.*?) ");
                        List <string>   atlist = new List <string>();
                        MatchCollection mcs    = regTr.Matches(wmsg.Msg);
                        for (int mi = 0; mi < mcs.Count; mi++)
                        {
                            atlist.Add(mcs[mi].Value.Trim().Replace("@", ""));
                        }
                        wmsg.AtMemberNikeName = atlist.ToArray();
                        if (wmsg.AtMemberNikeName != null && wmsg.AtMemberNikeName.Length == 1)
                        {
                            var matchnicks = AllUsers[fromName].MemberDict.Where(a => a.Value.DisplayName == wmsg.AtMemberNikeName[0] || a.Value.NickName == wmsg.AtMemberNikeName[0]);
                            foreach (var mem in matchnicks)
                            {
                                if (mem.Value.UserName == client.user.UserName)
                                {
                                    wmsg.IsAtToMe = true;
                                    break;
                                }
                            }
                        }
                    }
                    else
                    {
                        wmsg.FromUserNam        = fromName;
                        wmsg.FromNikeName       = NickName;
                        wmsg.FromMemberUserName = null;
                        wmsg.FromMemberNikeName = null;
                        wmsg.ToUserName         = ToName;
                        wmsg.ToNikeName         = ToNameNike;
                        wmsg.CreateTime         = wxmsg.CreateTime;
                        wmsg.Msg    = wxmsg.Content.Replace("<br/>", "\r\t");
                        wmsg.OrgMsg = wxmsg.Content;
                    }
                    messages.Add(wmsg);
                    //msgs.Add(new ListViewItem(strMsg));
                }
                if (messages.Count > 0)
                {
                    Task.Run(() => {
                        MsgProcessCompleted?.Invoke(this, messages);
                    });
                    Task.Run(() => {
                        InjectBuffs(messages);
                    });
                    //?.Invoke(this, messages);
                    ////if(MsgProcessCompleted_ForExtralInterfaceInvoke!=null)
                    ////{
                    ////    MsgProcessCompleted_ForExtralInterfaceInvoke.ForEach(
                    ////        a => {
                    ////            a.Invoke(this, messages);
                    ////        }
                    ////        );
                    ////}
                }
            }
            catch (Exception ce)
            {
                MessageBox.Show(string.Format("{0}:{1}", ce.Message, ce.StackTrace));
            }
            //tbox.Lines = msgs.ToArray();
            //tbox.Focus();
            //tbox.Select(tbox.Text.Length, 0);
            //tbox.ScrollToCaret();
        }