如何禁用Electron应用连接本地服务器时的错误弹窗?
问题描述
我开发了一款需连接本地服务器才能运行的Electron应用,启动后通过Node.js的net库尝试连接服务器,若1秒内未连接成功则自动重试,连接成功后正常运行。目前遇到的问题是:当客户端无法找到或连接服务器时,会弹出错误弹窗(Ubuntu和Windows系统均会出现),我希望禁用该弹窗,而非解决连接错误本身。
相关代码
main.js
const express = require('express'); const ChatApp = express(); require('./backend/scripts/javascript/net_receiver.js'); require('./backend/scripts/javascript/websocket_receiver.js'); ChatApp.use(express.static('./frontend')); ChatApp.listen(3003, function() { console.log('\nControl Panel Server Running on Port 3003'); }); const { app, BrowserWindow } = require("electron"); app.commandLine.appendSwitch('ignore-certificate-errors'); process.env['ELECTRON_DISABLE_SECURITY_WARNINGS'] = true let mainWindow; function createWindow() { mainWindow = new BrowserWindow( { icon: 'frontend/chat_app/css/App_Logo.jpg', width: 700, height: 830, //autoHideMenuBar: true, //fullscreen: true, webPreferences: { nodeIntegration: true, } }); mainWindow.loadURL("http://localhost:3003"); mainWindow.on("closed", function () { mainWindow = null; }); } app.on("ready", createWindow); app.on("resize", function (e, x, y) { mainWindow.setSize(x, y); }); app.on("window-all-closed", function () { if (process.platform !== "darwin") { app.quit(); } }); app.on("activate", function () { if (mainWindow === null) { createWindow(); } });
net_client_sender.js
const net = require('net'); const client = new net.Socket(); const settings = require("../../settings/comms_settings.json"); netClient = {} netClient.client = client; netClient.ConnectToPLC = () => { client.connect(settings["modbusPORT"], settings["plcIPv4"]); } netClient.SendRequest = (msg) => { client.write(msg); } module.exports = netClient;
net_client_receiver.js
api = require("./api.js"); netClient = require("./net_client.js"); var connected = false; /*Check if there is a connection to the PLC,*/ /*if not wait a moment and try to connect again*/ var CheckConnection = () => { /*if there is no connection to PLC*/ var connectionTester = setInterval(() => { if (connected === false) { console.log("Connection to PLC failed!"); console.log("Retrying connection...\n\n"); /*Retry connection to PLC*/ netClient.ConnectToPLC(); } /*if there is a connection to the PLC*/ else { /*stop trying to connect to the PLC*/ clearInterval(connectionTester); } }, 1000); } netClient.client.on("timeout", () => { connected = false; CheckConnection(); }); netClient.client.on("connect", () => { connected = true; api.StartMainLoop(); }); netClient.client.on("data", (data) => { api.ReadModbusMessage(data); }); netClient.ConnectToPLC(); CheckConnection();
解决方案
Electron弹出的系统错误弹窗,本质是net.Socket触发的未捕获错误被Electron转化为系统级提示。只需为socket添加error事件监听器,手动捕获错误即可禁用弹窗:
步骤1:添加错误事件监听
修改net_client_receiver.js,在现有事件监听后添加以下代码:
netClient.client.on("error", (err) => { connected = false; console.log("Connection error:", err.message); // 无需额外操作,CheckConnection会自动执行重试逻辑 });
步骤2:优化连接重试逻辑(可选)
为避免重复使用已失效的socket实例,可修改net_client_sender.js的ConnectToPLC方法,每次重试时创建新的socket并设置超时:
netClient.ConnectToPLC = () => { // 销毁旧的socket连接(如果存在) if (!client.destroyed) { client.destroy(); } // 创建新的socket实例 netClient.client = new net.Socket(); client = netClient.client; // 设置1秒连接超时,和你的重试间隔匹配 client.setTimeout(1000); client.connect(settings["modbusPORT"], settings["plcIPv4"]); }
修改完成后,未处理的连接错误会被捕获,Electron将不再弹出系统级错误弹窗,同时完全保留你的自动重试逻辑。
内容的提问来源于stack exchange,提问作者user19365766
相关产品推荐
相关产品推荐

