我第一次尝试使用message—streams时收到

【问题描述】:

我第一次尝试使用 message-ports-reply-streams 时收到此错误消息:Invalid value for transfer。

在preload.js 我定义了这个api:

 contextBridge.exposeInMainWorld(
  "api", {
      electronIpcPostMessage: (channel: string, message: any, transfer?: MessagePort[]) => {
        ipcRenderer.postMessage(channel, message, transfer)
      },
  }
)
declare global {
  interface Window {
    api: {
      electronIpcPostMessage: (channel: string, message: any, transfer?: MessagePort[]) => void;

  }
}

并且,按照此处找到的示例:,在renderer React component 中,我将streaming request 定义如下:

 const Layers = () => {
  const componentIsMounted = React.useRef(true)
  React.useEffect(() => {
    const cb = (event, args) => {
      try {

        if (componentIsMounted.current) {
          console.log("react-leaflet-layers-args: ", args)
        }
      } catch (err) {
        console.log("err: ", err)
      }
    }
    const makeStreamingRequest = (element, cb) => {
      // MessageChannels are lightweight--it's cheap to create a new one for each request.
      const { port1, port2 } = new MessageChannel()
      // We send one end of the port to the main process ...
      window.api.electronIpcPostMessage(

图片[1]-我第一次尝试使用message—streams时收到-唐朝资源网

'give-me-a-stream', { element, count: 10 }, [port2] ) // ... and we hang on to the other end. // The main process will send messages to its end of the port, // and close it when it's finished. port1.onmessage = (event) => { cb(event.data) } port1.onclose = () => { console.log('stream ended') } } makeStreamingRequest(42, (data) => { console.log('got response data:', event.data) }) // We will see "got response data: 42" 10 times. return () => { // clean-up function componentIsMounted.current = false window.api.electronIpcRemoveListener( "give-me-a-stream", cb, ) } }, [])

如前所述,在运行Electron-React app 时,我在访问该组件呈现的页面时得到的error message 是: Invalid value for transfer。

从这个StackOverflow 问题:Invalid value for transfer while using ipcRenderer.postMessage of electron,看来我不是唯一一个遇到此类错误的人,但我还没有找到任何解决方案。

我做错了什么或错过了什么?如何解决问题?

我的目标是以streaming 的方式更好地发送一个非常大的geojson 文件,从main 进程到renderer 进程。这就是为什么我想尝试使用ipcRenderer.postMessage。顺便说一句,欢迎任何其他实现此目标的working 解决方案。

其他信息:

electron: v. 16
node: v. 16.13.0
O.S: Ubuntu 20.04

期待提示和帮助

【问题讨论】:

© 版权声明
THE END
喜欢就支持一下吧
点赞84赞赏 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容