Skip to content

Commit

Permalink
updated the way of server working
Browse files Browse the repository at this point in the history
  • Loading branch information
Sir-Thom committed Dec 12, 2023
1 parent b357fd9 commit 7c6f65d
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 60 deletions.
43 changes: 16 additions & 27 deletions src/utils/hooks/ServerData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<IServer | null>(null);
// get IP api from server
const [apiIp, setApiIp] = useState<string | null>(null);
const [serverError, setserverError] = useState<string | null>(null);
//send fetch request to server
const [serverError, setServerError] = useState<string | null>(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;
37 changes: 19 additions & 18 deletions src/views/ServerInfoView/HlsServerInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<any[]>([]);
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 (
<motion.div
Expand Down
35 changes: 23 additions & 12 deletions src/views/ServerInfoView/RtmpServerInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,38 @@ 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 RtmpConnInfo() {
const [items, setItems] = useState<any[]>([]);
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);
Expand All @@ -40,7 +51,7 @@ export default function RtmpConnInfo() {
>
<div className="mt-4 mb-2">
<h2 className=" mx-auto mb-10 text-center font-bold text-3xl">
RTSP Informations
RTMP Informations
</h2>

<ListView
Expand Down
7 changes: 4 additions & 3 deletions src/views/ServerInfoView/RtspServerInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ 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 RtspServerInfo() {
const [items, setItems] = useState<any[]>([]);

const {apiIp} = useServerData();
useEffect(() => {
getAllRtspSessions();
}, []);

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) {
Expand All @@ -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
});

Expand Down
1 change: 1 addition & 0 deletions src/views/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ export default function Setting() {
try {

const apiIpValue = apiIp;
console.log("apiIpValue: " + apiIpValue);
if (!PatchData) {
setError("ConfigData is empty.");
return;
Expand Down

0 comments on commit 7c6f65d

Please sign in to comment.