如何在Electron JS中在窗口打开/关闭时进行HTTP请求

huangapple go评论61阅读模式
英文:

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) =&gt; {
return await axios.PUT(&#39;URL_HERE&#39;, _data)
}
app.on(&#39;window-all-closed&#39;, () =&gt; {
  if (process.platform !== &#39;darwin&#39;) {

    logout(someData)
    app.quit()
  }
})
 mainWindow.on(&#39;closed&#39;, function() {
    logout(someData)

    mainWindow = null
  })

My index.js (there isn't an index.html file) file is looking like this:

import React from &#39;react&#39;
import { render } from &#39;react-dom&#39;
import App  from &quot;./components/App&quot;

let root = document.createElement(&#39;div&#39;)

root.id = &#39;root&#39;
document.body.appendChild(root)

render(&lt;App /&gt;, document.getElementById(&#39;root&#39;))

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(&#39;my-channel&#39;);
​
// Send a message to the main process when the window is opened
ipcRenderer.on(&#39;window-open&#39;, () =&gt; {
  channel.send(&#39;open&#39;);
});
​
// Send a message to the main process when the window is closed
ipcRenderer.on(&#39;window-close&#39;, () =&gt; {
  channel.send(&#39;close&#39;);
});
​
// In the main process, listen for messages from the renderer process
ipcMain.on(&#39;my-channel&#39;, (event, message) =&gt; {
  // Make an HTTP request to update the user&#39;s login status
  fetch(&#39;https://example.com/api/update-login-status&#39;, {
    method: &#39;POST&#39;,
    headers: {
      &#39;Content-Type&#39;: &#39;application/json&#39;,
    },
    body: JSON.stringify({
      username: &#39;user@example.com&#39;,
      loggedIn: true,
    }),
  });
});

I hope this helps!

huangapple
  • 本文由 发表于 2023年7月20日 20:18:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76729799.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定