/// <summary> /// 引发接收消息事件. /// </summary> /// <param name="e"></param> protected void OnReciveMessage(QQMessageEventArgs e) { if (ReciveMessage != null) { ReciveMessage(this, e); } }
//{"retcode":0,"result":[{"poll_type":"message","value":{"msg_id":21273,"from_uin":2684355364,"to_uin":496063973,"msg_id2":105299,"msg_type":9,"reply_ip":176756443,"time":1386733521,"content":[["font",{"size":15,"color":"000000","style":[0,0,0],"name":"\u5B8B\u4F53"}],"strlist\u6492\u65E6\u53D1\u8069\r "]}},{"poll_type":"message","value":{"msg_id":21271,"from_uin":2684355364,"to_uin":496063973,"msg_id2":105297,"msg_type":9,"reply_ip":176756443,"time":1386733510,"content":[["font",{"size":15,"color":"000000","style":[0,0,0],"name":"\u5B8B\u4F53"}],"str\u5012\u8428\u7A7A\u95F4 "]}},{"poll_type":"message","value":{"msg_id":21270,"from_uin":2684355364,"to_uin":496063973,"msg_id2":105296,"msg_type":9,"reply_ip":176756443,"time":1386733508,"content":[["font",{"size":15,"color":"000000","style":[0,0,0],"name":"\u5B8B\u4F53"}],"str\u5012\u8428\u7A7A\u95F4 "]}},{"poll_type":"message","value":{"msg_id":21269,"from_uin":2684355364,"to_uin":496063973,"msg_id2":105295,"msg_type":9,"reply_ip":176756443,"time":1386733506,"content":[["font",{"size":15,"color":"000000","style":[0,0,0],"name":"\u5B8B\u4F53"}],"str\u5012\u8428\u7A7A\u95F4 "]}},{"poll_type":"message","value":{"msg_id":21272,"from_uin":2684355364,"to_uin":496063973,"msg_id2":105292,"msg_type":9,"reply_ip":176756443,"time":1386733512,"content":[["font",{"size":15,"color":"000000","style":[0,0,0],"name":"\u5B8B\u4F53"}],"str\u5012\u8428\u7A7A\u95F4 "]}},{"poll_type":"message","value":{"msg_id":21268,"from_uin":2684355364,"to_uin":496063973,"msg_id2":105291,"msg_type":9,"reply_ip":176756443,"time":1386733499,"content":[["font",{"size":15,"color":"000000","style":[0,0,0],"name":"\u5B8B\u4F53"}],"str\u5012\u8428\u7A7A\u95F4 "]}}]} public static List <QQMessageEventArgs> Parse(string text) { List <QQMessageEventArgs> list = new List <QQMessageEventArgs>(); JSONObject obj = JSONConvert.DeserializeObject(text); Debug.WriteLine(obj.GetString("result")); JSONArray array = obj.GetJSONArray("result"); foreach (JSONObject item in array) { JSONObject newobj = item.GetJSONObject("value"); QQMessageEventArgs m = new QQMessageEventArgs(text); switch (item.GetString("poll_type")) { case "tips": m = null; break; case "message": m.PollMsgType = PollType.Friend; m.Data = GetFriendMessage(newobj); break; case "group_message": m.PollMsgType = PollType.Group; m.Data = GetGroupMessage(newobj); break; case "kick_message": m.PollMsgType = PollType.KickMessage; m.Data = GetKickMessage(newobj); break; case "system_message": case "sys_g_msg": m.PollMsgType = PollType.System; m = null; break; default: m = null; break; } if (m != null) { list.Add(m); } } return(list); }
public static QQMessageEventArgs Parse2(string text) { string source = text; QQMessageEventArgs args = new QQMessageEventArgs(text); text = text.Replace("{", ""); text = text.Replace("]", ""); text = text.Replace("[", ""); text = text.Replace("}", ""); text = text.Replace("\"", ""); string[] array = text.Split(','); if (array.Length > 2) { string[] item = array[0].Split(':'); if (item.Length > 1) { args.Recode = int.Parse(item[1]); } string[] a = array[1].Split(':'); if (a.Length > 2) { switch (a[2].ToLower()) { case "tips": args.PollMsgType = PollType.Tips; Loger.WriteLog(string.Format("Tips :{0}", text)); args = null; break; case "message": args.PollMsgType = PollType.Friend; args.Data = GetFriendMessage(array, 2); break; case "group_message": args.PollMsgType = PollType.Group; Loger.WriteLog(string.Format("Group :{0}", text)); args.Data = GetGroupMessage(array, 2); break; case "kick_message": args.PollMsgType = PollType.KickMessage; args.Data = GetKiMessage(array, 2); break; case "system_message": case "sys_g_msg": Loger.WriteLog(string.Format("system_message :{0}", text)); break; case "buddies_status_change": args.PollMsgType = PollType.StatusChange; args.Data = GetStatusChangerMessage(array, 2); break; default: Loger.WriteLog(string.Format("接收到 other:{0}", text)); args = null; break; } } else { Loger.WriteLog("接收消息出现错误,消息返回值:" + source); } } return(args); }
/// <summary> /// 监听消息. /// </summary> private void ListenMessage() { try { while (true) { if (_KissOnline) { //QQ 掉线了. Thread.Sleep(500); continue; } StringBuilder sb = new StringBuilder(); sb.Append("r=%7B%22clientid%22%3A%22"); sb.Append(_ClientID); sb.Append("%22%2C%22psessionid%22%3A%22"); sb.Append(_PsessionID); sb.Append("%22%2C%22key%22%3A0%2C%22ids%22%3A%5B%5D%7D&clientid="); sb.Append(_ClientID); sb.Append("&psessionid="); sb.Append(_PsessionID); _HttpWeb.Referer = HttpText.PollReferer; string text = _HttpWeb.PollWebRequest("http://d.web2.qq.com/channel/poll2", sb.ToString(), Encoding.UTF8); try { if (text.IndexOf("result") > -1) { List <QQMessageEventArgs> list = QQMessageEventArgs.Parse(text); foreach (var item in list) { if (item.PollMsgType == PollType.KickMessage) { _KissOnline = true; } OnReciveMessage(item); } } else if (text.IndexOf("\"retcode\":102") > -1) { // 没有接收到任何消息. } else if (text.IndexOf("\"retcode\":121") > -1) { ReLogin(); //QQ掉线了 string temp = string.Format("接收错误:\r\nQQ号码:{0} \r\nPsessionID:{1}\r\nPTWebQQ:{2}\r\nVFWebQQ:{3}", _MyQQNumber, _PsessionID, _PTWebQQ, _VFWebQQ); Loger.WriteLog(temp); } else if (text.IndexOf("\"retcode\":116") > -1) { //改变Cookie ReLogin(); Loger.WriteLog("接收错误116:" + text); } else { Loger.WriteLog("其他接收错误:" + text); } } catch (Exception e) { Loger.WriteLog(e); } Thread.Sleep(500); } } catch (Exception ex) { Loger.WriteLog(ex); } }