Преглед изворни кода

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

Signed-off-by: Jonathan <theflametrooper@gmail.com>
Jonathan пре 3 година
родитељ
комит
69da354c4d
2 измењених фајлова са 17 додато и 1 уклоњено
  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());