Dont crash on invalid websocket (#949)

This commit is contained in:
Matthew Tancik authored and GitHub committed 2022-11-11 15:11:36 -08:00
1 parent 50f9895f1a
commit 714d6c96b4
2 files changed
+20 -2

No files matched your search

@@ -23,7 +23,11 @@ export default function WebSocketContextFunction({ children }) {
const connect = () => {
// of the form wss://ip_address:port
console.log(websocket_url);
socket = new WebSocket(websocket_url);
try {
socket = new WebSocket(websocket_url);
} catch (error) {
socket = new WebSocket('ws://localhost:7007');
}
socket.binaryType = 'arraybuffer';
socket.onopen = () => {
dispatch({
@@ -56,7 +60,9 @@ export default function WebSocketContextFunction({ children }) {
useEffect(() => {
// cleanup function to close the websocket on rerender
return () => {
socket.close();
if (socket !== null) {
socket.close();
}
};
}, [websocket_url]);
@@ -19,6 +19,16 @@ export default function WebSocketUrlField() {
});
};
const testWebSocket = (url) => {
try {
// eslint-disable-next-line no-new
new WebSocket(url);
return false;
} catch (error) {
return true;
}
};
return (
<div>
<TextField
@@ -28,6 +38,8 @@ export default function WebSocketUrlField() {
value={websocket_url}
onChange={websocket_url_onchange}
size="small"
error={testWebSocket(websocket_url)}
helperText={testWebSocket(websocket_url) ? 'Invalid websocket URL' : ''}
/>
<Link href={`/?websocket_url=${websocket_url}`}>
viewer.nerf.studio?websocket_url={websocket_url}