Skip to content

Commit

Permalink
Update websocker connection for offline mode
Browse files Browse the repository at this point in the history
  • Loading branch information
gkatrakazas committed May 24, 2024
1 parent 480ab1d commit 10658de
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/hoc/handleServerMessagesGuard.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ export default function handleServerMessagesGuard(Component) {
const appToken = api.getAppToken();

const [handshakeEstablished, setHandshakeEstablished] = useState(false);
const [isOnline, setIsOnline] = useState(navigator.onLine);
const socketRef = useRef(null);
const keystore = useLocalStorageKeystore();
const signingRequestHandlerService = SigningRequestHandlerService();

useEffect(
() => {
if (appToken) {
if (isOnline && appToken) {
if (!socketRef.current) {
const socket = new WebSocket(REACT_APP_WS_URL);
socketRef.current = socket;
Expand Down Expand Up @@ -80,7 +81,16 @@ export default function handleServerMessagesGuard(Component) {
[appToken],
);

if (handshakeEstablished === true || !appToken) {
// Check for online status changes and update the state accordingly
window.addEventListener('online', () => {
setIsOnline(true);
});

window.addEventListener('offline', () => {
setIsOnline(false);
});

if (!isOnline || handshakeEstablished === true || !appToken) {
return (<Component {...props} />);
}
else {
Expand Down

0 comments on commit 10658de

Please sign in to comment.