您可以使用 websockets/socket.io 来做到这一点。
使用socket.io,基本上你只会在你的http请求回调中有一个套接字发射。
这是一个小例子:
在您的express 应用程序中,设置一个socket.io 服务器,其路由会发出焦点窗口事件:
const app = require('express')()
const http = require('http').createServer(app)
const io = require('socket.io')(http)
app.post('/focus-window, (req, res) => {
io.emit('focus-window')
res.send(200)
});
io.on('connection', (socket) => {
// connections here
console.log('a user connected');
});
http.listen(3000, () => {
console.log('listening on *:3000');
});
在您的electron 主进程中,使用socket.io-client 设置一个可以访问BrowserWindow 对象的套接字客户端,如下所示:
const { app, BrowserWindow } = require('electron')
const socket = require('socket.io-client')('http://localhost:3000')
const path = require('path')
function createWindow () {
// Create the browser window.
const win = new BrowserWindow({
width: 400,
height: 600
})
// and load the index.html of the app.
win.loadFile(path.resolve('dist/index.html'))
// setup your socket listeners
socket.on('connect', function(){
console.log('socket.io-client connected')
})
// here it is listening for the focus-window event
socket.on('focus-window', () => {
win.focus()
})
}
app.whenReady().then(createWindow)
然后您可以使用套接字发射器将信息发送到电子应用程序,然后使用电子的 ipc 将信息发送到反应渲染器进程(从概念上讲,我还没有测试过)