electron打包后的exe顶部菜单栏点击后实现可以切换服务器ip的功能
可以把服务器ip房子一个配置文件中,每次调用都从配置文件中获取
然后下拉选择后去修改配置文件
引用自 chatGPT:
实现 Electron 打包后的 EXE 顶部菜单栏点击后可以切换服务器 IP 的功能,可以按照以下步骤进行:
1.在 Electron 的主进程中,创建一个名为 serverList 的数组,用于保存所有可用的服务器 IP,例如:
const serverList = [
'http://192.168.1.100',
'http://192.168.1.101',
'http://192.168.1.102'
];
2.在 Electron 的主进程中,创建一个名为 switchServer 的 IPC 事件,用于在渲染进程和主进程之间传递切换服务器 IP 的请求和响应,例如:
ipcMain.on('switchServer', (event, index) => {
const serverUrl = serverList[index];
// 将新的服务器 IP 保存到本地存储中,以便下次启动应用时自动调用
// 下面的代码示例使用 electron-store 库实现本地存储
const Store = require('electron-store');
const store = new Store();
store.set('serverUrl', serverUrl);
// 向渲染进程发送切换服务器 IP 的响应
event.sender.send('switchServer', serverUrl);
});
const { ipcRenderer } = require('electron');
const switchServer = (index) => {
ipcRenderer.send('switchServer', index);
ipcRenderer.once('switchServer', (event, serverUrl) => {
// 更新本地存储中的服务器 IP
const Store = require('electron-store');
const store = new Store();
store.set('serverUrl', serverUrl);
// 在页面上显示当前服务器 IP
document.getElementById('server-ip').innerText = serverUrl;
});
};
4.在渲染进程中,创建一个名为 serverList 的数组,用于保存所有可用的服务器 IP,例如:
const serverList = [
'http://192.168.1.100',
'http://192.168.1.101',
'http://192.168.1.102'
];
5.在渲染进程中,创建一个名为 switchServer 的方法,用于调用前面创建的 switchServer 方法,例如:
const switchServer = (index) => {
window.switchServer(index);
};
<div class="dropdown">
<button class="btn btn-secondary dropdown-toggle" type="button" id="server-dropdown" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
选择服务器
</button>
<div class="dropdown-menu" aria-labelledby="server-dropdown">
<a class="dropdown-item" href="#" onclick="switchServer(0)">服务器 1</a>
<a class="dropdown-item" href="#" onclick="switchServer(1)">服务器 2</a>
<a class="dropdown-item" href="#" onclick="switchServer(2)">服务器 3</a>
</div>
</div>
<p>当前服务器: <span id="server-ip"></span></p>
在上述代码中,我们创建了一个下拉菜单,其中每个菜单项都绑定了 switchServer 方法,并传递了对应的索引值。同时,我们在页面下方显示当前服务器 IP,以便用户查看。
electron-packager <location of project> <name of project> <platform> <architecture> <electron version> <optional options>
参数说明: