示例#1
0
        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="pid"></param>
        /// <param name="command"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        private SocketRespons SendData(int pid, Command command, object data = null)
        {
            if (!userConnections.ContainsKey(pid))
            {
                throw new Exception("当前微信Id不在线。");
            }
            SocketRespons respons = null;
            string        key     = Guid.NewGuid().ToString();

            JObject obj = null;

            if (data != null)
            {
                obj = QNSerialize.DeserializeObject <JObject>(QNSerialize.SerializeObject(data));
            }
            else
            {
                obj = new JObject();
            }

            obj.Add("Code", (int)command);
            obj.Add("Callback", key);
            var encode = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(obj.ToString()));

            userConnections[pid].Connection.Send(encode);

            AutoResetEvent returnWaitSignal = new AutoResetEvent(false);
            ResponsHandlerCallBackDelegate SendReceiveDelegate = (result) =>
            {
                respons = result;
                returnWaitSignal.Set();
            };

            AppendIncomingPacketHandler(key, SendReceiveDelegate);
            if (!returnWaitSignal.WaitOne(30 * 1000))//三十秒超时
            {
                RemoveIncomingPacketHandler(key, SendReceiveDelegate);
                throw new QNException(26, "请求超时。", null);
            }
            RemoveIncomingPacketHandler(key, SendReceiveDelegate);
            if (respons.Error != 0)
            {
                throw new QNException((uint)respons.Error, respons.Message);
            }
            return(respons);
        }
示例#2
0
        private void WeChat_WeChatInitEvent(object sender, WeChat.Events.WeChatInitArgs e)
        {
            pid = e.PorcessId;
            ShowLog($"[日志]{e.Message}");
            while (!WeChat.CheckLoginState(e.PorcessId))
            {
                Thread.Sleep(1000);
            }
            var user = WeChat.GetLoginUser(e.PorcessId);

            if (user != null)
            {
                this.Dispatcher.BeginInvoke((Action) delegate()
                {
                    tb_UserInfo.Text  = QNSerialize.SerializeObject(user);
                    lb_Status.Content = "已登录";
                });
            }
            this.Dispatcher.BeginInvoke((Action) delegate()
            {
                Contacts.Clear();
            });
            WeChat.GetContactList(pid);
        }
示例#3
0
 /// <summary>
 /// 发送数据
 /// </summary>
 /// <param name="pid"></param>
 /// <param name="data"></param>
 private void SendData(int pid, object data)
 {
     if (!userConnections.ContainsKey(pid))
     {
         throw new Exception("当前微信Id不在线。");
     }
     userConnections[pid].Connection.Send(Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(QNSerialize.SerializeObject(data))));
 }