[Plugin] PostMessage : messages between iframes

Post your completed addons to share with the community

Post » Tue Oct 16, 2012 4:48 pm

THis plugin allows you to exchange messages between two iframes created within C2.



I'm using the postMessage HTML5 API, allowing me two exchange messages between allowed iframes.

To illustrate the use of that plugin, imagine you create a game in C2 that must support two players, on the same screen.
To do that, you can create 3 C2 projects : one with just two iframes on a layout, each one linking to the URL of anoter exported C2 project.

With tat, each player can send a message to the other playe (playing on the same machine/tablet/phone <= although it's going to be a bit tiny on a phone ).

Since those message are text, you can decide to send the position of the player pieces (as text, and you parse it on the other side), or the base64 string of an image, or anything else.

You can imagine a battleship game where you send the position of the hit as "(x,y)" (text string) to the other side, for example !

To enforce security, you MUST specify the URL of the domain you want to accept message from, in the properties of the Plugin (or else somebody can hijack your app).

The demo : https://dl.dropbox.com/u/1412774/iFramePostMessageDemo/index.html
The plugin : https://dl.dropbox.com/u/1412774/iFramePostMessageDemo/pode_postMessage.1.0.zip
The .capx ("Main app") : https://dl.dropbox.com/u/1412774/iFramePostMessageDemo/iFramePostMessageDemo.capx
The .capx ("Player 1") : https://dl.dropbox.com/u/1412774/iFramePostMessageDemo/iFramePostMessageDemoPlayer1.capx
The .capx ("Player 2") : https://dl.dropbox.com/u/1412774/iFramePostMessageDemo/iFramePostMessageDemoPlayer2.capx
B
33
S
9
G
6
Posts: 709
Reputation: 6,704

Post » Mon Oct 29, 2012 1:34 am

This is exactly how my game is designed. I'm so glad that I don't need to invent a way to communicate with my game subapps. TYVM
B
68
S
21
G
15
Posts: 701
Reputation: 15,604

Post » Mon Oct 29, 2012 3:37 pm

@lanceal : I'm happy you have a use for the plugin !
B
33
S
9
G
6
Posts: 709
Reputation: 6,704

Post » Tue Mar 25, 2014 6:10 am

Hi @Pode,

Is there a way to send a message to the C2 app that contains the iFrames (the main app in your case), rather than only between peer iframe apps?

What i'd like to achieve is the ability to continuously exchange messages between an app in a frame and the main app, from setup and initialization messages to messages in response to external and internal events.

thanks,

Dan
B
8
S
4
G
1
Posts: 205
Reputation: 1,354

Post » Tue Mar 25, 2014 5:47 pm

@grossd : the postMessage API can also work between a page and a sub iframe.
B
33
S
9
G
6
Posts: 709
Reputation: 6,704

Post » Tue Mar 25, 2014 5:56 pm

What is the ID used to direct a message to the page that includes the iFrame?
B
8
S
4
G
1
Posts: 205
Reputation: 1,354

Post » Tue Mar 25, 2014 8:09 pm

@grossd : I didn't design it that way, so it's not possible for the moment.
B
33
S
9
G
6
Posts: 709
Reputation: 6,704


Return to Completed Addons

Who is online

Users browsing this forum: No registered users and 3 guests