From 1137e2d1b338f33ab0bd5c7d3ceb9bc1c37c8323 Mon Sep 17 00:00:00 2001 From: al Date: Thu, 26 Dec 2024 19:31:17 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E9=95=BF=E8=BF=9E=E6=8E=A5?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/space/setting/spaceRefund/page.jsx | 2 +- components/Websocket/index.jsx | 162 ++++++++++++++----------- 2 files changed, 91 insertions(+), 73 deletions(-) diff --git a/app/space/setting/spaceRefund/page.jsx b/app/space/setting/spaceRefund/page.jsx index b98fe11..6c237ce 100644 --- a/app/space/setting/spaceRefund/page.jsx +++ b/app/space/setting/spaceRefund/page.jsx @@ -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, diff --git a/components/Websocket/index.jsx b/components/Websocket/index.jsx index 6eac057..8367e85 100644 --- a/components/Websocket/index.jsx +++ b/components/Websocket/index.jsx @@ -10,86 +10,28 @@ const WebSocketComponent = ({ getData, authInfo }) => { const [messages, setMessages] = useState([]); // const [pending, startTransition] = useTransition(); const socketRef = useRef(null); + let interval = null; useEffect(() => { - let interval = null; 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) { - // 注意使用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) {} - }; + 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); } - }; - - // 连接关闭时触发 - 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(); - }; + }, retryInterval); } // 组件卸载时关闭WebSocket连接 return () => { @@ -102,10 +44,86 @@ const WebSocketComponent = ({ getData, authInfo }) => { }, [authInfo]); // 空依赖数组表示这个effect只在组件挂载时运行一次 // return ; + 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 = () => { while (messageQueue.length > 0) { const data = messageQueue.shift(); // 获取并移除队列中的第一个元素 - sendMessage(data); // 尝试再次发送 + // sendMessage(data); // 尝试再次发送 } }; return (