aboutsummaryrefslogtreecommitdiff
path: root/modern/src/SocketController.js
diff options
context:
space:
mode:
Diffstat (limited to 'modern/src/SocketController.js')
-rw-r--r--modern/src/SocketController.js19
1 files changed, 17 insertions, 2 deletions
diff --git a/modern/src/SocketController.js b/modern/src/SocketController.js
index 05236971..a6f1fa11 100644
--- a/modern/src/SocketController.js
+++ b/modern/src/SocketController.js
@@ -33,8 +33,23 @@ const SocketController = () => {
const socket = new WebSocket(`${protocol}//${window.location.host}/api/socket`);
socketRef.current = socket;
- socket.onerror = () => {
- setTimeout(() => connectSocket(), 60 * 1000);
+ socket.onopen = () => {
+ dispatch(sessionActions.updateSocket(true));
+ };
+
+ socket.onclose = async () => {
+ 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()));
+ }
+ setTimeout(() => connectSocket(), 60000);
};
socket.onmessage = (event) => {