websocket自動斷開連線問題
阿新 • • 發佈:2019-02-01
問題:每過5分鐘左右,websocket就會自動關閉
原因:使用了nginx服務,nginx配置:
proxy_read_timeout(Default: 60s;),如果一直沒有資料傳輸,連線會在過了這個時間之後自動關閉
參考:http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_read_timeout
Defines a timeout for reading a response from the proxied server. The timeout is set only between two successive read operations,
not for the transmission of the whole response. If the proxied server does not transmit anything within this time, the connection is closed.
可以通過心跳保持連線
參考:http://nginx.org/en/docs/http/websocket.html
the proxied server can be configured to periodically send WebSocket ping frames to reset the timeout and check if the connection is still alive.
在js中使用websocket傳送資訊之前,判斷websocket物件是不是開啟的,如果不是則重新構造/連線