Not Response
Hello Folks,
I use this project to make a internal http server to response the app request with html 5 and webview, this work fine, but some times the Thread of Http Server stop response the resquest and only work when restart the application.
Not receive error, only stop work.
This my code.
public HttpServer() { var listenerConfig = Initializer.GetListener("127.0.0.1", 8080);
_httpListener = listenerConfig.httpListener;
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
}
public async void Start() { _httpListener.Timeout = new TimeSpan(0, 0, 50);
var observerListener = await _httpListener.TcpHttpRequestObservable(
port: 8080,
allowMultipleBindToSamePort: false);
observerListener.Subscribe (async
request =>
{
try
{
if (request.RequestType == RequestType.TCP)
{
HttpResponse response = null;
if (request.Path =="/API")
{
NameValueCollection dados = HttpUtility.ParseQueryString(request.QueryString);
//if (Api != null)
//var result = Api("", null);
Dictionary<String, String> vk = new Dictionary<String, String>();
foreach(var p in dados.AllKeys)
{
if(!vk.ContainsKey(p))
{
vk.Add(p,dados[p]);
}
}
Common.POCO.ResultRequest rr = null;
try
{
if (Api != null)
rr = Api(vk["Command"], vk);
}
catch(Exception ex)
{
System.Console.WriteLine($" ERR API: { vk["Command"] } MSGERR:{ ex.Message }");
}
if(rr == null || (rr.Buffer == null || rr.Buffer.Length == 0))
response = new HttpResponse
{
StatusCode = (int)HttpStatusCode.NotFound,
ResponseReason = HttpStatusCode.NotFound.ToString(),
Headers = new System.Collections.Generic.Dictionary<string, string>
{
{"Date", DateTime.UtcNow.ToString("r")}
}
};
else
response = new HttpResponse
{
StatusCode = (int)HttpStatusCode.OK,
ResponseReason = HttpStatusCode.OK.ToString(),
Headers = new System.Collections.Generic.Dictionary<string, string>
{
{"Date", DateTime.UtcNow.ToString("r")},
{"Content-Type","application/json"},
},
Body = new MemoryStream(rr.Buffer)
};
}
else
{
var file = request.Path;
var filetoget = Session.IdePath + file;
if (File.Exists(filetoget))
{
var buffer = File.ReadAllBytes(filetoget);
System.Console.WriteLine($" Address - Found");
response = new HttpResponse
{
StatusCode = (int)HttpStatusCode.OK,
ResponseReason = HttpStatusCode.OK.ToString(),
Headers = new System.Collections.Generic.Dictionary<string, string>
{
{"Date", DateTime.UtcNow.ToString("r")},
{"Content-Type",GetContentType(Path.GetExtension(filetoget)) },
},
Body = new MemoryStream(buffer)
};
}
else
{
System.Console.WriteLine($" Address - not found { filetoget }");
response = new HttpResponse
{
StatusCode = (int)HttpStatusCode.NotFound,
ResponseReason = HttpStatusCode.NotFound.ToString(),
Headers = new System.Collections.Generic.Dictionary<string, string>
{
{"Date", DateTime.UtcNow.ToString("r")}
}
};
}
}
await _httpListener.HttpSendReponseAsync(request, response).ConfigureAwait(false);
}
}
catch(Exception ex)
{
System.Console.WriteLine(ex.Message);
}
});
}
A lot of things going on here.
I've not reviewed all parts of your code, but try and see my just updated readme for handling exceptions with Rx. Try listening for those. If there are not caught (like I've now specified) then I've seen the exact behavior that you describe.
Not sure I understand the first if-statement. If the request is not TCP, what do you expect it to be? The types are TCP or UDP. Do you expect UDP?
Here is a more Rx'ish way to write the code you shared...
observerListener
.Where(req => req.RequestType == RequestType.TCP)
.Subscribe(async request =>
{
HttpResponse response = null;
if (request.Path == "/API")
{
NameValueCollection dados = HttpUtility.ParseQueryString(request.QueryString);
//if (Api != null)
//var result = Api("", null);
Dictionary<String, String> vk = new Dictionary<String, String>();
foreach (var p in dados.AllKeys)
{
if (!vk.ContainsKey(p))
{
vk.Add(p, dados[p]);
}
}
Common.POCO.ResultRequest rr = null;
try
{
if (Api != null)
rr = Api(vk["Command"], vk);
}
catch (Exception ex)
{
System.Console.WriteLine($" ERR API: { vk["Command"] } MSGERR:{ ex.Message }");
}
if (rr == null || (rr.Buffer == null || rr.Buffer.Length == 0))
response = new HttpResponse
{
StatusCode = (int) HttpStatusCode.NotFound,
ResponseReason = HttpStatusCode.NotFound.ToString(),
Headers = new System.Collections.Generic.Dictionary<string, string>
{
{"Date", DateTime.UtcNow.ToString("r")}
}
};
else
{
response = new HttpResponse
{
StatusCode = (int) HttpStatusCode.OK,
ResponseReason = HttpStatusCode.OK.ToString(),
Headers = new System.Collections.Generic.Dictionary<string, string>
{
{"Date", DateTime.UtcNow.ToString("r")},
{"Content-Type", "application/json"},
},
Body = new MemoryStream(rr.Buffer)
};
}
}
else
{
var file = request.Path;
var filetoget = Session.IdePath + file;
if (File.Exists(filetoget))
{
var buffer = File.ReadAllBytes(filetoget);
System.Console.WriteLine($" Address - Found");
response = new HttpResponse
{
StatusCode = (int)HttpStatusCode.OK,
ResponseReason = HttpStatusCode.OK.ToString(),
Headers = new System.Collections.Generic.Dictionary<string, string>
{
{"Date", DateTime.UtcNow.ToString("r")},
{"Content-Type",GetContentType(Path.GetExtension(filetoget)) },
},
Body = new MemoryStream(buffer)
};
}
else
{
System.Console.WriteLine($" Address - not found { filetoget }");
response = new HttpResponse
{
StatusCode = (int)HttpStatusCode.NotFound,
ResponseReason = HttpStatusCode.NotFound.ToString(),
Headers = new System.Collections.Generic.Dictionary<string, string>
{
{"Date", DateTime.UtcNow.ToString("r")}
}
};
}
}
await _httpListener.HttpSendReponseAsync(request, response).ConfigureAwait(false);
},
ex =>
{
// Handle your Exceptions here
},
() =>
{
// Handle Completion here
});
}
Was this helpful?
No, Have the same problem, Only stopping accept new request.
From: 1iveowl [email protected] Reply-To: 1iveowl/Simple-Http-Listener-PCL [email protected] Date: Thursday, 31 August 2017 04:59 To: 1iveowl/Simple-Http-Listener-PCL [email protected] Cc: rhvieira [email protected], Author [email protected] Subject: Re: [1iveowl/Simple-Http-Listener-PCL] Not Response (#26)
Was this helpful?
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.
Can you try and set a breakpoint in the ex => {} part and the () => {} part to see what is going on?
I can simulate that. Always when the application enter in background mode per some time, the http server stop response.
Nothing throw in exception code block.
From: 1iveowl [email protected] Reply-To: 1iveowl/Simple-Http-Listener-PCL [email protected] Date: Friday, 1 September 2017 01:39 To: 1iveowl/Simple-Http-Listener-PCL [email protected] Cc: rhvieira [email protected], Author [email protected] Subject: Re: [1iveowl/Simple-Http-Listener-PCL] Not Response (#26)
Can you try and set a breakpoint in the ex => {} part and the () => {} part to see what is going on?
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.
If this is an App, I suggest that you gracefully stop the server as the App goes "to sleep" and start it again as the App resumes. That is usually how these sort of cases are handled.
It is not really supported that the httpserver listener stops as the App goes to sleep.
Alternatively you can try and play with OnObserve or OnSubscribe to try and control what thread the Listener is running on. However, my guess is than an UWP app will kill the Listerner when it sleeps.
Does this help?
it work, perfect!!
tks
From: 1iveowl [email protected] Reply-To: 1iveowl/Simple-Http-Listener-PCL [email protected] Date: Monday, 4 September 2017 16:56 To: 1iveowl/Simple-Http-Listener-PCL [email protected] Cc: rhvieira [email protected], Author [email protected] Subject: Re: [1iveowl/Simple-Http-Listener-PCL] Not Response (#26)
If this is an App, I suggest that you gracefully stop the server as the App goes "to sleep" and start it again as the App resumes. That is usually how these sort of cases are handled.
It is not really a support that the listener stops as the App goes to sleep.
Alternatively you can try and play with OnObserve or OnSubscribe to try and control what thread the Listener is running on. However, my guess is than an UWP app will kill the Listerner when it sleeps.
Does this help?
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.