Skip to content
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

Socket issue #1115

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 38 additions & 4 deletions modern/src/SocketController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -26,11 +29,18 @@ const SocketController = () => {
const [events, setEvents] = useState([]);
const [notifications, setNotifications] = useState([]);

const [lastUpdate, setLastUpdate] = useState();
const [timeoutId, setTimeoutId] = useState();

const soundEvents = useAttributePreference('soundEvents', '');
const soundAlarms = useAttributePreference('soundAlarms', 'sos');

const features = useFeatures();

const resetCounterKeepAlive = () => setLastUpdate(new Date());

const isConnected = () => Math.abs(new Date() - lastUpdate) < 10000;

const connectSocket = () => {
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
const socket = new WebSocket(`${protocol}//${window.location.host}/api/socket`);
Expand All @@ -44,11 +54,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()));
}
Expand All @@ -58,7 +67,7 @@ const SocketController = () => {
} catch (error) {
// ignore errors
}
setTimeout(() => connectSocket(), 60000);
setTimeout(connectSocket, 10000);
}
};

Expand All @@ -76,9 +85,33 @@ const SocketController = () => {
}
setEvents(data.events);
}
if (data) {
resetCounterKeepAlive();
}
};
};

const keepAlive = async () => {
if (!isConnected) {
const socket = socketRef.current;
if (socket) {
socket.close(logoutCode);
}
try {
connectSocket();
} catch (error) {
// ignore errors
}
}
};

useEffect(() => {
if (authenticated) {
clearTimeout(timeoutId);
setTimeoutId(setTimeout(keepAlive, 3000));
}
}, [lastUpdate]);

useEffectAsync(async () => {
if (authenticated) {
const response = await fetch('/api/devices');
Expand All @@ -87,6 +120,7 @@ const SocketController = () => {
} else {
throw Error(await response.text());
}
resetCounterKeepAlive();
connectSocket();
return () => {
const socket = socketRef.current;
Expand Down