0

我是 Electron 的新手,并且有一些代码可以创建一个新窗口。我想让窗口打开一个 BroadcastChannel 但我收到错误:

Failed to construct 'BroadcastChannel': Can't create BroadcastChannel in an opaque origin

代码如下所示:

主要的.ts

...
function createWindow() {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    height: 600,
    width: 800,
    show: false,
    webPreferences: {
      nodeIntegration: true,
      preload: path.join(__dirname, 'renderer.js'),
    },
  });

  // and load the index.html of the app.
  // mainWindow.loadURL("app://a/static/main.html");
  mainWindow.loadFile('../static/main.html');

  mainWindow.on('ready-to-show', () => {
    mainWindow.webContents.openDevTools();
    mainWindow.show();
  });

渲染器.js

...
window.onload = () => {
  const app = <App />;

  document.body.appendChild(app);

  const daemonChannel = new BroadcastChannel('Daemon');
  const rendererChannel = new BroadcastChannel('Renderer');

  daemonChannel.onmessage = (e) => {
    document.body.appendChild(<p>{e.data}</p>);
    rendererChannel.postMessage(`hello back ${e.data}`);
  };
};

这适用于我的 Mac 笔记本电脑,但不适用于多台 Windows 开发机器?

4

1 回答 1

0

好的,我想通了,main.html 文件没有正确解析:

mainWindow.loadFile('../static/main.html');

我确保该文件存在并且它现在可以工作。

于 2021-03-23T03:13:39.630 回答