Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Post Message Support on WebGL for Cross-Origin Comms #1088

Open
MoizAhmedd opened this issue Aug 24, 2024 · 4 comments
Open

Adding Post Message Support on WebGL for Cross-Origin Comms #1088

MoizAhmedd opened this issue Aug 24, 2024 · 4 comments

Comments

@MoizAhmedd
Copy link

Currently, the only way to communicate with the webview on WebGL, is using EvaluateJS

This is problematic, because running.eval on the iframe that the webview creates, is blocked by browser when applied on cross-origin frames.

A safe and common way to get around this is to post messages to the iframe instead.

It would be great for the WebGL version to have support for post message, so that communication between game and cross-origin frames can occur

@KojiNakamaru
Copy link
Member

I pushed a modified sample app to introduce postMessage:

https://github.com/gree/unity-webview/commits/example-for-1088/

As we can see in 40aba92, postMessage requires addEventListener in the iframe content, so it is not a complete alternative. Anyway, as shown here, adding postMessage is easy and might be adequate if you can edit the iframe content.

@MoizAhmedd
Copy link
Author

MoizAhmedd commented Aug 24, 2024

looks pretty good, will test this out

edit: so from what I understand, we'll still need separate logic to handle messages received/it won't trigger the callback in webviewObject.init callback?

@KojiNakamaru
Copy link
Member

Yes, so I wrote "not a complete alternative."

@KojiNakamaru
Copy link
Member

Sorry that the last comment was a bit too brief and didn't answer about the callback specifically. I've further updated the branch so that it can be directly utilized. Could you please check the latest.

  • d2c9e5b
    • modified EvaluateJS() to utilize postMessage.
    • modified Unity.call to utilize postMessage.
    • fixed postMessage to specify targetOrigin '*'.
  • 6ebe7e3
    • copied TemplateData so that the project can be directly opened/built.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants