-
Notifications
You must be signed in to change notification settings - Fork 360
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(calling): added web worker to send keepalives (#4101)
Co-authored-by: Shreyas Sharma <shreyassharma9912@gmail.com>
- Loading branch information
1 parent
51a0f4a
commit b06c160
Showing
8 changed files
with
161 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
packages/calling/src/CallingClient/registration/webWorker.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import {v4 as uuid} from 'uuid'; | ||
import {HTTP_METHODS, WorkerMessageType} from '../../common/types'; | ||
|
||
onmessage = (event: MessageEvent) => { | ||
const {type} = event.data; | ||
let keepaliveTimer: NodeJS.Timer | undefined; | ||
|
||
const postKeepAlive = async (accessToken: string, deviceUrl: string, url: string) => { | ||
const response = await fetch(`${url}/status`, { | ||
method: HTTP_METHODS.POST, | ||
headers: { | ||
'cisco-device-url': deviceUrl, | ||
'spark-user-agent': 'webex-calling/beta', | ||
Authorization: `${accessToken}`, | ||
trackingId: `web_worker_${uuid()}`, | ||
}, | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error(`Keepalive failed with status: ${response.status}`); | ||
} | ||
|
||
return response; | ||
}; | ||
|
||
if (type === WorkerMessageType.START_KEEPALIVE) { | ||
let keepAliveRetryCount = 0; | ||
const {accessToken, deviceUrl, interval, retryCountThreshold, url} = event.data; | ||
|
||
if (keepaliveTimer) { | ||
clearInterval(keepaliveTimer); | ||
keepaliveTimer = undefined; | ||
} | ||
|
||
keepaliveTimer = setInterval(async () => { | ||
if (keepAliveRetryCount < retryCountThreshold) { | ||
try { | ||
const res = await postKeepAlive(accessToken, deviceUrl, url); | ||
const statusCode = res.status; | ||
if (keepAliveRetryCount > 0) { | ||
postMessage({type: WorkerMessageType.KEEPALIVE_SUCCESS, statusCode}); | ||
} | ||
keepAliveRetryCount = 0; | ||
} catch (err: unknown) { | ||
keepAliveRetryCount += 1; | ||
postMessage({type: WorkerMessageType.KEEPALIVE_FAILURE, err}); | ||
} | ||
} | ||
}, interval * 1000); | ||
} | ||
|
||
if (type === WorkerMessageType.CLEAR_KEEPALIVE) { | ||
if (keepaliveTimer) { | ||
clearInterval(keepaliveTimer); | ||
keepaliveTimer = undefined; | ||
} | ||
} | ||
}; |
Oops, something went wrong.