aboutsummaryrefslogtreecommitdiff
path: root/modern/src/SocketController.js
diff options
context:
space:
mode:
authorAnton Tananaev <anton@traccar.org>2022-05-26 16:16:41 -0700
committerAnton Tananaev <anton@traccar.org>2022-05-26 16:16:41 -0700
commit3a651e5f02a90857e4023edb7ac9785d569417dd (patch)
tree8ca8ac15c3c2a708236e5db1fd1b577e880046aa /modern/src/SocketController.js
parente01380561a113f868538e9a3399fc2406e250368 (diff)
downloadtrackermap-web-3a651e5f02a90857e4023edb7ac9785d569417dd.tar.gz
trackermap-web-3a651e5f02a90857e4023edb7ac9785d569417dd.tar.bz2
trackermap-web-3a651e5f02a90857e4023edb7ac9785d569417dd.zip
Socket status and backup
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) => {