コード例 #1
0
ファイル: RealtimeClient.cs プロジェクト: xuan2261/Minista
        private async Task OnPacketReceived(Packet msg)
        {
            if (!Running)
            {
                return;
            }
            var writer = _outboundWriter;

            try
            {
                switch (msg.PacketType)
                {
                case PacketType.CONNACK:
                    this.Log("Received CONNACK");
                    await SubscribeForDM();
                    await RealtimeSub();
                    await PubSub();

                    if (SnapshotAt != null && SeqId <= 0)
                    {
                        await IrisSub();
                    }
                    StartKeepAliveLoop();
                    break;

                case PacketType.PUBLISH:
                    this.Log("Received PUBLISH");
                    var publishPacket = (PublishPacket)msg;
                    if (publishPacket.Payload == null)
                    {
                        throw new Exception($"{nameof(RealtimeClient)}: Publish packet received but payload is null");
                    }
                    if (publishPacket.QualityOfService == QualityOfService.AtLeastOnce)
                    {
                        await FbnsPacketEncoder.EncodePacket(PubAckPacket.InResponseTo(publishPacket), writer);
                    }


                    var payload = DecompressPayload(publishPacket.Payload);
                    var json    = await GetJsonFromThrift(payload);

                    this.Log($"MQTT json: {json}");
                    if (string.IsNullOrEmpty(json))
                    {
                        break;
                    }
                    try
                    {
                        Debug.WriteLine("");
                        Debug.WriteLine($"Unknown topic received:{msg.PacketType} :  {publishPacket.TopicName} : {json}");


                        Debug.WriteLine("");
                        Debug.WriteLine(json);
                        switch (publishPacket.TopicName)
                        {
                        case "150": break;

                        case "133":         //      /ig_send_message_response
                            try
                            {
                                Responses.AddItem(JsonConvert.DeserializeObject <InstaDirectRespondResponse>(json));
                            }
                            catch
                            {
                                try
                                {
                                    var o = JsonConvert.DeserializeObject <InstaDirectRespondV2Response>(json);
                                    Responses.AddItem(new InstaDirectRespondResponse
                                    {
                                        Action     = o.Action,
                                        Message    = o.Message,
                                        Status     = o.Status,
                                        StatusCode = o.StatusCode,
                                        Payload    = o.Payload[0]
                                    });
                                }
                                catch { }
                            }
                            break;

                        case "88":
                        {
                            var obj = JsonConvert.DeserializeObject <InstaRealtimeRespondResponse>(json);
                            if (obj?.Data?.Length > 0)
                            {
                                var typing = new List <InstaRealtimeTypingEventArgs>();
                                var dm     = new List <InstaDirectInboxItem>();
                                for (int i = 0; i < obj.Data.Length; i++)
                                {
                                    var item = obj.Data[i];
                                    if (item != null)
                                    {
                                        if (item.IsTyping)
                                        {
                                            var typingResponse = JsonConvert.DeserializeObject <InstaRealtimeTypingResponse>(item.Value);
                                            if (typingResponse != null)
                                            {
                                                try
                                                {
                                                    var tr = new InstaRealtimeTypingEventArgs
                                                    {
                                                        SenderId       = typingResponse.SenderId,
                                                        ActivityStatus = typingResponse.ActivityStatus,
                                                        RealtimeOp     = item.Op,
                                                        RealtimePath   = item.Path,
                                                        TimestampUnix  = typingResponse.Timestamp,
                                                        Timestamp      = DateTimeHelper.FromUnixTimeMiliSeconds(typingResponse.Timestamp),
                                                        Ttl            = typingResponse.Ttl
                                                    };
                                                    typing.Add(tr);
                                                }
                                                catch { }
                                            }
                                        }
                                        else if (item.IsThreadItem || item.IsThreadParticipants)
                                        {
                                            if (item.HasItemInValue)
                                            {
                                                var directItemResponse = JsonConvert.DeserializeObject <InstaDirectInboxItemResponse>(item.Value);
                                                if (directItemResponse != null)
                                                {
                                                    try
                                                    {
                                                        var dI = ConvertersFabric.Instance.GetDirectThreadItemConverter(directItemResponse).Convert();
                                                        dI.RealtimeOp   = item.Op;
                                                        dI.RealtimePath = item.Path;
                                                        dm.Add(dI);
                                                    }
                                                    catch { }
                                                }
                                            }
                                            else
                                            {
                                                var dI = new InstaDirectInboxItem
                                                {
                                                    RealtimeOp   = item.Op,
                                                    RealtimePath = item.Path,
                                                    ItemId       = item.Value
                                                };
                                                dm.Add(dI);
                                            }
                                        }
                                    }
                                }
                                if (typing.Count > 0)
                                {
                                    OnTypingChanged(typing);
                                }
                                if (dm.Count > 0)
                                {
                                    OnDirectItemChanged(dm);
                                }
                            }
                        }
                        break;
                        }
                    }
                    catch { }
                    break;

                case PacketType.PUBACK:
                    this.Log("Received PUBACK");
                    break;

                case PacketType.PINGRESP:
                    this.Log("Received PINGRESP");
                    break;

                default:
                    Debug.WriteLine($"Unknown topic received:{msg.PacketType}");
                    break;
                }
            }
            catch (Exception e)
            {
                DebugLogger.LogExceptionX(e);
            }
        }