退出登录,断开socket
This commit is contained in:
parent
339aab3baa
commit
01a6ae4c9e
|
@ -9,7 +9,6 @@ import {
|
||||||
Divider,
|
Divider,
|
||||||
Checkbox,
|
Checkbox,
|
||||||
Toast,
|
Toast,
|
||||||
Image,
|
|
||||||
Dialog,
|
Dialog,
|
||||||
Popup,
|
Popup,
|
||||||
} from "antd-mobile";
|
} from "antd-mobile";
|
||||||
|
@ -21,10 +20,8 @@ import { saveUserInfo, get, save } from "@/utils/storeInfo";
|
||||||
import { connect } from "react-redux";
|
import { connect } from "react-redux";
|
||||||
import { cryptoPassword } from "@/utils/crypto";
|
import { cryptoPassword } from "@/utils/crypto";
|
||||||
import requireAPI from "@/utils/requireAPI";
|
import requireAPI from "@/utils/requireAPI";
|
||||||
import { signOut, signIn, checkAuth } from "@/utils/auth";
|
import { signIn, checkAuth } from "@/utils/auth";
|
||||||
import OwnInput from "@/components/OwnInput";
|
import OwnInput from "@/components/OwnInput";
|
||||||
import Link from "next/link";
|
|
||||||
import OwnImage from "@/components/OwnImage";
|
|
||||||
import OwnIcon from "@/components/OwnIcon";
|
import OwnIcon from "@/components/OwnIcon";
|
||||||
/*
|
/*
|
||||||
params格式:
|
params格式:
|
||||||
|
|
|
@ -6,7 +6,9 @@ import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
||||||
import { faAngleLeft, faAngleRight } from "@fortawesome/free-solid-svg-icons";
|
import { faAngleLeft, faAngleRight } from "@fortawesome/free-solid-svg-icons";
|
||||||
import { useRouter } from "next/navigation";
|
import { useRouter } from "next/navigation";
|
||||||
import { handleLogout } from "@/api/public";
|
import { handleLogout } from "@/api/public";
|
||||||
export default function Setting() {
|
import { connect } from "react-redux";
|
||||||
|
import { handleLogin } from "@/store/actions";
|
||||||
|
function Setting({ handleLogin }) {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
|
@ -36,7 +38,9 @@ export default function Setting() {
|
||||||
/>
|
/>
|
||||||
}
|
}
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
handleLogout();
|
handleLogout().then(() => {
|
||||||
|
handleLogin({ isSignin: false, userToken: null });
|
||||||
|
});
|
||||||
router.push("/login");
|
router.push("/login");
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
|
@ -125,3 +129,8 @@ export default function Setting() {
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const mapDispatchToProps = {
|
||||||
|
handleLogin,
|
||||||
|
};
|
||||||
|
export default connect(null, mapDispatchToProps)(Setting);
|
||||||
|
|
|
@ -106,6 +106,7 @@ const MessageList = ({ mid, changeNoticeCount, refInstance, noticeCount }) => {
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
changeNoticeCount(_data.data.total);
|
||||||
const noticeObj = _data.data.result;
|
const noticeObj = _data.data.result;
|
||||||
setData(noticeObj);
|
setData(noticeObj);
|
||||||
setInfoItems((old) => {
|
setInfoItems((old) => {
|
||||||
|
|
|
@ -43,7 +43,7 @@ export default function NoticeItem({ leftIcon, hasLink, data }) {
|
||||||
<div
|
<div
|
||||||
className="flex flex-row justify-between flex-1 px-4 items-center"
|
className="flex flex-row justify-between flex-1 px-4 items-center"
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
if (data.is_valid) {
|
if (!data.is_valid) {
|
||||||
Toast.show({
|
Toast.show({
|
||||||
icon: "fail",
|
icon: "fail",
|
||||||
content: "此链接已失效",
|
content: "此链接已失效",
|
||||||
|
|
|
@ -41,7 +41,7 @@ function SystemNotice({ mid, changeNoticeCount, total, ...props }) {
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
changeNoticeCount();
|
changeNoticeCount(0);
|
||||||
received.current = true;
|
received.current = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -43,7 +43,7 @@ function BottomNav({ changeNoticeCount, changeInviter, noticeCount }) {
|
||||||
};
|
};
|
||||||
getDtata();
|
getDtata();
|
||||||
return () => {};
|
return () => {};
|
||||||
}, [noticeCount]);
|
}, [noticeCount, pathname]);
|
||||||
const setRouteActive = (value) => {
|
const setRouteActive = (value) => {
|
||||||
router.replace(value);
|
router.replace(value);
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,37 +1,45 @@
|
||||||
import React, { useEffect, useState, useTransition } from "react";
|
import React, { useEffect, useState, useRef } from "react";
|
||||||
import baseRequest from "@/utils/baseRequest";
|
import baseRequest from "@/utils/baseRequest";
|
||||||
const WebSocketComponent = ({ getData }) => {
|
import { connect } from "react-redux";
|
||||||
|
import { get } from "@/utils/storeInfo";
|
||||||
|
const WebSocketComponent = ({ getData, authInfo }) => {
|
||||||
const [messages, setMessages] = useState([]);
|
const [messages, setMessages] = useState([]);
|
||||||
const [pending, startTransition] = useTransition();
|
// const [pending, startTransition] = useTransition();
|
||||||
|
const socketRef = useRef(null);
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
let socket = null;
|
|
||||||
let interval = null;
|
let interval = null;
|
||||||
|
const account = get("account");
|
||||||
function connect_socket() {
|
function connect_socket() {
|
||||||
const base = baseRequest();
|
const base = baseRequest();
|
||||||
if (socket && socket.readyState === 1) return;
|
|
||||||
|
if (socketRef.current && socketRef.current.readyState === 1) return;
|
||||||
// 创建WebSocket连接
|
// 创建WebSocket连接
|
||||||
if (!socket)
|
if (!socketRef.current && account)
|
||||||
socket = new WebSocket(
|
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`
|
`${process.env.NEXT_PUBLIC_WEBSOCKET_URL}/ws?b_mid=${base.b_mid}&b_dt=1&b_token=${base.b_token}&b-ch=h5`
|
||||||
);
|
);
|
||||||
|
|
||||||
// 响应服务器的 ping
|
// 响应服务器的 ping
|
||||||
// socket.on("ping", () => {
|
// socketRef.current.on("ping", () => {
|
||||||
// socket.send("pong");
|
// socketRef.current.send("pong");
|
||||||
// });
|
// });
|
||||||
}
|
}
|
||||||
|
if (!account && socketRef.current) {
|
||||||
|
socketRef.current.close();
|
||||||
|
socketRef.current = null;
|
||||||
|
return;
|
||||||
|
}
|
||||||
connect_socket();
|
connect_socket();
|
||||||
if (socket) {
|
if (socketRef.current) {
|
||||||
// 注意使用wss协议(如果服务器支持)
|
// 注意使用wss协议(如果服务器支持)
|
||||||
// 连接打开时触发
|
// 连接打开时触发
|
||||||
socket.onopen = () => {
|
socketRef.current.onopen = () => {
|
||||||
// console.log("WebSocket connected.", socket.readyState);
|
// console.log("WebSocket connected.", socketRef.current.readyState);
|
||||||
// 可以在这里发送消息到服务器,例如:socket.send('Hello Server!');
|
// 可以在这里发送消息到服务器,例如:socket.send('Hello Server!');
|
||||||
socket.send(JSON.stringify({ t: 1 }));
|
socketRef.current.send(JSON.stringify({ t: 1 }));
|
||||||
};
|
};
|
||||||
// 处理收到的消息
|
// 处理收到的消息
|
||||||
socket.onmessage = (event) => {
|
socketRef.current.onmessage = (event) => {
|
||||||
if (Object.prototype.toString.call(event.data) === "[object Blob]") {
|
if (Object.prototype.toString.call(event.data) === "[object Blob]") {
|
||||||
var reader = new FileReader();
|
var reader = new FileReader();
|
||||||
reader.readAsText(event.data, "utf-8");
|
reader.readAsText(event.data, "utf-8");
|
||||||
|
@ -39,13 +47,13 @@ const WebSocketComponent = ({ getData }) => {
|
||||||
try {
|
try {
|
||||||
const data = JSON.parse(reader.result);
|
const data = JSON.parse(reader.result);
|
||||||
if (data.t === 2 && data.msg.ping_interval) {
|
if (data.t === 2 && data.msg.ping_interval) {
|
||||||
socket.send("ping");
|
socketRef.current.send("ping");
|
||||||
interval = setInterval(() => {
|
interval = setInterval(() => {
|
||||||
// 发送 ping 给服务器
|
// 发送 ping 给服务器
|
||||||
socket.send("ping");
|
socketRef.current.send("ping");
|
||||||
// 响应服务器的 ping
|
// 响应服务器的 ping
|
||||||
// socket.on("ping", () => {
|
// socketRef.current.on("ping", () => {
|
||||||
// socket.send("pong");
|
// socketRef.current.send("pong");
|
||||||
// });
|
// });
|
||||||
}, data.msg.ping_interval * 1000);
|
}, data.msg.ping_interval * 1000);
|
||||||
}
|
}
|
||||||
|
@ -59,23 +67,25 @@ const WebSocketComponent = ({ getData }) => {
|
||||||
};
|
};
|
||||||
|
|
||||||
// 连接关闭时触发
|
// 连接关闭时触发
|
||||||
socket.onclose = () => {
|
socketRef.current.onclose = () => {
|
||||||
clearInterval(interval);
|
clearInterval(interval);
|
||||||
connect_socket();
|
connect_socket();
|
||||||
// console.log("WebSocket disconnected.");
|
// console.log("WebSocket disconnected.");
|
||||||
};
|
};
|
||||||
// 连接错误时触发
|
// 连接错误时触发
|
||||||
socket.onerror = (error) => {
|
socketRef.current.onerror = (error) => {
|
||||||
// console.error("WebSocket error:", error);
|
// console.error("WebSocket error:", error);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
// 组件卸载时关闭WebSocket连接
|
// 组件卸载时关闭WebSocket连接
|
||||||
return () => {
|
return () => {
|
||||||
// console.log("state", socket.readyState);
|
// console.log("state", socketRef.current.readyState);
|
||||||
socket?.readyState === WebSocket.OPEN && socket?.close();
|
if (socketRef.current)
|
||||||
|
socketRef.current?.readyState === WebSocket.OPEN &&
|
||||||
|
socketRef.current?.close();
|
||||||
clearInterval(interval);
|
clearInterval(interval);
|
||||||
};
|
};
|
||||||
}, []); // 空依赖数组表示这个effect只在组件挂载时运行一次
|
}, [authInfo]); // 空依赖数组表示这个effect只在组件挂载时运行一次
|
||||||
// return <Error statusCode={500} />;
|
// return <Error statusCode={500} />;
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
|
@ -89,4 +99,9 @@ const WebSocketComponent = ({ getData }) => {
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export default WebSocketComponent;
|
const mapStateToProps = ({ reducer }) => {
|
||||||
|
return {
|
||||||
|
authInfo: reducer.authInfo,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
export default connect(mapStateToProps, null)(WebSocketComponent);
|
||||||
|
|
|
@ -56,9 +56,11 @@ export default function customFetch(
|
||||||
|
|
||||||
// // 检查响应状态码
|
// // 检查响应状态码
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(
|
try {
|
||||||
"Network response was not ok " + response.statusText
|
throw new Error(
|
||||||
);
|
"Network response was not ok " + response.statusText
|
||||||
|
);
|
||||||
|
} catch (error) {}
|
||||||
}
|
}
|
||||||
// console.log("response-----", response);
|
// console.log("response-----", response);
|
||||||
// const contentLength = response.headers.get("Content-Length");
|
// const contentLength = response.headers.get("Content-Length");
|
||||||
|
@ -79,13 +81,13 @@ export default function customFetch(
|
||||||
.read()
|
.read()
|
||||||
.then(({ done, value }) => {
|
.then(({ done, value }) => {
|
||||||
if (done) {
|
if (done) {
|
||||||
controller.close();
|
controller?.close();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// loaded += value.byteLength;
|
// loaded += value.byteLength;
|
||||||
// const progress = (loaded / total) * 100;
|
// const progress = (loaded / total) * 100;
|
||||||
// console.log(`Progress: ${progress.toFixed(2)}%`);
|
// console.log(`Progress: ${progress.toFixed(2)}%`);
|
||||||
controller.enqueue(value);
|
controller?.enqueue(value);
|
||||||
push();
|
push();
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
|
|
Loading…
Reference in New Issue