mirror of
https://github.com/hamster1963/nezha-dash.git
synced 2025-04-24 21:10:45 +08:00
44 lines
1.1 KiB
TypeScript
44 lines
1.1 KiB
TypeScript
"use client";
|
|
import useSWRSubscription, {
|
|
type SWRSubscriptionOptions,
|
|
} from "swr/subscription";
|
|
|
|
type LooseObject = {
|
|
[key: string]: any;
|
|
};
|
|
|
|
export function SSEDataFetch(url: string, fallbackData?: LooseObject): any {
|
|
const { data } = useSWRSubscription<LooseObject>(
|
|
url,
|
|
(key: string | URL, { next }: SWRSubscriptionOptions<LooseObject>) => {
|
|
const source = new EventSource(key);
|
|
source.onmessage = (event) => {
|
|
const parsedData = JSON.parse(event.data);
|
|
next(null, parsedData);
|
|
};
|
|
source.onerror = () => next(new Error("EventSource error"));
|
|
return () => source.close();
|
|
},
|
|
{
|
|
fallbackData: fallbackData,
|
|
},
|
|
);
|
|
return data;
|
|
}
|
|
|
|
export function verifySSEConnection(url: string): Promise<{ name: string }> {
|
|
return new Promise<{ name: string }>((resolve, reject) => {
|
|
const eventSource = new EventSource(url);
|
|
|
|
eventSource.onopen = () => {
|
|
resolve({ name: "SSE Connected" });
|
|
eventSource.close();
|
|
};
|
|
|
|
eventSource.onerror = () => {
|
|
reject("Failed to connect");
|
|
eventSource.close();
|
|
};
|
|
});
|
|
}
|