🍻 连接错误后会发送通知

This commit is contained in:
刘嘉伟 2023-12-01 15:16:16 +08:00
parent b957414e48
commit c0c69643fd

View File

@ -1,4 +1,4 @@
import {app, ipcMain} from "electron"; import {app, ipcMain, Notification} from "electron";
import {Config, getConfig} from "../storage/config"; import {Config, getConfig} from "../storage/config";
import {listProxy} from "../storage/proxy"; import {listProxy} from "../storage/proxy";
import {getVersionById} from "../storage/version"; import {getVersionById} from "../storage/version";
@ -6,15 +6,13 @@ import treeKill from "tree-kill";
const fs = require("fs"); const fs = require("fs");
const path = require("path"); const path = require("path");
const {exec, spawn} = require("child_process"); const {exec, spawn} = require("child_process");
export let frpcProcess = null; export let frpcProcess = null;
const runningCmd = { const runningCmd = {
commandPath: null, commandPath: null,
configPath: null configPath: null
}; };
let frpcStatusListener = null;
/** /**
* *
@ -125,8 +123,22 @@ const startFrpcProcess = (commandPath: string, configPath: string) => {
console.log("启动错误", data) console.log("启动错误", data)
stopFrpcProcess() stopFrpcProcess()
}); });
frpcStatusListener = setInterval(() => {
const status = frpcProcessStatus()
if (!status) {
console.log("连接已断开")
new Notification({
title: "Frpc Desktop",
body: "连接已断开,请前往日志查看原因"
}).show()
clearInterval(frpcStatusListener)
}
}, 3000)
}; };
/**
* frpc配置
*/
export const reloadFrpcProcess = () => { export const reloadFrpcProcess = () => {
if (frpcProcess && !frpcProcess.killed) { if (frpcProcess && !frpcProcess.killed) {
getConfig((err1, config) => { getConfig((err1, config) => {
@ -146,6 +158,9 @@ export const reloadFrpcProcess = () => {
} }
}; };
/**
* frpc子进程
*/
export const stopFrpcProcess = () => { export const stopFrpcProcess = () => {
if (frpcProcess) { if (frpcProcess) {
treeKill(frpcProcess.pid, (error: Error) => { treeKill(frpcProcess.pid, (error: Error) => {
@ -153,18 +168,33 @@ export const stopFrpcProcess = () => {
console.log("关闭失败", frpcProcess.pid, error) console.log("关闭失败", frpcProcess.pid, error)
} else { } else {
frpcProcess = null frpcProcess = null
clearInterval(frpcStatusListener)
} }
}) })
} }
} }
/**
* frpc子进程状态
*/
export const frpcProcessStatus = () => {
if (!frpcProcess) {
return false;
}
try {
// 发送信号给进程,如果进程存在,会正常返回
process.kill(frpcProcess.pid, 0);
return true;
} catch (error) {
// 进程不存在,抛出异常
return false;
}
}
export const initFrpcApi = () => { export const initFrpcApi = () => {
ipcMain.handle("frpc.running", async (event, args) => { ipcMain.handle("frpc.running", async (event, args) => {
if (!frpcProcess) { return frpcProcessStatus()
return false;
} else {
return !frpcProcess.killed;
}
}); });
ipcMain.on("frpc.start", async (event, args) => { ipcMain.on("frpc.start", async (event, args) => {