feat: Contro逻辑调整

This commit is contained in:
song_xiao_lin
2024-05-15 14:43:04 +08:00
committed by go0p
parent 8d61a5f5e8
commit 42b0e958ba
2 changed files with 34 additions and 48 deletions
+2 -9
View File
@@ -3,10 +3,10 @@ const connectWebsocket = url => {
disconnectWebsocket() disconnectWebsocket()
socket = new WebSocket(url); socket = new WebSocket(url);
socket.onopen = () => { socket.onopen = () => {
chrome.runtime.sendMessage({status: "connected"}) chrome.runtime.sendMessage({connected: true})
} }
socket.onclose = () => { socket.onclose = () => {
chrome.runtime.sendMessage({status: "disconnected"}) chrome.runtime.sendMessage({connected: false})
} }
} }
@@ -44,7 +44,6 @@ console.info("Chrome Extenstion Background is loaded")
let proxyHost = ""; let proxyHost = "";
chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) { chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
console.log(msg);
if (msg.action === "connect") { if (msg.action === "connect") {
console.info("Start to connect websocket") console.info("Start to connect websocket")
const host = msg['host'] || "127.0.0.1" const host = msg['host'] || "127.0.0.1"
@@ -52,12 +51,6 @@ chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
connectWebsocket(`ws://${host}:${port}/?token=${"a"}`) connectWebsocket(`ws://${host}:${port}/?token=${"a"}`)
} else if (msg.action === 'disconnect') { } else if (msg.action === 'disconnect') {
disconnectWebsocket() disconnectWebsocket()
} else if (msg.action === "status") {
if (socket) {
chrome.runtime.sendMessage({connected: true})
} else {
chrome.runtime.sendMessage({connected: false})
}
} else if (msg.action === 'setproxy') { } else if (msg.action === 'setproxy') {
chrome.proxy.settings.set({ chrome.proxy.settings.set({
value: { value: {
+32 -39
View File
@@ -8,7 +8,6 @@ import {
RefreshIcon, RefreshIcon,
XIcon, XIcon,
} from "@assets/icon/icon"; } from "@assets/icon/icon";
import { useUpdateEffect } from "ahooks";
import { wsc } from "@network/chrome"; import { wsc } from "@network/chrome";
import "./Contro.css"; import "./Contro.css";
@@ -16,56 +15,50 @@ interface ControProps {}
export const Contro: React.FC<ControProps> = () => { export const Contro: React.FC<ControProps> = () => {
const [isEdit, setIsEdit] = useState<boolean>(false); const [isEdit, setIsEdit] = useState<boolean>(false);
const [connected, setConnected] = useState(false); const [connected, setConnected] = useState(false);
const [init, setInit] = React.useState(false);
const [autoFindFailedReason, setAutoFindFailedReason] = useState<string>(""); const [autoFindFailedReason, setAutoFindFailedReason] = useState<string>("");
const [enginePort, setEnginePort] = useState<string>(""); const [enginePort, setEnginePort] = useState<string>("");
const [enginePortTemp, setEnginePortTemp] = useState<string>(enginePort); const [enginePortTemp, setEnginePortTemp] = useState<string>(enginePort);
useEffect(() => { useEffect(() => {
const updateStatus = () => { findPort(11212, 11222);
wsc.updateWSCStatus(); }, []);
}; const findPort = (port: number, max: number) => {
updateStatus(); const ws = new WebSocket(`ws://127.0.0.1:${port}`);
const id = setInterval(updateStatus, 500); ws.onclose = (e: CloseEvent) => {
if (e.reason !== `FoundYakitWebSocketController` && port + 1 <= max) {
setTimeout(() => findPort(port + 1, max), 200);
}
if (port + 1 > max) {
setConnected(false);
setEnginePort("");
setAutoFindFailedReason("Cannot found Yakit");
}
};
ws.onopen = () => {
setConnected(true);
setEnginePort(port + "");
setAutoFindFailedReason("");
ws.close(1000, "FoundYakitWebSocketController");
connectPort(port);
};
};
const connectPort = (port: number) => {
wsc.connect(port);
wsc.onWSCMessage((req: { connected: boolean }) => { wsc.onWSCMessage((req: { connected: boolean }) => {
if (req["connected"] === undefined) { if (req["connected"] === undefined) {
return; return;
} }
setConnected(req.connected); setConnected(req.connected);
setInit(true); if (req.connected === false) {
setAutoFindFailedReason("Yakit WebSocket Controller Port is not right");
} else {
setAutoFindFailedReason("");
}
}); });
return () => {
clearInterval(id);
};
}, []);
const findPort = (port: number, max: number) => {
const ws = new WebSocket(`ws://127.0.0.1:${port}`);
ws.onclose = (e: CloseEvent) => {
if (e.reason !== `FoundYakitWebSocketController` && port + 1 <= max) {
setTimeout(() => findPort(port + 1, max), 300);
}
if (port + 1 > max) {
setAutoFindFailedReason(
"Cannot found Yakit or Yakit WebSocket Controller Port is not right"
);
}
};
ws.onopen = () => {
setEnginePort(port + "");
ws.close(1000, "FoundYakitWebSocketController");
};
}; };
useUpdateEffect(() => {
if (!init || connected) {
return;
}
findPort(11212, 11222);
}, [connected, init]);
const safeConnected = useMemo(() => { const safeConnected = useMemo(() => {
return connected && enginePort; return connected && enginePort;
}, [connected, enginePort]); }, [connected, enginePort]);
@@ -109,7 +102,7 @@ export const Contro: React.FC<ControProps> = () => {
if (enginePortTemp) { if (enginePortTemp) {
setIsEdit(false); setIsEdit(false);
setEnginePort(enginePortTemp); setEnginePort(enginePortTemp);
wsc.connect(Number(enginePortTemp)); connectPort(Number(enginePortTemp));
} }
}} }}
/> />
@@ -151,7 +144,7 @@ export const Contro: React.FC<ControProps> = () => {
className="grey-icon icon-active" className="grey-icon icon-active"
onClick={() => { onClick={() => {
if (enginePort) { if (enginePort) {
wsc.connect(Number(enginePort)); connectPort(Number(enginePort));
} else { } else {
findPort(11212, 11222); findPort(11212, 11222);
} }