-
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: migrate entire websocket stack to solid-use
- migrate to useWebSocket hook - simplify code - each camera object now has useWebSocket accessors
- Loading branch information
1 parent
861d7d6
commit a1827a1
Showing
8 changed files
with
43 additions
and
252 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 was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,111 +1,34 @@ | ||
// TODO: Switch to tauri websocket plugin - https://github.com/tauri-apps/tauri-plugin-websocket | ||
import { RTCState } from '@src/static/types/enums' | ||
import { rtcTimeout, rtcConnectInterval } from '@store/api/selectors' | ||
import { setRTCStatus, setConnectInterval, setRTCTimeout } from '@store/api/websocket' | ||
import { useWebSocket } from 'solidjs-use' | ||
import { addNotification, ENotificationType } from '@hooks/notifications' | ||
import { getGlobalNotificationsType } from '@store/app/settings/selectors' | ||
import { cameras } from '@store/camera/selectors' | ||
|
||
import { isEmpty } from '@utils/index' | ||
const PORT = 7856 | ||
const LOCAL_HOST = 'wss://127.0.0.1' | ||
|
||
interface IWebRTCMessage { | ||
msg: string | object | null | undefined | ||
} | ||
|
||
export const sendToRTCServer = (msg: IWebRTCMessage) => { | ||
if (!msg) { | ||
console.error('[sendToRTCServer]: Message is null or undefined') | ||
return | ||
} | ||
cameras().forEach((camera) => { | ||
if (camera.ws.ws) camera.ws.ws.send(JSON.stringify(msg)) | ||
}) | ||
} | ||
|
||
export const check = () => { | ||
cameras().forEach((camera) => { | ||
if (!camera.ws.ws || camera.ws.ws.readyState == WebSocket.CLOSED) { | ||
//check if websocket instance is closed, if so call `init` function. | ||
setRTCStatus(RTCState.DISCONNECTED) | ||
initWebSocket() | ||
} | ||
}) | ||
} | ||
|
||
export const generateWebsocketClients = () => { | ||
const clients = cameras().map((camera, i) => { | ||
if (!camera.ws.ws) { | ||
camera.ws.ws = new WebSocket(`${LOCAL_HOST}:${PORT + i}`) | ||
return camera.ws | ||
} | ||
}) | ||
|
||
clients.forEach((client) => { | ||
console.log('[WebSocket Handler]: Websocket Clients - ', client) | ||
}) | ||
} | ||
|
||
/********************************* connect *************************************/ | ||
/** | ||
* @description initialize connection to the server | ||
* we use websocket heartbeat to the server | ||
* every 10 seconds | ||
*/ | ||
export const initWebSocket = () => { | ||
setRTCStatus(RTCState.CONNECTING) | ||
cameras().forEach((camera) => { | ||
if (camera.ws.ws) { | ||
camera.ws.ws.onopen = () => { | ||
setRTCTimeout(250) // reset timer to 250 on open of websocket connection | ||
clearTimeout(rtcConnectInterval()) // clear Interval on open of websocket connection | ||
|
||
setRTCStatus(RTCState.CONNECTED) | ||
setInterval(() => { | ||
sendToRTCServer({ | ||
msg: { | ||
msg_type: 'heartbeat', | ||
receiver: '', | ||
sender: '', | ||
msg: '', | ||
}, | ||
}) | ||
}, 1000 * 10) | ||
|
||
console.log('[WebSocket Client]: Connection Opened') | ||
} | ||
} | ||
}) | ||
//* TODO: Add notification to the user | ||
cameras().forEach((camera) => { | ||
if (camera.ws.ws) { | ||
camera.ws.ws.onerror = (e) => { | ||
setRTCStatus(RTCState.ERROR) | ||
console.error('[WebSocket Client]: Socket encountered error: ', e, 'Closing socket') | ||
cameras().forEach((camera) => { | ||
if (camera.ws.ws) { | ||
if (camera.ws.ws.readyState != WebSocket.CONNECTING) { | ||
if (camera.ws.ws.bufferedAmount <= 0) camera.ws.ws.close() | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
}) | ||
//* TODO: Add notification to the user | ||
cameras().forEach((camera) => { | ||
if (camera.ws.ws) { | ||
camera.ws.ws.onclose = (e) => { | ||
console.log( | ||
`[WebSocket Client]: Socket is closed. Reconnect will be attempted in ${Math.min( | ||
10000 / 1000, | ||
((rtcTimeout() as number) + (rtcTimeout() as number)) / 1000, | ||
)} second.`, | ||
e.reason, | ||
) | ||
//increment retry interval | ||
setRTCTimeout((rtcTimeout() as number) + (rtcTimeout() as number)) | ||
//call check function after timeout | ||
setConnectInterval(setTimeout(check, Math.min(10000, rtcTimeout() as number))) | ||
} | ||
cameras().map((camera, i) => { | ||
if (isEmpty(camera.ws)) { | ||
const { status, data, send, open, close } = useWebSocket(`${LOCAL_HOST}:${PORT + i}`, { | ||
autoReconnect: { | ||
retries: 3, | ||
delay: 1000, | ||
onFailed() { | ||
addNotification({ | ||
type: ENotificationType.ERROR, | ||
action: getGlobalNotificationsType(), | ||
title: 'Websocket Connection Failed', | ||
message: `[WebSocket Handler]: Failed to connect to ${camera.address}`, | ||
}) | ||
}, | ||
}, | ||
heartbeat: { | ||
message: 'ping', | ||
interval: 1000, | ||
pongTimeout: 1000, | ||
}, | ||
}) | ||
camera.ws = { status, data, send, open, close } | ||
} | ||
}) | ||
} |
Oops, something went wrong.