如何从另一个 npm 进程访问 electron 进程?
How to access the electron process from another npm process?
我制作了一个脚本,可以打开一个 TCP 服务器并侦听传入的请求,然后创建一个 windows 通知。这是代码:
const notifier = require('node-notifier');
const path = require('path');
const net = require('net');
const port = 7070;
const host = '';
const server = net.createServer();
server.listen(port, host, () => {
console.log('TCP Server is running on port ' + port + '.');
});
let sockets = [];
server.on('connection', function(sock) {
console.log('CONNECTED: ' + sock.remoteAddress + ':' + sock.remotePort);
sockets.push(sock);
sock.on('data', function(data) {
var tryCatch = true;
try {
JSON.parse(data);
} catch (err) {
tryCatch = err;
}
if (tryCatch == true) {
var JSONdata = JSON.parse(data);
if (JSONdata["action"] == "notification") {
notifier.notify({
title: 'Recived Message',
message: JSONdata["message"],
icon: path.join(__dirname, 'icon.png'),
actions: ["OK", "Abbrechen"]
},
(err, data) => {
console.log('Waited');
console.log(JSON.stringify({ err, data }));
sock.write(JSON.stringify({ err, data }));
sock.write('\r');
sock.destroy();
}
);
} else if (JSONdata["action"] == "closeServer") {
sock.destroy();
server.close();
}
} else {
sock.write(tryCatch.message);
sock.destroy();
}
});
// Add a 'close' event handler to this instance of socket
sock.on('close', function(data) {
let index = sockets.findIndex(function(o) {
return o.remoteAddress === sock.remoteAddress && o.remotePort === sock.remotePort;
})
if (index !== -1) sockets.splice(index, 1);
console.log('CLOSED: ' + sock.remoteAddress + ' ' + sock.remotePort);
// server.close();
});
});
该脚本没有问题。现在我想将它与我的电子应用程序连接起来。我想从这个 npm 进程访问电子应用程序,例如打开一个页面。但我不知道如何从外部和外部访问电子进程,我的意思是从另一个 npm 进程。希望有人能帮助我或指出正确的方向。我感谢每一个答案或信息。
说起来简单。我没有找到关于如何访问另一个进程的解决方案,但是如果用户试图打开另一个 window,你可以只 vorbidd 或拦截和中断。然后您可以生成自己的 window 并使用 ipcMain 和 ipcRender 模块将 smth 发送到所需的 window.
我制作了一个脚本,可以打开一个 TCP 服务器并侦听传入的请求,然后创建一个 windows 通知。这是代码:
const notifier = require('node-notifier');
const path = require('path');
const net = require('net');
const port = 7070;
const host = '';
const server = net.createServer();
server.listen(port, host, () => {
console.log('TCP Server is running on port ' + port + '.');
});
let sockets = [];
server.on('connection', function(sock) {
console.log('CONNECTED: ' + sock.remoteAddress + ':' + sock.remotePort);
sockets.push(sock);
sock.on('data', function(data) {
var tryCatch = true;
try {
JSON.parse(data);
} catch (err) {
tryCatch = err;
}
if (tryCatch == true) {
var JSONdata = JSON.parse(data);
if (JSONdata["action"] == "notification") {
notifier.notify({
title: 'Recived Message',
message: JSONdata["message"],
icon: path.join(__dirname, 'icon.png'),
actions: ["OK", "Abbrechen"]
},
(err, data) => {
console.log('Waited');
console.log(JSON.stringify({ err, data }));
sock.write(JSON.stringify({ err, data }));
sock.write('\r');
sock.destroy();
}
);
} else if (JSONdata["action"] == "closeServer") {
sock.destroy();
server.close();
}
} else {
sock.write(tryCatch.message);
sock.destroy();
}
});
// Add a 'close' event handler to this instance of socket
sock.on('close', function(data) {
let index = sockets.findIndex(function(o) {
return o.remoteAddress === sock.remoteAddress && o.remotePort === sock.remotePort;
})
if (index !== -1) sockets.splice(index, 1);
console.log('CLOSED: ' + sock.remoteAddress + ' ' + sock.remotePort);
// server.close();
});
});
该脚本没有问题。现在我想将它与我的电子应用程序连接起来。我想从这个 npm 进程访问电子应用程序,例如打开一个页面。但我不知道如何从外部和外部访问电子进程,我的意思是从另一个 npm 进程。希望有人能帮助我或指出正确的方向。我感谢每一个答案或信息。
说起来简单。我没有找到关于如何访问另一个进程的解决方案,但是如果用户试图打开另一个 window,你可以只 vorbidd 或拦截和中断。然后您可以生成自己的 window 并使用 ipcMain 和 ipcRender 模块将 smth 发送到所需的 window.