Browse Source

fix(WS): added ping/pong for client/server to prevent nginx dropping connection every 60s

Signed-off-by: Jonathan <theflametrooper@gmail.com>
Jonathan 3 years ago
parent
commit
69da354c4d
2 changed files with 17 additions and 1 deletions
  1. 16 0
      backend/logic/ws.js
  2. 1 1
      frontend/src/ws.js

+ 16 - 0
backend/logic/ws.js

@@ -68,8 +68,24 @@ class _WSModule extends CoreClass {
 				WSModule.runJob("HANDLE_WS_USE", { socket, req }).then(socket =>
 					WSModule.runJob("HANDLE_WS_CONNECTION", { socket })
 				);
+
+				socket.isAlive = true;
+				socket.on("pong", function heartbeat() {
+					this.isAlive = true;
+				});
 			});
 
+			const keepAliveInterval = setInterval(() => {
+				this._io.clients.forEach(socket => {
+					if (socket.isAlive === false) return socket.terminate();
+
+					socket.isAlive = false;
+					return socket.ping(() => {});
+				});
+			}, 45000);
+
+			this._io.on("close", () => clearInterval(keepAliveInterval));
+
 			this.setStage(4);
 
 			return resolve();

+ 1 - 1
frontend/src/ws.js

@@ -122,7 +122,7 @@ export default {
 		store.dispatch("websockets/createSocket", this.socket);
 
 		this.socket.onopen = () => {
-			console.log("IO: SOCKET CONNECTED");
+			console.log("WS: SOCKET CONNECTED");
 
 			setTimeout(() => {
 				onConnect.temp.forEach(cb => cb());