What is the best way to automatically reestablish

2019-09-06 23:43发布

问题:

I am working on a project using long polling, as the nginx server will response 504 Gateway Timeout after the connection established 1 minute or so, I write the error handling function in the $.ajax so that it can automatically reconnect to the server when it receives 504 error. But although I have done this, I don't know whether this is the best practice of long polling mechanism, and this will produce a lot of 504 in log. It seems not good enough, so how should I do it?

回答1:

You should put a shorter time-out in the long polling response. Usually 30 seconds or less makes the trick. If within 30 seconds your server does not have data to send, it must send an empty response. The client will then issue a new request (immediately, or after a pause, depending on how much you want to reduce latency; for example, in Lightstreamer by default we don't use any pause between long-polling cycles).