退出登录,断开socket

This commit is contained in:
al 2024-12-24 20:21:53 +08:00
parent 339aab3baa
commit 01a6ae4c9e
8 changed files with 63 additions and 39 deletions

View File

@ -9,7 +9,6 @@ import {
Divider,
Checkbox,
Toast,
Image,
Dialog,
Popup,
} from "antd-mobile";
@ -21,10 +20,8 @@ import { saveUserInfo, get, save } from "@/utils/storeInfo";
import { connect } from "react-redux";
import { cryptoPassword } from "@/utils/crypto";
import requireAPI from "@/utils/requireAPI";
import { signOut, signIn, checkAuth } from "@/utils/auth";
import { signIn, checkAuth } from "@/utils/auth";
import OwnInput from "@/components/OwnInput";
import Link from "next/link";
import OwnImage from "@/components/OwnImage";
import OwnIcon from "@/components/OwnIcon";
/*
params格式

View File

@ -6,7 +6,9 @@ import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faAngleLeft, faAngleRight } from "@fortawesome/free-solid-svg-icons";
import { useRouter } from "next/navigation";
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();
return (
<div>
@ -36,7 +38,9 @@ export default function Setting() {
/>
}
onClick={() => {
handleLogout();
handleLogout().then(() => {
handleLogin({ isSignin: false, userToken: null });
});
router.push("/login");
}}
>
@ -125,3 +129,8 @@ export default function Setting() {
</div>
);
}
const mapDispatchToProps = {
handleLogin,
};
export default connect(null, mapDispatchToProps)(Setting);

View File

@ -106,6 +106,7 @@ const MessageList = ({ mid, changeNoticeCount, refInstance, noticeCount }) => {
});
return;
}
changeNoticeCount(_data.data.total);
const noticeObj = _data.data.result;
setData(noticeObj);
setInfoItems((old) => {

View File

@ -43,7 +43,7 @@ export default function NoticeItem({ leftIcon, hasLink, data }) {
<div
className="flex flex-row justify-between flex-1 px-4 items-center"
onClick={() => {
if (data.is_valid) {
if (!data.is_valid) {
Toast.show({
icon: "fail",
content: "此链接已失效",

View File

@ -41,7 +41,7 @@ function SystemNotice({ mid, changeNoticeCount, total, ...props }) {
});
return;
}
changeNoticeCount();
changeNoticeCount(0);
received.current = true;
}

View File

@ -43,7 +43,7 @@ function BottomNav({ changeNoticeCount, changeInviter, noticeCount }) {
};
getDtata();
return () => {};
}, [noticeCount]);
}, [noticeCount, pathname]);
const setRouteActive = (value) => {
router.replace(value);
};

View File

@ -1,37 +1,45 @@
import React, { useEffect, useState, useTransition } from "react";
import React, { useEffect, useState, useRef } from "react";
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 [pending, startTransition] = useTransition();
// const [pending, startTransition] = useTransition();
const socketRef = useRef(null);
useEffect(() => {
let socket = null;
let interval = null;
const account = get("account");
function connect_socket() {
const base = baseRequest();
if (socket && socket.readyState === 1) return;
if (socketRef.current && socketRef.current.readyState === 1) return;
// WebSocket
if (!socket)
socket = new 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
// socket.on("ping", () => {
// socket.send("pong");
// socketRef.current.on("ping", () => {
// socketRef.current.send("pong");
// });
}
if (!account && socketRef.current) {
socketRef.current.close();
socketRef.current = null;
return;
}
connect_socket();
if (socket) {
if (socketRef.current) {
// 使wss
//
socket.onopen = () => {
// console.log("WebSocket connected.", socket.readyState);
socketRef.current.onopen = () => {
// console.log("WebSocket connected.", socketRef.current.readyState);
// 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]") {
var reader = new FileReader();
reader.readAsText(event.data, "utf-8");
@ -39,13 +47,13 @@ const WebSocketComponent = ({ getData }) => {
try {
const data = JSON.parse(reader.result);
if (data.t === 2 && data.msg.ping_interval) {
socket.send("ping");
socketRef.current.send("ping");
interval = setInterval(() => {
// ping
socket.send("ping");
socketRef.current.send("ping");
// ping
// socket.on("ping", () => {
// socket.send("pong");
// socketRef.current.on("ping", () => {
// socketRef.current.send("pong");
// });
}, data.msg.ping_interval * 1000);
}
@ -59,23 +67,25 @@ const WebSocketComponent = ({ getData }) => {
};
//
socket.onclose = () => {
socketRef.current.onclose = () => {
clearInterval(interval);
connect_socket();
// console.log("WebSocket disconnected.");
};
//
socket.onerror = (error) => {
socketRef.current.onerror = (error) => {
// console.error("WebSocket error:", error);
};
}
// WebSocket
return () => {
// console.log("state", socket.readyState);
socket?.readyState === WebSocket.OPEN && socket?.close();
// console.log("state", socketRef.current.readyState);
if (socketRef.current)
socketRef.current?.readyState === WebSocket.OPEN &&
socketRef.current?.close();
clearInterval(interval);
};
}, []); // effect
}, [authInfo]); // effect
// return <Error statusCode={500} />;
return (
<div>
@ -89,4 +99,9 @@ const WebSocketComponent = ({ getData }) => {
);
};
export default WebSocketComponent;
const mapStateToProps = ({ reducer }) => {
return {
authInfo: reducer.authInfo,
};
};
export default connect(mapStateToProps, null)(WebSocketComponent);

View File

@ -56,9 +56,11 @@ export default function customFetch(
// // 检查响应状态码
if (!response.ok) {
try {
throw new Error(
"Network response was not ok " + response.statusText
);
} catch (error) {}
}
// console.log("response-----", response);
// const contentLength = response.headers.get("Content-Length");
@ -79,13 +81,13 @@ export default function customFetch(
.read()
.then(({ done, value }) => {
if (done) {
controller.close();
controller?.close();
return;
}
// loaded += value.byteLength;
// const progress = (loaded / total) * 100;
// console.log(`Progress: ${progress.toFixed(2)}%`);
controller.enqueue(value);
controller?.enqueue(value);
push();
})
.catch((error) => {