public IMHttpContext Send(Base_Upload ul, Action <IMHttpContext> callback) { IMHttpContext ctx = new IMHttpContext(); ctx.url = url; ctx.ul = ul; ctx.retry = 2; ctx.callback = callback; sendQueue.Enqueue(ctx); return(ctx); }
protected override bool OnHeatbeatDone(IMHttpContext ctx) { if (ctx.code != 0) { Debug.Log("获取私聊失败"); im.SetOffline(); return(false); } try { if (!ctx.response.Contains("private")) { im.SetOffline(); return(false); } return(SyncPrivateMsgs(ctx.response["private"].AsBonDocument) > 0); } catch (Exception e) { Debug.Log(e); im.SetOffline(); } return(false); }
protected override bool OnHeatbeatDone(IMHttpContext ctx) { ClearInvalidChannels(); if (ctx.code != 0) { Debug.Log("获取频道新聊天失败"); ClearAll(); return(false); } BonDocument chds = ctx.response.GetBonDocument("channels"); if (chds == null) { ClearAll(); return(false); } bool haveMsg = false; for (int i = channels.Count; --i >= 0;) { IMChannel ch = im.GetChannel(channels[i]); try { if (!chds.Contains(ch.id)) { ch.SetOffline(); continue; } if (SyncChannelMsgs(chds[ch.id].AsBonDocument, ch) > 0) { haveMsg = true; } } catch (Exception e) { Debug.Log(e); ch.SetOffline(); continue; } } return(haveMsg); }
protected override void OnSendDone(IMMsgForSend msg, IMHttpContext ctx) { if (ctx.code != 0) { Debug.Log("发送聊天致命错误"); msg.Distributed = false; im.SetOffline(); return; } msg.Sent = true; try { if (!ctx.response.Contains("private")) { im.SetOffline(); return; } SyncPrivateMsgs(ctx.response["private"].AsBonDocument); } catch (Exception e) { Debug.Log(e); im.SetOffline(); } }
protected override void OnSendDone(IMMsgForSend msg, IMHttpContext ctx) { IMChannel ch = im.GetChannel(msg.recId); if (ch == null) { return; } ClearInvalidChannels(); if (ctx.code < 0) { Debug.Log("发送聊天致命错误"); msg.Distributed = false; ClearAll(); return; } if (ctx.code > 0) { Debug.Log("发送聊天错误"); msg.Distributed = false; ch.SetOffline(); return; } msg.Sent = true; try { BonDocument chd = ctx.response.GetBonDocument("channel"); if (chd == null) { msg.Distributed = false; ch.SetOffline(); return; } SyncChannelMsgs(chd, ch); } catch (Exception e) { Debug.Log(e); msg.Distributed = false; ch.SetOffline(); } }
protected abstract bool OnHeatbeatDone(IMHttpContext ctx);
protected abstract void OnSendDone(IMMsgForSend msg, IMHttpContext ctx);
IEnumerator _Send() { IMHttpContext ctx = sendQueue.Dequeue(); BonDocument request = new BonDocument(); ctx.request = request; request["ul"] = ctx.ul.GetType().Name; BonDocument p = BonUtil.ToBon(ctx.ul, null, null) as BonDocument; request["p"] = p; ctx.sno = (++snoSeed); request["sno"] = ctx.sno; IMMe me = IM.Instance.me; if (me != null) { p["uid"] = me.id; if (me.auth != null) { request["auth"] = me.auth; } } byte[] postdata = ctx.request.ToBonBytes(); postdata = EncryptDecrypt.Encrypt(postdata); while (ctx.retry >= 0) { #if HTTP_DEBUG_LOG Debug.Log("发送数据{" + postdata.Length + "}: " + ctx.request.ToJsonString()); Debug.Log("到: " + ctx.url); #endif ctx.sendTime = TimeUtil.UnixTimestampNow(); float lastProgress = 0; double timeout = TimeUtil.UnixTimestampNow() + 10; bool isTimeout = false; using (WWW www = new WWW(ctx.url, postdata, header)) { while (!www.isDone) { double now = TimeUtil.UnixTimestampNow(); if (now >= timeout) { isTimeout = true; break; } if (lastProgress != www.progress) { lastProgress = www.progress; timeout = now + 20; } yield return(null); } if (destroyed) { busy = false; yield break; } if (isTimeout) { ctx.error = "UI.网络错误"; ctx.code = -1; Debug.LogError("访问超时"); } else if (www.error != null) { ctx.error = "UI.网络错误"; ctx.code = -1; Debug.LogError("Err:" + www.error); } else { byte[] data = www.bytes; try { if (data[0] == 31) { try { data = GZip.Decompress(data); } catch (Exception) { } } ctx.response = BonDocument.FromBonBytes(data); #if HTTP_DEBUG_LOG Debug.Log("收到数据: " + ctx.response); #endif ctx.code = ctx.response.GetInt("code"); ctx.error = null; ctx.retry = 0; } catch (Exception e) { ctx.error = "数据解析错误"; Debug.LogError("下行数据解析错误 " + e.ToString()); } } } ctx.retry--; } busy = false; if (ctx.callback != null) { ctx.callback(ctx); } if (OnResult != null) { OnResult(ctx); } }