- Home /
How to avoid reestablishing an HTTPS request, use Connection: Keep-Alive or reuse WWW object?
Hi
I have an application which uses HTTPS to communicate with an API. Establishing the initial handshake and connection is killing our performance.
When profiling with Apache's ab tool the time it takes to establish a new connection to my web serve ranges anywhere from 250ms-400ms. This is consistent with our customers experience with our app, they complain its slow.
When using keep-alive
and profiling the initial connection is anywhere from 250ms-400ms, but all subsequent connections are, as expected, close-to-or 0ms.
I can find 0 documentation on how if Unity will respect the Connection: keep-alive
header, which I am not hopeful of given it disposes of a WWW connection).
Is there a way to reuse a WWW connection, make Unity respect the Connection: keep-alive
header (and thus not reestablish the hand shake with each new instance) or another method?
I found this Unity employee state they are "working on it" for the UnityWebRequest class but that was a year ago and "noted" is "noted" is the developer equivalent of a blow off. The official documentation still states you can not set that header.
Hello, maybe you found a solution to this problem? We need this too.
Answer by Bunny83 · Apr 04, 2018 at 02:27 PM
No. The WWW class as well as the UnityWebRequest represent a single request / response. The reuse of an HTTP or HTTPS connection only makes sense in an actual HTTP client which actually holds certain persistent data (including cookies). Of course it may depend on your target platform what classes you can use. The HTTPClient may not work on WebGL. In this case you may want handle AJAX requests directly in Javascript. Keep in mind that keep-alive always has a time-out delay. So you can't keep the connection forever. Some servers only have a keep alive timeout of a few seconds. It's mainly meant to serve several resources of the same page as a "chunk" without creating a new connection for every image / css / js file. If you are in full control of the server you can of course set the keep alive time to something longer. Though note that TCP connections might have their own timeout. Especially when you go through a NAT router you don't want to keep connections too long without any communication as the "NAT hole" the router creates might be closed by the router. So you might need some heartbeat requests in regular intervals to actually keep the connection alive.
Your answer
Follow this Question
Related Questions
How do I add a header and a file at the web request at the same time? 0 Answers
HTTPS: ignore untrusted certificate? 1 Answer
Unity 5.6.7 - UnityWebRequest SSL/HTTPs 2 Answers
How to validate SSL certificate before sending data via https? 1 Answer
How to validate SSL certificates when using HttpWebRequest 2 Answers