Skip to content

Commit

Permalink
Handle WebSocket close codes in console-v2
Browse files Browse the repository at this point in the history
  • Loading branch information
retrixe committed Nov 28, 2024
1 parent 55ecfdc commit d1c503f
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions pages/dashboard/[server]/console.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ const CommandTextField = ({ ws, id, buffer }: {
const terminalUi = typeof localStorage === 'object' && localStorage.getItem('terminal-ui') === 'true'
? { backgroundColor: '#141729', color: '#00cc74' } : {}

const Console = ({ setAuthenticated }: {
// setServerExists: React.Dispatch<React.SetStateAction<boolean>>,
const Console = ({ setAuthenticated, setServerExists }: {
setAuthenticated: React.Dispatch<React.SetStateAction<boolean>>
setServerExists: React.Dispatch<React.SetStateAction<boolean>>
}): JSX.Element => {
const color = useTheme().palette.mode === 'dark' ? '#d9d9d9' : undefined
const { ip, node, server } = useOctyneData()
Expand Down Expand Up @@ -134,14 +134,11 @@ const Console = ({ setAuthenticated }: {
}
let versionFallbackInEffect = false
newWS.onerror = () => {
const fallback = !newWS.protocol && !versionFallback.current
if (fallback) {
versionFallback.current = true
versionFallbackInEffect = true
}
versionFallbackInEffect = !newWS.protocol && !versionFallback.current
versionFallback.current = versionFallback.current || versionFallbackInEffect
buffer.current.push({
id: ++id.current,
text: fallback
text: versionFallbackInEffect
? '[Ecthelion] Console v2 API unsupported! Falling back to v1 (your connection may ' +
'close randomly and some features may be missing). Upgrade to Octyne v1.1 or newer!' +
' Note: If you\'re running Octyne v1.1+ and you still see this message, Ecthelion' +
Expand All @@ -150,7 +147,9 @@ const Console = ({ setAuthenticated }: {
})
}
newWS.onclose = event => {
if (event.code === 4999) return
if (event.code === 4401) setAuthenticated(false)
if (event.code === 4404) setServerExists(false)
if (event.code >= 4000 && event.code <= 4999) return
if (versionFallbackInEffect) return setWs(null)
buffer.current.push({
id: ++id.current,
Expand All @@ -163,7 +162,7 @@ const Console = ({ setAuthenticated }: {
setListening(false)
console.error(`An error occurred while connecting to console.\n${e}`)
}
}, [ip, ky, server, setAuthenticated])
}, [ip, ky, server, setAuthenticated, setServerExists])
useEffect(() => {
if (!ws) {
const ignore = { current: false } // Required to handle React.StrictMode correctly.
Expand Down Expand Up @@ -214,7 +213,7 @@ const Console = ({ setAuthenticated }: {

const ConsolePage = (): JSX.Element => {
const { server, nodeExists } = useOctyneData()
const [serverExists] = useState(true) // TODO: setServerExists
const [serverExists, setServerExists] = useState(true)
const [authenticated, setAuthenticated] = useState(true)
return (
<React.StrictMode>
Expand All @@ -228,7 +227,7 @@ const ConsolePage = (): JSX.Element => {
? <NotExistsError node={!nodeExists} />
: !authenticated
? <AuthFailure />
: <Console setAuthenticated={setAuthenticated} />}
: <Console setAuthenticated={setAuthenticated} setServerExists={setServerExists} />}
</DashboardLayout>
</React.StrictMode>
)
Expand Down

0 comments on commit d1c503f

Please sign in to comment.