2024-08-24 23:29:04 +08:00
|
|
|
// src/main.ts
|
2024-08-25 22:34:21 +08:00
|
|
|
import dotenv from 'dotenv';
|
2024-08-24 23:29:04 +08:00
|
|
|
import { app, BrowserWindow, ipcMain } from 'electron';
|
|
|
|
import * as path from 'path';
|
|
|
|
import { WebSocketServer } from 'ws';
|
|
|
|
|
2024-08-25 22:34:21 +08:00
|
|
|
// Load environment variables from .env file
|
|
|
|
dotenv.config();
|
|
|
|
|
2024-08-24 23:29:04 +08:00
|
|
|
let server: WebSocketServer | null = null;
|
2024-08-25 22:34:21 +08:00
|
|
|
let port: number = process.env.PORT ? parseInt(process.env.PORT, 10) : 8080; // 默认端口为 8080
|
2024-08-24 23:29:04 +08:00
|
|
|
|
|
|
|
function createWindow() {
|
|
|
|
const win = new BrowserWindow({
|
|
|
|
width: 800,
|
|
|
|
height: 600,
|
|
|
|
webPreferences: {
|
|
|
|
preload: path.join(__dirname, 'preload.js'),
|
|
|
|
contextIsolation: true,
|
|
|
|
nodeIntegration: false,
|
|
|
|
// contextIsolation: false, // 使渲染進程能夠使用 Node.js
|
|
|
|
// nodeIntegration: true, // 使渲染進程可以使用 Node.js 模組
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
win.loadFile(path.join(__dirname, 'index.html'));
|
2024-08-25 22:34:21 +08:00
|
|
|
// win.webContents.openDevTools();
|
2024-08-24 23:29:04 +08:00
|
|
|
|
|
|
|
return win;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mainWindow: BrowserWindow | null = null;
|
|
|
|
|
2024-08-25 22:34:21 +08:00
|
|
|
// Create a function to send log messages to the renderer
|
|
|
|
function sendLogToRenderer(window: BrowserWindow, message: string) {
|
|
|
|
window.webContents.send('log-message', message);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 重寫 console.log 方法以便將訊息發送到渲染進程
|
|
|
|
const originalConsoleLog = console.log;
|
|
|
|
console.log = (...args: any[]) => {
|
|
|
|
const message = args.join(' ');
|
|
|
|
originalConsoleLog(message); // 保留原始行為
|
|
|
|
if (BrowserWindow.getAllWindows().length > 0) {
|
|
|
|
sendLogToRenderer(BrowserWindow.getAllWindows()[0], message); // 發送訊息到渲染進程
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-08-24 23:29:04 +08:00
|
|
|
app.whenReady().then(() => {
|
|
|
|
mainWindow = createWindow();
|
|
|
|
|
|
|
|
app.on('activate', () => {
|
|
|
|
if (BrowserWindow.getAllWindows().length === 0) {
|
|
|
|
createWindow();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
app.on('window-all-closed', () => {
|
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
app.quit();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// 启动 WebSocket 服务器
|
|
|
|
ipcMain.on('start-websocket', (event, portNumber: number) => {
|
|
|
|
if (server) {
|
|
|
|
server.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
port = portNumber || 8080;
|
|
|
|
|
|
|
|
server = new WebSocketServer({ port: port });
|
|
|
|
|
2024-08-25 22:34:21 +08:00
|
|
|
server.on('connection', (socket, request) => {
|
|
|
|
const ip = request.socket.remoteAddress.replace("::ffff:", "") || 'Unknown IP';
|
|
|
|
console.log(`Client connected from IP: ${ip}`);
|
2024-08-24 23:29:04 +08:00
|
|
|
socket.send('Welcome to the WebSocket server');
|
|
|
|
|
|
|
|
socket.on('message', (message: string) => {
|
2024-08-25 22:34:21 +08:00
|
|
|
console.log(`[RPC] 收到client呼叫: ${message}`);
|
2024-08-24 23:29:04 +08:00
|
|
|
socket.send(`Server received your message: ${message}`);
|
2024-08-25 22:34:21 +08:00
|
|
|
console.log(`[RPC] 回傳client呼叫: ${message}`);
|
2024-08-24 23:29:04 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('close', () => {
|
|
|
|
console.log('Client disconnected');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-08-25 22:34:21 +08:00
|
|
|
event.reply('websocket-status', `WebSocket server started on port ${port}`);
|
2024-08-24 23:29:04 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
// 关闭 WebSocket 服务器
|
|
|
|
ipcMain.on('stop-websocket', (event) => {
|
|
|
|
if (server) {
|
|
|
|
server.clients.forEach(client => {
|
|
|
|
if (client.readyState === 1) {
|
|
|
|
client.close();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
server.close((err) => {
|
|
|
|
if (err) {
|
|
|
|
event.reply('websocket-status', 'Failed to stop WebSocket server');
|
|
|
|
} else {
|
|
|
|
event.reply('websocket-status', 'WebSocket server stopped');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
event.reply('websocket-status', 'No WebSocket server is running');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// 打开开发者工具
|
|
|
|
ipcMain.on('open-devtools', () => {
|
|
|
|
mainWindow.webContents.openDevTools();
|
|
|
|
});
|