Adding an API for inter-iframe communication

Adds a first version of an API to communicate between an iFrame opened by WorkAdventure and WorkAdventure itself.
The first API method is a method allowing to add messages in the chat, from the iFrame.

Comes with a test file.
This commit is contained in:
David Négrier 2021-03-04 19:00:00 +01:00
parent bf8e8bf777
commit eb93a04341
8 changed files with 184 additions and 4 deletions

View file

@ -0,0 +1,34 @@
import {Subject} from "rxjs";
interface ChatEvent {
message: string,
author: string
}
/**
* Listens to messages from iframes and turn those messages into easy to use observables.
*/
class IframeListener {
private readonly _chatStream: Subject<ChatEvent> = new Subject();
public readonly chatStream = this._chatStream.asObservable();
init() {
window.addEventListener("message", (event) => {
// Do we trust the sender of this message?
//if (event.origin !== "http://example.com:8080")
// return;
// event.source is window.opener
// event.data is the data sent by the iframe
// FIXME: this is WAAAAAAAY too sloppy as "any" let's us put anything in the message.
if (event.data.type === 'chat') {
this._chatStream.next(event.data.data);
}
}, false);
}
}
export const iframeListener = new IframeListener();