forked from LanwyWriteXU/DoIt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
49 lines (40 loc) · 1.1 KB
/
main.js
File metadata and controls
49 lines (40 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
const { app, BrowserWindow } = require('electron')
const path = require('path')
const WebSocket = require('ws')
let wss = null
app.whenReady().then(() => {
// 创建浏览器窗口
const mainWindow = new BrowserWindow({
width: 1280,
height: 720,
icon: path.join(__dirname, 'icon.png'),
title: 'DoIt!',
autoHideMenuBar: true,
webPreferences: {
nodeIntegration: true,
contextIsolation: true, // 启用上下文隔离
preload: path.join(__dirname, 'preload.js') // 添加预加载脚本
}
})
// 加载应用界面
mainWindow.loadFile('index.html')
// 启动WebSocket服务器
wss = new WebSocket.Server({ port: 8080 }, () => {
console.log('WS server ready on port 8080')
})
// 处理连接
wss.on('connection', (ws) => {
ws.on('message', (message) => {
console.log('Received:', message.toString())
})
})
// 退出时关闭服务器
app.on('will-quit', () => {
if (wss) {
wss.close()
wss = null
}
})
// 在创建BrowserWindow后添加
mainWindow.webContents.openDevTools({ mode: 'detach' });
})