private void OnMessageSend(ulong to, string msg) { ChatAppMsg chatMsg = new ChatAppMsg(); chatMsg.Request = new ChatRequest(); chatMsg.Request.SendRequest = new ChatSend(); chatMsg.Request.SendRequest.To = to; chatMsg.Request.SendRequest.Message = msg; _api.App.SendAppMsg(_info, chatMsg.ToByteArray(), AppMsgRecipient.PROVIDER).Exec(); }
private byte[] ListRequest(ulong userId, MsgContext ctx) { ChatAppMsg msg = new ChatAppMsg(); msg.List = new ChatList(); if (_chats.ContainsKey(userId)) { msg.List.List.AddRange(_chats[userId].Select(x => x.Value.ToNetworkModel())); if (_newMsgs.TryGetValue(userId, out Queue <ChatObjMsg> updates)) { updates.Clear(); } } return(msg.ToByteArray()); }
public byte[] HandleMessage(byte[] data, int size, MsgContext ctx) { if (ctx.senderType != SenderType.USER) { throw new ChatAppException("Server can not use this app."); } ChatAppMsg appMsg = ChatAppMsg.Parser.ParseFrom(data); if (appMsg == null) { throw new ChatAppException("Unknown message format."); } ChatRequest request = appMsg.Request; if (request == null) { throw new ChatAppException("Invalid message type."); } ulong?userId = _api.User.ClientId2UserId(ctx.senderId); if (!userId.HasValue) { throw new ChatAppException("You must be signedIn to add some friends."); } switch (request.RequestTypeCase) { case ChatRequest.RequestTypeOneofCase.SendRequest: return(SendMessage(userId.Value, request.SendRequest.To, request.SendRequest.Message, ctx)); case ChatRequest.RequestTypeOneofCase.ListRequest: return(ListRequest(userId.Value, ctx)); default: throw new ChatAppException("Unknown request type."); } }
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)); } }