-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from readyplayerme/feature/updates
Removed Subscription Deleted Event
- Loading branch information
Showing
5 changed files
with
20,023 additions
and
4,912 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,46 @@ | ||
import React, { useRef } from "react"; | ||
import { WebView } from "react-native-webview"; | ||
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; | ||
} | ||
export default function App() { | ||
const webView = useRef(); | ||
|
||
function onAvatarUrlReceived(message) { | ||
alert(`Avatar Url = ${message.data?.url}`); | ||
} | ||
|
||
isSubscribed = true; | ||
webview.current.postMessage( | ||
function onWebViewLoaded() { | ||
webView.current.postMessage( | ||
JSON.stringify({ | ||
target: "readyplayerme", | ||
type: "subscribe", | ||
eventName: "v1.avatar.exported", | ||
}) | ||
); | ||
}; | ||
} | ||
|
||
const process = (data) => { | ||
function onMessageReceived(message) { | ||
const data = message.nativeEvent.data; | ||
const json = JSON.parse(data); | ||
|
||
// Filter for only Ready Player Me Events | ||
if (json.source !== "readyplayerme") { | ||
|
||
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, | ||
}) | ||
); | ||
} | ||
onAvatarUrlReceived(json); | ||
} | ||
}; | ||
} | ||
|
||
return ( | ||
<WebView | ||
ref={webview} | ||
source={{ | ||
uri: `https://${subdomain}.readyplayer.me/avatar?frameApi`, | ||
}} | ||
style={{ marginTop: 20 }} | ||
onLoad={subscribe} | ||
onMessage={(message) => process(message.nativeEvent.data)} | ||
<WebView | ||
ref={webView} | ||
style={{ marginTop: 30 }} | ||
onLoad={onWebViewLoaded} | ||
onMessage={onMessageReceived} | ||
source={{ uri: `https://${subdomain}.readyplayer.me/avatar?frameApi` }} | ||
/> | ||
); | ||
}; | ||
|
||
export default class App extends React.Component { | ||
render() { | ||
return <View />; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.