forked from readyplayerme/Example-React-Native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
78 lines (65 loc) · 1.63 KB
/
App.js
File metadata and controls
78 lines (65 loc) · 1.63 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
import React, { useRef } from "react";
import { WebView } from "react-native-webview";
// Replace with your custom subdomain
const subdomain = "demo";
let isSubscribed = false;
let count = 0;
const correlationId = "a0bf9c2a-44d7-4882-8e72-4bc7ab73849f";
function onAvatarExported(message) {
alert(`Avatar Url = ${message.data?.url}`);
}
const View = () => {
const webview = useRef();
const subscribe = () => {
if (isSubscribed) {
return;
}
isSubscribed = true;
webview.current.postMessage(
JSON.stringify({
target: "readyplayerme",
type: "subscribe",
eventName: "v1.avatar.exported",
})
);
};
const process = (data) => {
const json = JSON.parse(data);
// Filter for only Ready Player Me Events
if (json.source !== "readyplayerme") {
return;
}
if (json.eventName === "v1.avatar.exported") {
// Event called after avatar has been created and the URL generated
onAvatarExported(json);
}
if (json.eventName !== "v1.subscription.deleted") {
count++;
if (count > 4) {
webview.current.postMessage(
JSON.stringify({
target: "readyplayerme",
type: "unsubscribe",
correlationId,
})
);
}
}
};
return (
<WebView
ref={webview}
source={{
uri: `https://${subdomain}.readyplayer.me/avatar?frameApi`,
}}
style={{ marginTop: 20 }}
onLoad={subscribe}
onMessage={(message) => process(message.nativeEvent.data)}
/>
);
};
export default class App extends React.Component {
render() {
return <View />;
}
}