From 7285abacc34695dc431b5278014a077a1deadc8e Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 5 Sep 2022 11:21:22 -0700 Subject: Logout websocket handling --- modern/src/SocketController.js | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) (limited to 'modern/src') diff --git a/modern/src/SocketController.js b/modern/src/SocketController.js index c986e363..49874ba7 100644 --- a/modern/src/SocketController.js +++ b/modern/src/SocketController.js @@ -13,6 +13,8 @@ import alarm from './resources/alarm.mp3'; import { eventsActions } from './store/events'; import useFeatures from './common/util/useFeatures'; +const logoutCode = 4000; + const SocketController = () => { const dispatch = useDispatch(); const navigate = useNavigate(); @@ -40,19 +42,21 @@ const SocketController = () => { dispatch(sessionActions.updateSocket(true)); }; - socket.onclose = async () => { + socket.onclose = async (event) => { dispatch(sessionActions.updateSocket(false)); - const devicesResponse = await fetch('/api/devices'); - if (devicesResponse.ok) { - dispatch(devicesActions.update(await devicesResponse.json())); - } - const positionsResponse = await fetch('/api/positions', { - headers: { 'Content-Type': 'application/json' }, - }); - if (positionsResponse.ok) { - dispatch(positionsActions.update(await positionsResponse.json())); + if (event.code !== logoutCode) { + const devicesResponse = await fetch('/api/devices'); + if (devicesResponse.ok) { + dispatch(devicesActions.update(await devicesResponse.json())); + } + const positionsResponse = await fetch('/api/positions', { + headers: { 'Content-Type': 'application/json' }, + }); + if (positionsResponse.ok) { + dispatch(positionsActions.update(await positionsResponse.json())); + } + setTimeout(() => connectSocket(), 60000); } - setTimeout(() => connectSocket(), 60000); }; socket.onmessage = (event) => { @@ -84,7 +88,7 @@ const SocketController = () => { return () => { const socket = socketRef.current; if (socket) { - socket.close(); + socket.close(logoutCode); } }; } -- cgit v1.2.3