简体   繁体   English

带广播频道的 Electron - 无法在不透明的来源中创建 BroadcastChannel

[英]Electron w/ Broadcast Channels - Can't create BroadcastChannel in an opaque origin

I am new to Electron and have some code to create a new window.我是 Electron 的新手,并且有一些代码可以创建新的 window。 I'd like for the window to open a BroadcastChannel but am getting the error:我想让 window 打开一个 BroadcastChannel 但我收到错误:

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

The code looks like this:代码如下所示:

main.ts main.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();
  });

renderer.js渲染器.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}`);
  };
};

This is working on my mac laptop but not multiple Windows development machines?这适用于我的 mac 笔记本电脑,但不适用于多台 Windows 开发机器?

OK I figured it out, the main.html file wasn't resolving correctly:好的,我想通了, main.html 文件没有正确解析:

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

I made sure that file exists and it now works.我确保该文件存在并且它现在可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM