private byte[] GetMsgUpdate(ulong userId) { ChatUpdateMsg chatUpdate = new ChatUpdateMsg(); if (_newMsgs.ContainsKey(userId)) { chatUpdate.Messages.AddRange(_newMsgs[userId].Select(x => x.ToNetworkModel())); if (_newMsgs.TryGetValue(userId, out Queue <ChatObjMsg> updates)) { updates.Clear(); } } return(chatUpdate.ToByteArray()); }
private IEnumerator ChatCoroutine() { ChatAppMsg msg = new ChatAppMsg(); msg.Request = new ChatRequest(); msg.Request.ListRequest = true; var req = _api.App.SendAppMsg(_info, msg.ToByteArray(), AppMsgRecipient.PROVIDER); yield return(req.WaitCoroutine()); if (req.HasException) { throw req.Exception; } if (req.Result != null) { ChatAppMsg response = ChatAppMsg.Parser.ParseFrom(req.Result); if (response == null || response.List == null) { throw new ChatAppException("Unknown response."); } _chats = response.List.List .Select(x => new ChatObj(x)) .ToDictionary(x => x.User1 == _userId ? x.User2 : x.User1, x => x); yield return(null); } else { _chats = new Dictionary <ulong, ChatObj>(); } _recentBlock.SetChatList(_chats.Select(x => x.Value).ToList()); yield return(null); while (true) { EventDataMsg updateReq = new EventDataMsg(); updateReq.AppId = APP_ID; updateReq.EventType = (uint)ChatAppEventType.GET_UPDATE; var updateReqService = _api.Event.SendEvent(updateReq, EventRecipient.PROVIDER); yield return(updateReqService.WaitCoroutine()); if (updateReqService.HasException) { yield return(new WaitForSeconds(UPDATE_INTERVAL_SEC)); continue; } ChatUpdateMsg updateMsg = ChatUpdateMsg.Parser.ParseFrom(updateReqService.Result); if (updateMsg == null) { yield return(new WaitForSeconds(UPDATE_INTERVAL_SEC)); continue; } List <ChatObjMsg> msgs = updateMsg.Messages .Select(x => new ChatObjMsg(x)) .ToList(); Update(msgs); yield return(new WaitForSeconds(UPDATE_INTERVAL_SEC)); } }