From 40e9d7dda120ab6b3bdb844c08e6eacb2212dda2 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 6 Sep 2021 11:59:42 -0700 Subject: Close socket on logout (fix #896) --- modern/src/SocketController.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'modern/src/SocketController.js') diff --git a/modern/src/SocketController.js b/modern/src/SocketController.js index ae82d13..886ae15 100644 --- a/modern/src/SocketController.js +++ b/modern/src/SocketController.js @@ -1,3 +1,4 @@ +import { useRef } from 'react'; import { useDispatch, useSelector, connect } from 'react-redux'; import { useHistory } from 'react-router-dom'; @@ -24,13 +25,17 @@ const displayNotifications = (events) => { const SocketController = () => { const dispatch = useDispatch(); const history = useHistory(); + const authenticated = useSelector((state) => !!state.session.user); + const socketRef = useRef(); + const connectSocket = () => { const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'; const socket = new WebSocket(`${protocol}//${window.location.host}/api/socket`); + socketRef.current = socket; - socket.onclose = () => { + socket.onerror = () => { setTimeout(() => connectSocket(), 60 * 1000); }; @@ -62,6 +67,12 @@ const SocketController = () => { dispatch(devicesActions.refresh(await response.json())); } connectSocket(); + return () => { + const socket = socketRef.current; + if (socket) { + socket.close(); + } + } } else { const response = await fetch('/api/session'); if (response.ok) { -- cgit v1.2.3