mirror of
https://github.com/khairul169/code-share.git
synced 2025-04-28 16:49:36 +07:00
21 lines
404 B
TypeScript
21 lines
404 B
TypeScript
import { useEffect } from "react";
|
|
|
|
export const useSSE = (url: string, onData: (data: any) => void) => {
|
|
useEffect(() => {
|
|
const sse = new EventSource(url, { withCredentials: true });
|
|
|
|
sse.onmessage = (e) => {
|
|
onData(JSON.parse(e.data));
|
|
};
|
|
|
|
sse.onerror = () => {
|
|
console.log("err!");
|
|
sse.close();
|
|
};
|
|
|
|
return () => {
|
|
sse.close();
|
|
};
|
|
}, [url]);
|
|
};
|