对象已在win.webContents.send - Electron/node-pty处销毁

ar7v8xwq  于 2023-04-18  发布在  Electron
关注(0)|答案(1)|浏览(154)

我试图在我的Electron应用程序中创建一个终端(类似于vscode中的终端)。
当我尝试发送终端时创建的数据时(使用win.webContents.send()时发生错误)
如何将数据发送回渲染器而不发生此错误?
我不知道该怎么做,因为我没有太多的经验,在节点/电子。
任何帮助都很感激。谢谢!
错误:

主要工艺:

const { app, BrowserWindow, ipcMain } = require('electron')
const path = require("path")
require('@electron/remote/main').initialize()

const createMainWindow = () => {
    const win = new BrowserWindow({
        width: 1920,
        height: 1080,
        webPreferences: {
            nodeIntegration: true,
            contextIsolation: false,
            enableRemoteModule: true,
            devTools: true
        },
        resizable: true,
        frame: false,
        devTools: true,
        contextIsolation: false,
        enableRemoteModule: true,
        //icon: path.join(__dirname + '/relico.ico'),
    })

    win.loadFile('./page-app/index.html')
    require('@electron/remote/main').enable(win.webContents)
}

const createLoaderWindow = () => {
    const win = new BrowserWindow({
        width: 300,
        height: 400,
        webPreferences: {
            nodeIntegration: true,
            contextIsolation: false,
            enableRemoteModule: true,
            devTools: true
        },
        resizable: false,
        frame: false,
        devTools: true,
        contextIsolation: false,
        enableRemoteModule: true,
        //icon: path.join(__dirname + '/relico.ico'),
    })

    win.loadFile('./page-app/loader.html')

    const pty = require("node-pty");
    const os = require("os");
    var shell = os.platform() === "win32" ? "powershell.exe" : "bash";

    var ptyProcess = pty.spawn(shell, [], {
        name: "xterm-color",
        cols: 80,
        rows: 30,
        cwd: process.env.HOME,
        env: process.env
    });

    ptyProcess.onData((data) => {
        process.stdout.write(data);
        win.webContents.send('terminal.incomingData', data);
    });

    ipcMain.on("terminal.keystroke", (event, key) => {
        console.log("KEY:", key)
        ptyProcess.write(key);
    });

    require('@electron/remote/main').enable(win.webContents)

}


app.whenReady().then(createLoaderWindow)

ipcMain.handle('openMain', async () => {
    createMainWindow();
    return true;
})

渲染过程:

const { Terminal } = require('xterm');
const { FitAddon } = require('xterm-addon-fit');
const { WebLinksAddon } = require('xterm-addon-web-links');
const os = require('os');

var term = new Terminal({
    cursorBlink: true,
    fontFamily: 'monospace',
    fontSize: 14,
    lineHeight: 1.4,
    scrollback: 1000,
});
term.open(document.getElementById('terminal'));

ipcRenderer.on("terminal.incomingData", (event, data) => {
    console.log("INCOMING:")
    console.dir(data);
    term.write(data);
});

term.onData(e => {
    console.dir(e)
    ipcRenderer.send("terminal.keystroke", e);
});
hmtdttj4

hmtdttj41#

我现在明白了
我使用了这个代码:

const pty = require("node-pty");
    const os = require("os");
    var shell = os.platform() === "win32" ? "powershell.exe" : "bash";

    var ptyProcess = pty.spawn(shell, [], {
        name: "xterm-color",
        cols: 80,
        rows: 30,
        cwd: process.env.HOME,
        env: process.env
    });

    ptyProcess.onData((data) => {
        process.stdout.write(data);
        win.webContents.send('terminal.incomingData', data);
    });

    ipcMain.on("terminal.keystroke", (event, key) => {
        console.log("KEY:", key)
        ptyProcess.write(key);
    });

当加载程序窗口关闭时,加载程序窗口接收消息,而不是主窗口。

相关问题