async Task GetLastIndexAsync() { Trace.WriteLine("Tražim index poslednje poruke"); string result = DecodeUri((await StintoUrl.AppendPathSegment("poll").SetQueryParam("seq", 0).WithCookies(cookies).GetStringAsync()).Trim()); index = Convert.ToInt32(result.Split('\n').Last().Split('\t')[0], 16); }
async Task LogIn2RoomAsync() { while (true) { me = RandomString(6); IFlurlResponse response; try { Trace.WriteLine("Hoću da se ulogujem"); response = await StintoUrl.AppendPathSegment("login").WithTimeout(999).PostUrlEncodedAsync(new { nick = me, termsOfUse = "true" }); if (response.ResponseMessage.IsSuccessStatusCode) { cookies.AddOrReplace(response.Cookies[1]); break; } } catch (FlurlHttpException ex) when(ex.StatusCode == 429) { Trace.WriteLine("Previše brzo se logujem"); await Task.Delay(500); continue; } } Trace.WriteLine("Tražim svoje ime"); string result = await StintoUrl.AppendPathSegment("poll").SetQueryParam("seq", -2).WithCookies(cookies).GetStringAsync(); string[] lines = result.Substring(0, result.IndexOf("\n0")).Split('\n'); lines = lines.Take(lines.Length - 1).Where(line => line.Split('\t')[6] == me && line.Split('\t')[8] == "false").ToArray(); me = lines[0].Split('\t')[4]; }
void ConnectManager() { new Thread(async() => { while (!Connected) { string result = ""; try { Trace.WriteLine("Čekam da mi se neko javi"); result = DecodeUri(await StintoUrl.AppendPathSegment("poll").SetQueryParam("seq", index).WithCookies(cookies).GetStringAsync(cancellationTokenSource.Token)); } catch (FlurlHttpException ex) when(ex.InnerException is TaskCanceledException) { return; } if (cancellationTokenSource.IsCancellationRequested) { return; } if (result.Length == 0) { continue; } foreach (string line in result.Trim().Split('\n')) { index++; string[] words = line.Split('\t'); if (words[2] != me && words[3] == "t") { string[] message = words[4].Split(delimeter); if (message[0] == "private" && (message[1] == talktome || message[1] == areyoufree)) { Trace.WriteLine("Odgovaram na poziv"); await SendPrivateMessageAsync(yeahimhere + delimeter + message[2]); if (message[1] == talktome) { kolega = message[2]; Connected = true; OnConnect?.Invoke(true); break; } } } } } }) { IsBackground = true }.Start(); }
async Task WaitResponseAsync(CancellationToken cancelToken) { await GetLastIndexAsync(); while (!cancelToken.IsCancellationRequested) { Trace.WriteLine("Čekam da mi se domaćin javi"); string result = DecodeUri(await StintoUrl.AppendPathSegment("poll").SetQueryParam("seq", index).WithCookies(cookies).GetStringAsync()); foreach (string line in result.Trim().Split('\n')) { index++; string[] words = line.Split('\t'); if (words[3] == "t" && words[4] == "private" + delimeter + yeahimhere + delimeter + me) { kolega = words[2]; return; } } } }
async Task ConnectAsync() { Trace.WriteLine("Zovem domaćina"); await StintoUrl.AppendPathSegment("post").WithCookies(cookies).PostUrlEncodedAsync(new { type = "TXT", text = "private" + delimeter + talktome + delimeter + me }); var cancellationToken = new CancellationTokenSource(); var task = WaitResponseAsync(cancellationToken.Token); if (await Task.WhenAny(task, Task.Delay(waitTime, cancellationToken.Token)) == task) { Trace.WriteLine("Javio se"); await task; Trace.WriteLine("Task gotov"); Connected = true; OnConnect?.Invoke(true); } else { cancellationToken.Cancel(); OnConnect?.Invoke(false); } }
public async Task <string> ReadMessageAsync() { while (true) { string result; try { Trace.WriteLine("Čitam poruke"); result = DecodeUri(await StintoUrl.AppendPathSegment("poll").SetQueryParam("seq", index).WithCookies(cookies).GetStringAsync(cancellationTokenSource.Token)); } catch (FlurlHttpException ex) when(ex.InnerException is TaskCanceledException) { return(""); } if (result.Length == 0) { continue; } foreach (string line in result.Trim().Split('\n')) { index++; string[] words = line.Split('\t'); if (words[2] != me && words[3] == "t") { string[] message = words[4].Split(delimeter); if (message[0] == "public") { return(DecodeUri(message[1])); } } else if (words[2] == kolega && words[3] == "x") { Connected = false; OnDisconnect?.Invoke(); return(""); } } } }
async Task SendPrivateMessageAsync(string message) { Trace.WriteLine("Šaljem privatnu poruku " + message); await StintoUrl.AppendPathSegment("post").WithCookies(cookies).PostUrlEncodedAsync(new { type = "TXT", text = "private" + delimeter + message }); }