diff --git a/src/utils/hooks/ServerData.tsx b/src/utils/hooks/ServerData.tsx index ea4ce3d0..433649c4 100644 --- a/src/utils/hooks/ServerData.tsx +++ b/src/utils/hooks/ServerData.tsx @@ -2,52 +2,41 @@ import { useEffect, useState } from 'react'; import { invoke } from '@tauri-apps/api'; import { IServer } from '../../interfaces/IServer'; - function useServerData() { const [configData, setConfigData] = useState(null); - // get IP api from server const [apiIp, setApiIp] = useState(null); - const [serverError, setserverError] = useState(null); - //send fetch request to server + const [serverError, setServerError] = useState(null); useEffect(() => { async function fetchData() { try { const apiIpValue = await getApiIp(); setApiIp(apiIpValue); - setserverError(null); + setServerError(null); const serverUrl = `http://${apiIpValue}/v3/config/global/get`; - const response = await invoke('get_server_request', { url: serverUrl }).then((response: string) => { - console.log("Server Response in ServerData:", response) - return response - }); - console.log('Server response: ' + response); + const response = await invoke('get_server_request', { url: serverUrl }); const parsedResponse: IServer = JSON.parse(response as string); - setConfigData(parsedResponse); - console.log('Server data: ' + JSON.stringify(parsedResponse)); - console.log('Server data: ' + JSON.stringify(parsedResponse)); } catch (error) { - setserverError('Unable to connect to the server.'); + setServerError('Unable to connect to the server.'); } } fetchData(); - }, []); // Empty dependency array ensures this effect runs once when the component mounts - - return { configData, serverError,apiIp }; -} - -async function getApiIp() { - try { - const res = await invoke('get_api_ip'); - console.log('API IP: ' + res); - return res.toString(); - } catch (e) { - console.error(e); - return 'unable to get API Ip adress.'; + }, []); + + async function getApiIp() { + try { + const res = await invoke('get_api_ip'); + return res.toString().replace(/^"(.*)"$/, '$1'); + } catch (e) { + console.error(e); + return 'unable to get API Ip address.'; + } } + + return { configData, serverError, apiIp }; } export default useServerData; diff --git a/src/views/ServerInfoView/HlsServerInfo.tsx b/src/views/ServerInfoView/HlsServerInfo.tsx index 0b02c15c..f916408f 100644 --- a/src/views/ServerInfoView/HlsServerInfo.tsx +++ b/src/views/ServerInfoView/HlsServerInfo.tsx @@ -3,32 +3,33 @@ import { motion } from "framer-motion"; import { fadeIn } from "../../utils/animation/screenAnimation"; import { invoke } from "@tauri-apps/api"; import ListView from "../../components/ListBox/listView"; +import useServerData from "../../utils/hooks/ServerData"; export default function HLsConnInfo() { const [items, setItems] = useState([]); + const { apiIp } = useServerData(); + + useEffect(() => { + if (apiIp !== null) { + console.log("apiIp:", apiIp); + + invoke("get_server_request", { - url: "http://127.0.0.1:9997/v3/hlsmuxers/list" + url: `http://${apiIp}/v3/hlsmuxers/list`, }).then((response) => { - console.log("response:", JSON.parse(response.toString())); - response = JSON.parse(response.toString()); - console.log("response:", response); - if (response && (response as { items: any[] }).items) { - setItems((response as { items: any[] }).items); - } else { - console.error("Response does not contain 'items'."); - } + console.log("response:", JSON.parse(response.toString())); + response = JSON.parse(response.toString()); + console.log("response:", response); + if (response && (response as { items: any[] }).items) { + setItems((response as { items: any[] }).items); + } else { + console.error("Response does not contain 'items'."); + } }); - }, []); + } + }, [apiIp]); - async function KickRTMPession(valueToSend: string) { - invoke("post_server_request", { - url: "http://127.0.0.1:9997/v3/rtmpconns/kick/", - value: valueToSend - }).then((res) => { - console.log(res); - }); - } return ( ([]); + const { apiIp } = useServerData(); + + useEffect(() => { + if (apiIp !== null) { + console.log("apiIp:", apiIp); + + invoke("get_server_request", { - url: "http://127.0.0.1:9997/v3/rtmpconns/list" + url: `http://${apiIp}/v3/rtmpconns/list`, }).then((response) => { - console.log("response:", JSON.parse(response.toString())); - response = JSON.parse(response.toString()); - console.log("response:", response); - if (response && (response as { items: any[] }).items) { - setItems((response as { items: any[] }).items); - } else { - console.error("Response does not contain 'items'."); - } + console.log("response:", JSON.parse(response.toString())); + response = JSON.parse(response.toString()); + console.log("response:", response); + if (response && (response as { items: any[] }).items) { + setItems((response as { items: any[] }).items); + } else { + console.error("Response does not contain 'items'."); + } }); - }, []); + } + }, [apiIp]); async function KickRTMPession(valueToSend: string) { invoke("post_server_request", { - url: "http://127.0.0.1:9997/v3/rtmpconns/kick/", + url: `http://${apiIp}/v3/rtmpconns/kick/`, value: valueToSend }).then((res) => { console.log(res); @@ -40,7 +51,7 @@ export default function RtmpConnInfo() { >

- RTSP Informations + RTMP Informations

([]); - + const {apiIp} = useServerData(); useEffect(() => { getAllRtspSessions(); }, []); @@ -14,7 +15,7 @@ export default function RtspServerInfo() { async function getAllRtspSessions() { try { const response = await invoke("get_server_request", { - url: "http://127.0.0.1:9997/v3/rtspsessions/list" + url: `http://${apiIp}/v3/rtspsessions/list` }); const parsedResponse = JSON.parse(response.toString()); if (parsedResponse && parsedResponse.items) { @@ -30,7 +31,7 @@ export default function RtspServerInfo() { async function kickRstpSession(valueToSend: string) { try { await invoke("post_server_request", { - url: `http://127.0.0.1:9997/v3/rtspsessions/kick/`, + url: `http://${apiIp}/v3/rtspsessions/kick/`, value: valueToSend }); diff --git a/src/views/Settings.tsx b/src/views/Settings.tsx index f4aaf717..c4bc427a 100644 --- a/src/views/Settings.tsx +++ b/src/views/Settings.tsx @@ -440,6 +440,7 @@ export default function Setting() { try { const apiIpValue = apiIp; + console.log("apiIpValue: " + apiIpValue); if (!PatchData) { setError("ConfigData is empty."); return;