更新长连接功能

This commit is contained in:
al 2024-12-26 19:31:17 +08:00
parent e8bbfe5a8c
commit 1137e2d1b3
2 changed files with 91 additions and 73 deletions

View File

@ -59,7 +59,7 @@ export default function SpaceRefund() {
if (isSubmitting) return;
setIsSubmitting(true);
try {
const _data = await requireAPI("POST", "/api/zone/refund_v2", {
const _data = await requireAPI("POST", "/api/zone/refund", {
body: {
zid: Number(searchParams.get("id")),
contact_name: name,

View File

@ -10,31 +10,50 @@ const WebSocketComponent = ({ getData, authInfo }) => {
const [messages, setMessages] = useState([]);
// const [pending, startTransition] = useTransition();
const socketRef = useRef(null);
useEffect(() => {
let interval = null;
useEffect(() => {
const account = get("account");
function connect_socket() {
const base = baseRequest();
if (socketRef.current && socketRef.current.readyState === 1) return;
// WebSocket
if (!socketRef.current && account)
socketRef.current = new WebSocket(
`${process.env.NEXT_PUBLIC_WEBSOCKET_URL}/ws?b_mid=${base.b_mid}&b_dt=1&b_token=${base.b_token}&b-ch=h5`
);
// ping
// socketRef.current.on("ping", () => {
// socketRef.current.send("pong");
// });
}
if (!account && socketRef.current) {
socketRef.current.close();
socketRef.current = null;
return;
}
connect_socket();
if (!socketRef.current && account) {
retryInterval = Math.min(retryInterval * 2, maxInterval);
const timer = setInterval(() => {
if (socketRef.current?.readyState !== 1) {
if (socketRef.current) {
socketRef.current?.close();
socketRef.current = null;
clearInterval(interval);
}
connect_socket();
} else {
clearInterval(timer);
}
}, retryInterval);
}
// WebSocket
return () => {
// console.log("state", socketRef.current.readyState);
if (socketRef.current)
socketRef.current?.readyState === WebSocket.OPEN &&
socketRef.current?.close();
clearInterval(interval);
};
}, [authInfo]); // effect
// return <Error statusCode={500} />;
const connect_socket = () => {
const base = baseRequest();
const account = get("account");
if (socketRef.current?.readyState === 1) return;
// WebSocket
console.log("desdas--------.", account);
socketRef.current = new WebSocket(
`${process.env.NEXT_PUBLIC_WEBSOCKET_URL}/ws?b_mid=${base.b_mid}&b_dt=1&b_token=${base.b_token}&b-ch=h5`
);
// 使wss
//
socketRef.current.onopen = () => {
@ -79,9 +98,15 @@ const WebSocketComponent = ({ getData, authInfo }) => {
//
socketRef.current.onclose = () => {
console.log("WebSocket closed.", socketRef.current.readyState);
clearInterval(interval);
if (account) {
console.log("WebSocket closed--------.", account);
setTimeout(connect_socket, 1000);
retryInterval = Math.min(retryInterval * 2, maxInterval);
// retryInterval = Math.min(retryInterval * 2, maxInterval);
}
// console.log("WebSocket disconnected.");
};
//
@ -90,22 +115,15 @@ const WebSocketComponent = ({ getData, authInfo }) => {
//
socketRef.current?.close();
};
}
// WebSocket
return () => {
// console.log("state", socketRef.current.readyState);
if (socketRef.current)
socketRef.current?.readyState === WebSocket.OPEN &&
socketRef.current?.close();
clearInterval(interval);
// ping
// socketRef.current.on("ping", () => {
// socketRef.current.send("pong");
// });
};
}, [authInfo]); // effect
// return <Error statusCode={500} />;
const sendMessageQueue = () => {
while (messageQueue.length > 0) {
const data = messageQueue.shift(); //
sendMessage(data); //
// sendMessage(data); //
}
};
return (