英文:
How make HTTP requests in Electron JS, upon the window opening/closing
问题
我正在使用Electron JS(第一次使用),与React JS一起工作。
我尝试在窗口打开/关闭时进行HTTP请求,但无法弄清楚如何操作。从基本的控制台日志中,我发现当我从浏览器关闭应用程序时,下面的这两个函数会被触发。在应用程序关闭之前,我尝试进行HTTP请求以更新用户的登录状态。
const logout = async (_data) => {
return await axios.PUT('URL_HERE', _data)
}
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
logout(someData)
app.quit()
}
})
mainWindow.on('closed', function() {
logout(someData)
mainWindow = null
})
我的index.js(没有index.html文件)文件如下:
import React from 'react'
import { render } from 'react-dom'
import App from "./components/App"
let root = document.createElement('div')
root.id = 'root'
document.body.appendChild(root)
render(<App />, document.getElementById('root'))
我该如何处理这个问题?非常感谢任何帮助。
英文:
I'm working with Electron JS (for the first time), alongside React JS.
I'm trying to make HTTP requests when the window is opened/closed, but can't figure out how. From basic console logs, I've found these 2 functions below are fired when I close down the app from the browser. Before the app closes, I'm trying to make a HTTP request to update a users login status.
const logout = async (_data) => {
return await axios.PUT('URL_HERE', _data)
}
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
logout(someData)
app.quit()
}
})
mainWindow.on('closed', function() {
logout(someData)
mainWindow = null
})
My index.js (there isn't an index.html file) file is looking like this:
import React from 'react'
import { render } from 'react-dom'
import App from "./components/App"
let root = document.createElement('div')
root.id = 'root'
document.body.appendChild(root)
render(<App />, document.getElementById('root'))
How can I go about this? Any help is massively appreciated.
答案1
得分: 0
为了在Electron中在窗口打开或关闭时进行HTTP请求,您可以使用ipcRenderer.send()
方法。这个方法允许您从渲染进程向主进程发送消息。
要使用ipcRenderer.send()
,您首先需要创建一个IPC通道。可以通过调用ipcRenderer.createChannel()
方法来实现。这个方法的第一个参数是通道的名称。
一旦您创建了一个IPC通道,就可以通过调用ipcRenderer.send()
方法向主进程发送消息。这个方法的第一个参数是通道的名称,第二个参数是消息。
主进程将接收到消息,然后可以执行任何必要的操作。在您的情况下,您可以使用消息来进行HTTP请求以更新用户的登录状态。
以下是如何使用ipcRenderer.send()
在窗口打开或关闭时进行HTTP请求的示例:
// 创建一个IPC通道
const channel = ipcRenderer.createChannel('my-channel');
// 当窗口打开时向主进程发送消息
ipcRenderer.on('window-open', () => {
channel.send('open');
});
// 当窗口关闭时向主进程发送消息
ipcRenderer.on('window-close', () => {
channel.send('close');
});
// 在主进程中,监听来自渲染进程的消息
ipcMain.on('my-channel', (event, message) => {
// 发送HTTP请求以更新用户的登录状态
fetch('https://example.com/api/update-login-status', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
username: 'user@example.com',
loggedIn: true,
}),
});
});
希望这有所帮助!
英文:
To make an HTTP request when the window is opened or closed in Electron, you can use the ipcRenderer.send()
method. This method allows you to send a message to the main process from the renderer process.
To use ipcRenderer.send()
, you first need to create an IPC channel. This can be done by calling the ipcRenderer.createChannel()
method. The first argument to this method is the name of the channel.
Once you have created an IPC channel, you can send a message to the main process by calling the ipcRenderer.send()
method. The first argument to this method is the name of the channel, and the second argument is the message.
The main process will receive the message and can then take any action that it needs to. In your case, you can use the message to make an HTTP request to update a user's login status.
Here is an example of how to use ipcRenderer.send()
to make an HTTP request when the window is opened or closed:
// Create an IPC channel
const channel = ipcRenderer.createChannel('my-channel');
// Send a message to the main process when the window is opened
ipcRenderer.on('window-open', () => {
channel.send('open');
});
// Send a message to the main process when the window is closed
ipcRenderer.on('window-close', () => {
channel.send('close');
});
// In the main process, listen for messages from the renderer process
ipcMain.on('my-channel', (event, message) => {
// Make an HTTP request to update the user's login status
fetch('https://example.com/api/update-login-status', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
username: 'user@example.com',
loggedIn: true,
}),
});
});
I hope this helps!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论