diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2021-09-06 11:59:42 -0700 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2021-09-06 11:59:42 -0700 |
commit | 40e9d7dda120ab6b3bdb844c08e6eacb2212dda2 (patch) | |
tree | b16edd2395820afe1e9a619f3a495292e9979bf3 /modern/src/SocketController.js | |
parent | 158f8ba27f354b53f4d25613d1ff1828cbad0bea (diff) | |
download | trackermap-web-40e9d7dda120ab6b3bdb844c08e6eacb2212dda2.tar.gz trackermap-web-40e9d7dda120ab6b3bdb844c08e6eacb2212dda2.tar.bz2 trackermap-web-40e9d7dda120ab6b3bdb844c08e6eacb2212dda2.zip |
Close socket on logout (fix #896)
Diffstat (limited to 'modern/src/SocketController.js')
-rw-r--r-- | modern/src/SocketController.js | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/modern/src/SocketController.js b/modern/src/SocketController.js index ae82d134..886ae15b 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) { |