更新长连接功能

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; if (isSubmitting) return;
setIsSubmitting(true); setIsSubmitting(true);
try { try {
const _data = await requireAPI("POST", "/api/zone/refund_v2", { const _data = await requireAPI("POST", "/api/zone/refund", {
body: { body: {
zid: Number(searchParams.get("id")), zid: Number(searchParams.get("id")),
contact_name: name, contact_name: name,

View File

@ -10,86 +10,28 @@ const WebSocketComponent = ({ getData, authInfo }) => {
const [messages, setMessages] = useState([]); const [messages, setMessages] = useState([]);
// const [pending, startTransition] = useTransition(); // const [pending, startTransition] = useTransition();
const socketRef = useRef(null); const socketRef = useRef(null);
let interval = null;
useEffect(() => { useEffect(() => {
let interval = null;
const account = get("account"); 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) { if (!account && socketRef.current) {
socketRef.current.close(); socketRef.current.close();
socketRef.current = null; socketRef.current = null;
return; return;
} }
connect_socket(); if (!socketRef.current && account) {
if (socketRef.current) { retryInterval = Math.min(retryInterval * 2, maxInterval);
// 使wss const timer = setInterval(() => {
// if (socketRef.current?.readyState !== 1) {
socketRef.current.onopen = () => { if (socketRef.current) {
// console.log("WebSocket connected.", socketRef.current.readyState); socketRef.current?.close();
// socket.send('Hello Server!'); socketRef.current = null;
retryInterval = 1000; clearInterval(interval);
sendMessageQueue(); }
socketRef.current.send(JSON.stringify({ t: 1 })); connect_socket();
};
//
socketRef.current.onmessage = (event) => {
if (Object.prototype.toString.call(event.data) === "[object Blob]") {
var reader = new FileReader();
reader.readAsText(event.data, "utf-8");
reader.onload = function (e) {
if (reader.result === "pong") {
console.log("pong received");
}
try {
const data = JSON.parse(reader.result);
if (data.t === 2 && data.msg.ping_interval) {
socketRef.current.send("ping");
interval = setInterval(() => {
// ping
socketRef.current.send("ping");
// ping
// socketRef.current.on("ping", () => {
// socketRef.current.send("pong");
// });
}, data.msg.ping_interval * 1000);
}
if (data.t === 3) {
getData(data.msg);
setMessages((prevMessages) => [...prevMessages, data]);
}
} catch (error) {}
};
} else { } else {
clearInterval(timer);
} }
}; }, retryInterval);
//
socketRef.current.onclose = () => {
clearInterval(interval);
setTimeout(connect_socket, 1000);
retryInterval = Math.min(retryInterval * 2, maxInterval);
// console.log("WebSocket disconnected.");
};
//
socketRef.current.onerror = (error) => {
// console.error("WebSocket error:", error);
//
socketRef.current?.close();
};
} }
// WebSocket // WebSocket
return () => { return () => {
@ -102,10 +44,86 @@ const WebSocketComponent = ({ getData, authInfo }) => {
}, [authInfo]); // effect }, [authInfo]); // effect
// return <Error statusCode={500} />; // 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 = () => {
// console.log("WebSocket connected.", socketRef.current.readyState);
// socket.send('Hello Server!');
retryInterval = 1000;
sendMessageQueue();
socketRef.current.send(JSON.stringify({ t: 1 }));
};
//
socketRef.current.onmessage = (event) => {
if (Object.prototype.toString.call(event.data) === "[object Blob]") {
var reader = new FileReader();
reader.readAsText(event.data, "utf-8");
reader.onload = function (e) {
if (reader.result === "pong") {
console.log("pong received");
}
try {
const data = JSON.parse(reader.result);
if (data.t === 2 && data.msg.ping_interval) {
socketRef.current.send("ping");
interval = setInterval(() => {
// ping
socketRef.current.send("ping");
// ping
// socketRef.current.on("ping", () => {
// socketRef.current.send("pong");
// });
}, data.msg.ping_interval * 1000);
}
if (data.t === 3) {
getData(data.msg);
setMessages((prevMessages) => [...prevMessages, data]);
}
} catch (error) {}
};
} else {
}
};
//
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);
}
// console.log("WebSocket disconnected.");
};
//
socketRef.current.onerror = (error) => {
// console.error("WebSocket error:", error);
//
socketRef.current?.close();
};
// ping
// socketRef.current.on("ping", () => {
// socketRef.current.send("pong");
// });
};
const sendMessageQueue = () => { const sendMessageQueue = () => {
while (messageQueue.length > 0) { while (messageQueue.length > 0) {
const data = messageQueue.shift(); // const data = messageQueue.shift(); //
sendMessage(data); // // sendMessage(data); //
} }
}; };
return ( return (