更新长连接功能
This commit is contained in:
parent
e8bbfe5a8c
commit
1137e2d1b3
|
@ -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,
|
||||||
|
|
|
@ -10,31 +10,50 @@ 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);
|
||||||
useEffect(() => {
|
|
||||||
let interval = null;
|
let interval = null;
|
||||||
|
useEffect(() => {
|
||||||
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) {
|
||||||
|
retryInterval = Math.min(retryInterval * 2, maxInterval);
|
||||||
|
const timer = setInterval(() => {
|
||||||
|
if (socketRef.current?.readyState !== 1) {
|
||||||
if (socketRef.current) {
|
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协议(如果服务器支持)
|
// 注意使用wss协议(如果服务器支持)
|
||||||
// 连接打开时触发
|
// 连接打开时触发
|
||||||
socketRef.current.onopen = () => {
|
socketRef.current.onopen = () => {
|
||||||
|
@ -79,9 +98,15 @@ const WebSocketComponent = ({ getData, authInfo }) => {
|
||||||
|
|
||||||
// 连接关闭时触发
|
// 连接关闭时触发
|
||||||
socketRef.current.onclose = () => {
|
socketRef.current.onclose = () => {
|
||||||
|
console.log("WebSocket closed.", socketRef.current.readyState);
|
||||||
clearInterval(interval);
|
clearInterval(interval);
|
||||||
|
|
||||||
|
if (account) {
|
||||||
|
console.log("WebSocket closed--------.", account);
|
||||||
setTimeout(connect_socket, 1000);
|
setTimeout(connect_socket, 1000);
|
||||||
retryInterval = Math.min(retryInterval * 2, maxInterval);
|
// retryInterval = Math.min(retryInterval * 2, maxInterval);
|
||||||
|
}
|
||||||
|
|
||||||
// console.log("WebSocket disconnected.");
|
// console.log("WebSocket disconnected.");
|
||||||
};
|
};
|
||||||
// 连接错误时触发
|
// 连接错误时触发
|
||||||
|
@ -90,22 +115,15 @@ const WebSocketComponent = ({ getData, authInfo }) => {
|
||||||
// 重新连接
|
// 重新连接
|
||||||
socketRef.current?.close();
|
socketRef.current?.close();
|
||||||
};
|
};
|
||||||
}
|
// 响应服务器的 ping
|
||||||
// 组件卸载时关闭WebSocket连接
|
// socketRef.current.on("ping", () => {
|
||||||
return () => {
|
// socketRef.current.send("pong");
|
||||||
// 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 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 (
|
||||||
|
|
Loading…
Reference in New Issue