-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
66 lines (53 loc) · 1.42 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
+(async function () {
console.log('GDevelop Demo web interaction');
// 這是必要的,與 blockly.json 的 name 一致。
const IFRAME_NAME = "GDevelop Demo";
let iframe__;
let mqttInstance__;
function setIframe(iframe) {
iframe__ = iframe;
}
function getIframe() {
return iframe__;
}
function setMqttInstance(inst) {
mqttInstance__ = inst;
}
function getMqttInstance() {
return mqttInstance__;
}
async function init() {
try {
// 由平台提供的 api
const iframe = await webduinoPlatform.getWebInteractionIframe(IFRAME_NAME);
if (!iframe) throw new Error('[GDevelopDemo] iframe does not exist!');
setIframe(iframe);
} catch (err) {
console.error(err);
}
}
async function sendMessage(message) {
const topic = getMqttTopic().subscribe;
const mqttInstance = await createMqttInstance();
if (!mqttInstance) console.warn('[GDevelopDemo] No Mqtt instance!');
mqttInstance.send({
topic,
message,
});
}
function getMqttTopic() {
const { gdjs } = getIframe().contentWindow;
return gdjs.evtTools.webduinoMqtt.topic;
}
async function createMqttInstance() {
if (getMqttInstance()) return getMqttInstance();
const inst = new webduino.module.mqttClient();
await inst.connect();
setMqttInstance(inst);
return inst;
}
await init();
window.webduinoGDevelopDemo = {
sendMessage,
};
})();