Hello
I’m building a web php App which includes a Playcanvas project via iFrame. bit not communicating
playcanvas code :
window.addEventListener(“message”, function (event) { console.log(‘1’);
if (event.origin === "http://apicanvas.daruldev.com/") { // always check message came from your website
var score = event.data.score;
// call API method one:
window.setScore(score);
// call API method two:
var app = pc.Application.getApplication();
app.fire("score:set", score);
}
}, false);
web page code :
Document
function iFramereload(){
var iframe = document.getElementById("app-frame");
At a first glance I think your issue may be in the Playcanvas side, you need to listen to the parent or top window for messages.
Something like this:
window.top.addEventListener(...);
Also, when iframing a Playcanvas build is better to use the direct link to the iframe otherwise you will get multiple iframes and make it much harder to sweep the communication.
You can find that url by opening the build with the browser dev tools.
i have used https://playcanv.as/p/Po8lzmyn/ link in iframe are you talking about this one ? i am new in this so can you please explain
window top.addEventListener(…);
window.top.addEventListener(“message”, function (event) { console.log(‘1’);
if (event.origin === "http://apicanvas.daruldev.com/") { // always check message came from your website
var score = event.data.score;
// call API method one:
//window.setScore(score);
// call API method two:
console.log('2');
// var app = pc.Application.getApplication();
// app.fire("score:set", score);
}
So you will have a single iframe in your final wesite.
Each website and each iframe have their own window object instance. You need to target the correct one when listening to message from one to the other.