Skip to content

Commit

Permalink
Merge branch 'development' into lukaskabc/Enhancement-535
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaskabc authored Dec 10, 2024
2 parents 9ace04d + 617b291 commit bf04224
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/WebSocketApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,23 +93,28 @@ export const WebSocketWrapper: React.FC<{
loadDispatchers = true,
}) => {
const [securityToken, setSecurityToken] = useState<string>("");
const [isTokenInvalid, setIsTokenInvalid] = useState<boolean>(false);

useEffect(() => {
const callback = () => {
const token = SecurityUtils.loadToken();
const token = SecurityUtils.loadToken() || "";
// using length prevents from aborting websocket due to token refresh
// but will abort it when token is cleared or new one is set
if (token.length !== securityToken.length) {
if (
token.length !== securityToken.length ||
(isTokenInvalid && token !== securityToken)
) {
setSecurityToken(token);
setIsTokenInvalid(false);
}
};
callback();
return BrowserStorage.onTokenChange(callback);
}, [securityToken]);
}, [securityToken, isTokenInvalid]);

return (
<Provider
enabled={securityToken.trim() !== ""}
enabled={securityToken.trim() !== "" && !isTokenInvalid}
url={Constants.WEBSOCKET_URL}
connectHeaders={{
Authorization: securityToken,
Expand All @@ -119,6 +124,10 @@ export const WebSocketWrapper: React.FC<{
onUnhandledReceipt={(receipt) =>
console.warn("Unhandled STOMP receipt", receipt)
}
onStompError={(frame) => {
console.error("WebSocket STOMP error", frame);
setIsTokenInvalid(true);
}}
>
{loadDispatchers && registerDispatchers()}
{children}
Expand Down

0 comments on commit bf04224

Please sign in to comment.