private async Task Reader()
        {
            using (HttpClient client = new HttpClient {
                Timeout = Timeout.InfiniteTimeSpan
            })
            {
                for (int i = 1; AutoReconnect && (RetryLimit == -1 || i <= RetryLimit); i++)
                {
                    try
                    {
                        using (var response = await client.GetAsync($"https://stream.lobi.co/2/group/{GroupID}?token={AccessToken}", HttpCompletionOption.ResponseHeadersRead))
                            using (var reader = new System.IO.StreamReader(await response.Content.ReadAsStreamAsync()))
                            {
                                i         = 0;    //カウンタ初期化
                                Connected = true; //Connected!!
                                ConnectedEvent?.Invoke(GroupID);
                                while (!reader.EndOfStream)
                                {
                                    token = new CancellationTokenSource();       //トークン初期化
                                    var jobj = await _Read(reader, token.Token); //Jsonデータ読み取り

                                    var ev = jobj["event"]?.ToString();
                                    if (ev == "chat")
                                    {
                                        ChatEvent?.Invoke(GroupID, JsonConvert.DeserializeObject <Chat>(jobj["chat"].ToString()));
                                    }
                                    else if (ev == "chat_deleted")
                                    {
                                        ChatDeletedEvent?.Invoke(GroupID, jobj["id"].ToString());
                                    }
                                    else if (ev == "part")
                                    {
                                        PartEvent?.Invoke(GroupID, JsonConvert.DeserializeObject <UserMinimal>(jobj["user"].ToString()));
                                    }
                                    else if (ev == "archive")
                                    {
                                        ArchiveEvent?.Invoke(GroupID);
                                    }
                                }
                            }
                        token     = null;
                        Connected = false;
                        DisconnectedEvent?.Invoke(GroupID);
                    }
                    catch (OperationCanceledException)
                    {
                        token     = null;
                        Connected = false;
                        DisconnectedEvent?.Invoke(GroupID);
                        return;
                    }
                    catch (Exception ex)
                    {
                        if (Connected)
                        {
                            token     = null;
                            Connected = false;
                            DisconnectedEvent?.Invoke(GroupID);//必要かな??
                        }
                        else
                        {
                            token     = null;
                            Connected = false;//一応ね
                        }
                        FailConnectEvent?.Invoke(GroupID, ex);
                    }
                    await Task.Delay(RetryCoolTimeMilliseconds);//クールタイム
                }
            }
        }
예제 #2
0
 public void UpdateWatermark(ArchiveItem item, ArchiveEvent ev)
 {
     _watermark = new Watermark(item, ev);
     _storage.SaveWatermarkForPartition(_partitionId, _watermark);
 }