-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Socket issue #1115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Socket issue #1115
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,10 @@ import React, { useEffect, useRef, useState } from 'react'; | |
| import { useDispatch, useSelector, connect } from 'react-redux'; | ||
| import { useNavigate } from 'react-router-dom'; | ||
| import { Snackbar } from '@mui/material'; | ||
| import { devicesActions, sessionActions } from './store'; | ||
| import { | ||
| devicesActions, | ||
| sessionActions, | ||
| } from './store'; | ||
| import { useEffectAsync } from './reactHelper'; | ||
| import { useTranslation } from './common/components/LocalizationProvider'; | ||
| import { snackBarDurationLongMs } from './common/util/duration'; | ||
|
|
@@ -26,11 +29,19 @@ const SocketController = () => { | |
| const [events, setEvents] = useState([]); | ||
| const [notifications, setNotifications] = useState([]); | ||
|
|
||
| const [timeKeepAlive, setTimeKeepAlive] = useState(); | ||
|
|
||
| const soundEvents = useAttributePreference('soundEvents', ''); | ||
| const soundAlarms = useAttributePreference('soundAlarms', 'sos'); | ||
|
|
||
| const features = useFeatures(); | ||
|
|
||
| const resetCounterKeepAlive = () => { | ||
| setTimeKeepAlive(new Date()); | ||
| }; | ||
|
|
||
| const isConnected = () => Math.abs(new Date() - timeKeepAlive) < 10000; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These can probably be inlined.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. True |
||
|
|
||
| const connectSocket = () => { | ||
| const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'; | ||
| const socket = new WebSocket(`${protocol}//${window.location.host}/api/socket`); | ||
|
|
@@ -44,11 +55,10 @@ const SocketController = () => { | |
| dispatch(sessionActions.updateSocket(false)); | ||
| if (event.code !== logoutCode) { | ||
| try { | ||
| const devicesResponse = await fetch('/api/devices'); | ||
| const [devicesResponse, positionsResponse] = await Promise.all([fetch('/api/devices'), fetch('/api/positions')]); | ||
| if (devicesResponse.ok) { | ||
| dispatch(devicesActions.update(await devicesResponse.json())); | ||
| } | ||
| const positionsResponse = await fetch('/api/positions'); | ||
| if (positionsResponse.ok) { | ||
| dispatch(sessionActions.updatePositions(await positionsResponse.json())); | ||
| } | ||
|
|
@@ -58,7 +68,7 @@ const SocketController = () => { | |
| } catch (error) { | ||
| // ignore errors | ||
| } | ||
| setTimeout(() => connectSocket(), 60000); | ||
| setTimeout(connectSocket, 10000); | ||
| } | ||
| }; | ||
|
|
||
|
|
@@ -76,9 +86,39 @@ const SocketController = () => { | |
| } | ||
| setEvents(data.events); | ||
| } | ||
| if (data) { | ||
| resetCounterKeepAlive(); | ||
| } | ||
| }; | ||
| }; | ||
|
|
||
| const keepAlive = async () => { | ||
| if (!isConnected) { | ||
| const socket = socketRef.current; | ||
| if (socket) { | ||
| socket.close(logoutCode); | ||
| } | ||
| try { | ||
| const [devicesResponse, positionsResponse] = await Promise.all([fetch('/api/devices'), fetch('/api/positions')]); | ||
| if (devicesResponse.ok) { | ||
| dispatch(devicesActions.update(await devicesResponse.json())); | ||
| } | ||
| if (positionsResponse.ok) { | ||
| dispatch(sessionActions.updatePositions(await positionsResponse.json())); | ||
| } | ||
| connectSocket(); | ||
| } catch (error) { | ||
| // ignore errors | ||
| } | ||
|
Comment on lines
+101
to
+104
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need to duplicate this?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll remove the duplication |
||
| } | ||
| }; | ||
|
|
||
| useEffect(() => { | ||
| if (authenticated) { | ||
| setTimeout(keepAlive, 3000); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like this is called only once. Is that by design?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Even if the value for the useEffect change? lastUpdate change every time a data in sent via the socket.onmessage, and the code inside the useEffect is re-executed
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh I see... but how do you cancel the old timeout?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about doing that?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess it should work. The only minor nit is use "timeout" as a single word, not "timeOut".
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I push the new changes, I'll give it a try on some devices and came back with the result |
||
| } | ||
| }, [timeKeepAlive]); | ||
|
|
||
| useEffectAsync(async () => { | ||
| if (authenticated) { | ||
| const response = await fetch('/api/devices'); | ||
|
|
@@ -87,6 +127,7 @@ const SocketController = () => { | |
| } else { | ||
| throw Error(await response.text()); | ||
| } | ||
| resetCounterKeepAlive(); | ||
| connectSocket(); | ||
| return () => { | ||
| const socket = socketRef.current; | ||
|
|
@@ -123,6 +164,11 @@ const SocketController = () => { | |
| message={notification.message} | ||
| autoHideDuration={snackBarDurationLongMs} | ||
| onClose={() => setEvents(events.filter((e) => e.id !== notification.id))} | ||
| ContentProps={{ | ||
| sx: { | ||
| background: 'red', | ||
| }, | ||
| }} | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's move out all unrelated changes like this.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My bad |
||
| /> | ||
| ))} | ||
| </> | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would probably rename this to something like
lastUpdate.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll do that