在godot中监听iframe父消息

问题描述 投票:0回答:1

我正在尝试在 iframe 游戏窗口中监听来自家长的消息。

以下代码可能是正确的方法吗?

func _ready():
  var window = JavaScriptBridge.get_interface("window")

  var callback = JavaScriptBridge.create_callback(on_message_received)
  window.addEventListener("message", callback)

func on_message_received(event):
    print("Message received in godot. ")

实际上这不起作用(函数 on_message_received 未触发),但以下代码起作用:

JavaScriptBridge.eval("
    window.addEventListener('message', (event) => {
               console.log('LISTEN')
            })
    ")
godot godot4
1个回答
0
投票

我已经修复了它,将回调的定义移动为我的脚本的属性,这样它就不会被释放。

var callback = JavaScriptBridge.create_callback(on_message_received)

func _ready():
  var window = JavaScriptBridge.get_interface("window")

  window.addEventListener("message", callback)

func on_message_received(event):
    print("Message received in godot. ")
© www.soinside.com 2019 - 2024. All rights reserved.