diff --git a/FireSharp.Test.Console/Program.cs b/FireSharp.Test.Console/Program.cs index 1cdce82..537b074 100644 --- a/FireSharp.Test.Console/Program.cs +++ b/FireSharp.Test.Console/Program.cs @@ -1,4 +1,5 @@ -using FireSharp.Config; +using System; +using FireSharp.Config; namespace FireSharp.Test.Console { @@ -32,13 +33,23 @@ private static async void Crud() private static async void EventStreaming() { + await _client.DeleteAsync("chat"); + await _client.OnAsync("chat", - added: (sender, args) => { System.Console.WriteLine(args.Data + "1"); }, + added: async (sender, args) => + { + System.Console.WriteLine(args.Data + "-> 1\n"); + await _client.PushAsync("chat/", new + { + name = "someone", + text = "Console 1:" + DateTime.Now.ToString("f") + }); + }, changed: (sender, args) => { System.Console.WriteLine(args.Data); }, removed: (sender, args) => { System.Console.WriteLine(args.Path); }); await _client.OnAsync("chat", - added: (sender, args) => { System.Console.WriteLine(args.Data + "2"); }, + added: (sender, args) => { System.Console.WriteLine(args.Data + " -> 2\n"); }, changed: (sender, args) => { System.Console.WriteLine(args.Data); }, removed: (sender, args) => { System.Console.WriteLine(args.Path); }); } diff --git a/FireSharp.WebApp/Controllers/HomeController.cs b/FireSharp.WebApp/Controllers/HomeController.cs index 40563ea..c452489 100644 --- a/FireSharp.WebApp/Controllers/HomeController.cs +++ b/FireSharp.WebApp/Controllers/HomeController.cs @@ -7,11 +7,11 @@ namespace FireSharp.WebApp.Controllers { public class HomeController : Controller { - private readonly IFirebaseClient _firebaseClient; + private readonly IFirebaseClient _client; - public HomeController(IFirebaseClient firebaseClient) + public HomeController(IFirebaseClient client) { - _firebaseClient = firebaseClient; + _client = client; } public ActionResult Index() @@ -21,7 +21,7 @@ public ActionResult Index() public async Task CallFirebase() { - await _firebaseClient.PushAsync("chat/", new + await _client.PushAsync("chat/", new { name = "someone", text = "Hello from backend :" + DateTime.Now.ToString("f") diff --git a/FireSharp.sln b/FireSharp.sln index 761381f..1387fb5 100644 --- a/FireSharp.sln +++ b/FireSharp.sln @@ -22,8 +22,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FireSharp.Serialization.Jso EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FireSharp.Serialization.ServiceStack", "FireSharp.Serialization.ServiceStack\FireSharp.Serialization.ServiceStack.csproj", "{7B0E3BC1-4C1D-4748-B678-3197E8E194FC}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FireSharp.Sample.Windows8App", "Samples\FireSharp.Sample.Windows8App\FireSharp.Sample.Windows8App.csproj", "{DEE14420-0A35-4E47-BB42-33C3EC691A8E}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -96,30 +94,6 @@ Global {7B0E3BC1-4C1D-4748-B678-3197E8E194FC}.Release|ARM.ActiveCfg = Release|Any CPU {7B0E3BC1-4C1D-4748-B678-3197E8E194FC}.Release|x64.ActiveCfg = Release|Any CPU {7B0E3BC1-4C1D-4748-B678-3197E8E194FC}.Release|x86.ActiveCfg = Release|Any CPU - {DEE14420-0A35-4E47-BB42-33C3EC691A8E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {DEE14420-0A35-4E47-BB42-33C3EC691A8E}.Debug|Any CPU.Build.0 = Debug|Any CPU - {DEE14420-0A35-4E47-BB42-33C3EC691A8E}.Debug|Any CPU.Deploy.0 = Debug|Any CPU - {DEE14420-0A35-4E47-BB42-33C3EC691A8E}.Debug|ARM.ActiveCfg = Debug|ARM - {DEE14420-0A35-4E47-BB42-33C3EC691A8E}.Debug|ARM.Build.0 = Debug|ARM - {DEE14420-0A35-4E47-BB42-33C3EC691A8E}.Debug|ARM.Deploy.0 = Debug|ARM - {DEE14420-0A35-4E47-BB42-33C3EC691A8E}.Debug|x64.ActiveCfg = Debug|x64 - {DEE14420-0A35-4E47-BB42-33C3EC691A8E}.Debug|x64.Build.0 = Debug|x64 - {DEE14420-0A35-4E47-BB42-33C3EC691A8E}.Debug|x64.Deploy.0 = Debug|x64 - {DEE14420-0A35-4E47-BB42-33C3EC691A8E}.Debug|x86.ActiveCfg = Debug|x86 - {DEE14420-0A35-4E47-BB42-33C3EC691A8E}.Debug|x86.Build.0 = Debug|x86 - {DEE14420-0A35-4E47-BB42-33C3EC691A8E}.Debug|x86.Deploy.0 = Debug|x86 - {DEE14420-0A35-4E47-BB42-33C3EC691A8E}.Release|Any CPU.ActiveCfg = Release|Any CPU - {DEE14420-0A35-4E47-BB42-33C3EC691A8E}.Release|Any CPU.Build.0 = Release|Any CPU - {DEE14420-0A35-4E47-BB42-33C3EC691A8E}.Release|Any CPU.Deploy.0 = Release|Any CPU - {DEE14420-0A35-4E47-BB42-33C3EC691A8E}.Release|ARM.ActiveCfg = Release|ARM - {DEE14420-0A35-4E47-BB42-33C3EC691A8E}.Release|ARM.Build.0 = Release|ARM - {DEE14420-0A35-4E47-BB42-33C3EC691A8E}.Release|ARM.Deploy.0 = Release|ARM - {DEE14420-0A35-4E47-BB42-33C3EC691A8E}.Release|x64.ActiveCfg = Release|x64 - {DEE14420-0A35-4E47-BB42-33C3EC691A8E}.Release|x64.Build.0 = Release|x64 - {DEE14420-0A35-4E47-BB42-33C3EC691A8E}.Release|x64.Deploy.0 = Release|x64 - {DEE14420-0A35-4E47-BB42-33C3EC691A8E}.Release|x86.ActiveCfg = Release|x86 - {DEE14420-0A35-4E47-BB42-33C3EC691A8E}.Release|x86.Build.0 = Release|x86 - {DEE14420-0A35-4E47-BB42-33C3EC691A8E}.Release|x86.Deploy.0 = Release|x86 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/FireSharp/Config/FirebaseConfig.cs b/FireSharp/Config/FirebaseConfig.cs index 15da15f..6152f8d 100644 --- a/FireSharp/Config/FirebaseConfig.cs +++ b/FireSharp/Config/FirebaseConfig.cs @@ -1,4 +1,5 @@ -using FireSharp.Interfaces; +using System; +using FireSharp.Interfaces; namespace FireSharp.Config { @@ -6,13 +7,13 @@ public class FirebaseConfig : IFirebaseConfig { public FirebaseConfig() { - TimeoutInMinute = 1; Serializer = new JsonNetSerializer(); } public string BasePath { get; set; } public string AuthSecret { get; set; } - public int TimeoutInMinute { get; set; } + + public TimeSpan? RequestTimeout { get; set; } public ISerializer Serializer { get; set; } } diff --git a/FireSharp/Config/IFirebaseConfig.cs b/FireSharp/Config/IFirebaseConfig.cs index 37d1ac7..b6e07c2 100644 --- a/FireSharp/Config/IFirebaseConfig.cs +++ b/FireSharp/Config/IFirebaseConfig.cs @@ -1,4 +1,5 @@ -using FireSharp.Interfaces; +using System; +using FireSharp.Interfaces; namespace FireSharp.Config { @@ -6,7 +7,7 @@ public interface IFirebaseConfig { string BasePath { get; set; } string AuthSecret { get; set; } - int TimeoutInMinute { get; set; } + TimeSpan? RequestTimeout { get; set; } ISerializer Serializer { get; set; } } } \ No newline at end of file diff --git a/FireSharp/FireSharp.nuspec b/FireSharp/FireSharp.nuspec index 6074eb7..e871997 100644 --- a/FireSharp/FireSharp.nuspec +++ b/FireSharp/FireSharp.nuspec @@ -3,7 +3,7 @@ FireSharp - 2.0.1-beta3 + 2.0.1-beta4 Fire# ziyasal ziyasal diff --git a/FireSharp/RequestManager.cs b/FireSharp/RequestManager.cs index 1df4f81..f3d0eb3 100644 --- a/FireSharp/RequestManager.cs +++ b/FireSharp/RequestManager.cs @@ -19,13 +19,18 @@ internal RequestManager(IFirebaseConfig config) _client = GetClient(); } - private HttpClient GetClient() + private HttpClient GetClient(HttpClientHandler handler = null) { - return new HttpClient + var client = handler == null ? new HttpClient() : new HttpClient(handler, true); + + client.BaseAddress = new Uri(_config.BasePath); + + if (_config.RequestTimeout.HasValue) { - BaseAddress = new Uri(_config.BasePath), - Timeout = TimeSpan.FromMinutes(_config.TimeoutInMinute) - }; + client.Timeout = _config.RequestTimeout.Value; + } + + return client; } public void Dispose() @@ -88,35 +93,19 @@ public HttpResponseMessage Patch(string path, T data) public HttpResponseMessage Listen(string path) { - var client = new HttpClient(new HttpClientHandler { AllowAutoRedirect = true }, true) - { - BaseAddress = new Uri(_config.BasePath), - Timeout = TimeSpan.FromMinutes(_config.TimeoutInMinute) - }; - var uri = PrepareUri(path); - - var request = new HttpRequestMessage(HttpMethod.Get, uri); - request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("text/event-stream")); + HttpRequestMessage request; + var client = PrepareEventStreamRequest(path, out request); var response = client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead).Result; response.EnsureSuccessStatusCode(); return response; - } public async Task ListenAsync(string path) { - var client = new HttpClient(new HttpClientHandler { AllowAutoRedirect = true }, true) - { - BaseAddress = new Uri(_config.BasePath), - Timeout = TimeSpan.FromMinutes(_config.TimeoutInMinute) - }; - - var uri = PrepareUri(path); - - var request = new HttpRequestMessage(HttpMethod.Get, uri); - request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("text/event-stream")); + HttpRequestMessage request; + var client = PrepareEventStreamRequest(path, out request); var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead); response.EnsureSuccessStatusCode(); @@ -154,6 +143,16 @@ private HttpResponseMessage ProcessRequest(HttpMethod method, string path, objec } } + private HttpClient PrepareEventStreamRequest(string path, out HttpRequestMessage request) + { + var client = GetClient(new HttpClientHandler { AllowAutoRedirect = true }); + var uri = PrepareUri(path); + + request = new HttpRequestMessage(HttpMethod.Get, uri); + request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("text/event-stream")); + return client; + } + private HttpRequestMessage PrepareRequest(HttpMethod method, string path, object payload) { var uri = PrepareUri(path);