Winrt/WP8.1 中的长轮询请求出现巨大延迟
Huge delay in long polling request in Winrt/WP8.1
我正在为 Windows 8.1 和 Windows Phone 8.1 编写通用应用程序。我需要打开长轮询请求以获得持续更新的数据流。我用 HttpClient
这样做并且它工作正常,除了从流中获取新数据的巨大延迟。在 WinRT 上,这个延迟大约 10-20 秒,在 Windows Phone 上大约 1-2 分钟 (!!!)。这对于这个应用程序来说是不可接受的,我希望我的代码有问题并且有人可以帮助我修复它。这是我的代码:
var handler = new HttpClientHandler();
using (var client = new HttpClient(handler))
{
client.Timeout = TimeSpan.FromMilliseconds(Timeout.Infinite);
client.BaseAddress = new Uri("https://my.site.com/");
handler.CookieContainer.SetCookies(client.BaseAddress, cookies);
var link = "my/link";
using (var tokenRequestMessage = new HttpRequestMessage(HttpMethod.Get, link))
{
tokenRequestMessage.Headers.Add("Authorization", token);
tokenRequestMessage.Headers.Add("Foo", "Foo");
tokenRequestMessage.Headers.Add("Foo1", "Foo2");
var t = await client.SendAsync(tokenRequestMessage, HttpCompletionOption.ResponseHeadersRead);
using (var httpResponse = t.Content)
{
using (var v = await httpResponse.ReadAsStreamAsync())
{
using (var reader = new StreamReader(v))
{
while (!reader.EndOfStream)
{
Debug.WriteLine(reader.ReadLine());
}
}
}
}
}
}
好吧,这是我完成任务的方式。它工作不完美,但当它出现在 InputStream
.
中时,我立即获取数据
var uri = new Uri("www.example.com");
using (var client = new Windows.Web.Http.HttpClient())
{
client.DefaultRequestHeaders.Cookie.ParseAdd(this.sessionCookies);
client.DefaultRequestHeaders.Add("header", "value");
var response = await client.GetAsync(uri, Windows.Web.Http.HttpCompletionOption.ResponseHeadersRead);
var inputStream = await response.Content.ReadAsInputStreamAsync();
IBuffer buffer = new Buffer(10000);
do
{
buffer = await inputStream.ReadAsync(buffer, buffer.Capacity, InputStreamOptions.ReadAhead);
var data = Encoding.UTF8.GetString(buffer.ToArray(), 0, (int) buffer.Length);
Debug.WriteLine(data);
} while (buffer.Length > 0);
}
我正在为 Windows 8.1 和 Windows Phone 8.1 编写通用应用程序。我需要打开长轮询请求以获得持续更新的数据流。我用 HttpClient
这样做并且它工作正常,除了从流中获取新数据的巨大延迟。在 WinRT 上,这个延迟大约 10-20 秒,在 Windows Phone 上大约 1-2 分钟 (!!!)。这对于这个应用程序来说是不可接受的,我希望我的代码有问题并且有人可以帮助我修复它。这是我的代码:
var handler = new HttpClientHandler();
using (var client = new HttpClient(handler))
{
client.Timeout = TimeSpan.FromMilliseconds(Timeout.Infinite);
client.BaseAddress = new Uri("https://my.site.com/");
handler.CookieContainer.SetCookies(client.BaseAddress, cookies);
var link = "my/link";
using (var tokenRequestMessage = new HttpRequestMessage(HttpMethod.Get, link))
{
tokenRequestMessage.Headers.Add("Authorization", token);
tokenRequestMessage.Headers.Add("Foo", "Foo");
tokenRequestMessage.Headers.Add("Foo1", "Foo2");
var t = await client.SendAsync(tokenRequestMessage, HttpCompletionOption.ResponseHeadersRead);
using (var httpResponse = t.Content)
{
using (var v = await httpResponse.ReadAsStreamAsync())
{
using (var reader = new StreamReader(v))
{
while (!reader.EndOfStream)
{
Debug.WriteLine(reader.ReadLine());
}
}
}
}
}
}
好吧,这是我完成任务的方式。它工作不完美,但当它出现在 InputStream
.
var uri = new Uri("www.example.com");
using (var client = new Windows.Web.Http.HttpClient())
{
client.DefaultRequestHeaders.Cookie.ParseAdd(this.sessionCookies);
client.DefaultRequestHeaders.Add("header", "value");
var response = await client.GetAsync(uri, Windows.Web.Http.HttpCompletionOption.ResponseHeadersRead);
var inputStream = await response.Content.ReadAsInputStreamAsync();
IBuffer buffer = new Buffer(10000);
do
{
buffer = await inputStream.ReadAsync(buffer, buffer.Capacity, InputStreamOptions.ReadAhead);
var data = Encoding.UTF8.GetString(buffer.ToArray(), 0, (int) buffer.Length);
Debug.WriteLine(data);
} while (buffer.Length > 0);
}