Skip to content

Commit

Permalink
tray!
Browse files Browse the repository at this point in the history
  • Loading branch information
steve02081504 committed Jan 29, 2025
1 parent 4837ecd commit 1f8fce4
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 13 deletions.
19 changes: 9 additions & 10 deletions src/server/ipc_server.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import net from 'node:net'
import { console } from '../scripts/console.mjs'
import { loadShell } from './managers/shell_manager.mjs'
import { shutdown } from "./on_shutdown.mjs";

const IPC_PORT = 16698 // 选择一个不太可能冲突的端口

Expand Down Expand Up @@ -34,9 +35,9 @@ export class IPCManager {
if (err.code === 'EADDRINUSE') {
console.log('另一个实例正在运行')
resolve(false) // 服务器已在运行
} else
} else
reject(err)

})

this.server.listen(IPC_PORT, () => {
Expand All @@ -56,12 +57,10 @@ export class IPCManager {
const result = await shell.ArgumentsHandler(username, args)
socket.write(JSON.stringify({ status: 'ok', result }) + '\n') // 添加换行符作为结束
}
else if (command.type === 'shutdown')
process.exit(0)

else
else if (command.type === 'shutdown')
shutdown()
else
socket.write(JSON.stringify({ status: 'error', message: '不支持的命令类型' }) + '\n')

} catch (err) {
console.error('处理 IPC 消息时出错:', err)
socket.write(JSON.stringify({ status: 'error', message: err.message }) + '\n')
Expand All @@ -87,11 +86,11 @@ export class IPCManager {

try {
const response = JSON.parse(message)
if (response.status === 'ok')
if (response.status === 'ok')
resolve(response.result) // 返回结果
else
else
reject(new Error(response.message || '未知错误'))

} catch (err) {
console.error('解析服务器响应失败:', err)
reject(new Error('无法解析服务器响应'))
Expand Down
2 changes: 1 addition & 1 deletion src/server/on_shutdown.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const shutdowm_functions = []
export function on_shutdown(func) {
shutdowm_functions.unshift(func)
}
function shutdown() {
export function shutdown() {
shutdowm_functions.forEach(func => func())
process.exit(0)
}
Expand Down
6 changes: 4 additions & 2 deletions src/server/server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import fileUpload from 'npm:express-fileupload'
import path from 'node:path'
import fs from 'node:fs'
import process from 'node:process'
import https from 'node:https'
import { console } from '../scripts/console.mjs'
import { registerEndpoints } from './endpoints.mjs'
import { on_shutdown } from './on_shutdown.mjs'
import { IPCManager } from './ipc_server.mjs'
import https from 'node:https' // 引入 https 模块
import { initAuth } from './auth.mjs' // 引入新的身份验证模块
import { initAuth } from './auth.mjs'
import { createTray } from "./tray.mjs";

export const app = express()

Expand Down Expand Up @@ -94,6 +95,7 @@ export async function init() {
console.freshLine('server start', 'server ready')
const titleBackup = process.title
on_shutdown(() => setWindowTitle(titleBackup))
createTray()
setDefaultWindowTitle()
console.freshLine('server start', Array(Math.floor(Math.random() * 7)).fill('fo-').join('') + 'fount!')
return true
Expand Down
29 changes: 29 additions & 0 deletions src/server/tray.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {
createTrayIcon,
destroyTrayIcon,
} from "npm:node-tray";
import { shutdown } from "./on_shutdown.mjs";
import { __dirname } from "./server.mjs";
import path from "node:path";

const trayItems = [
{
id: Symbol(),
text: "Exit",
enabled: true,
onClick: () => {
console.log("Exiting...");

destroyTrayIcon();
shutdown();
},
},
];

export function createTray() {
createTrayIcon({
icon: path.join(__dirname, "/src/public/favicon.ico"),
items: trayItems,
tooltip: `Fount`,
});
}

0 comments on commit 1f8fce4

Please sign in to comment.