import React from "react"; import { Typography, Button, Layout, Row, Col, Card, Collapse, Input, message } from "antd"; import { createNetlessIframeSDK, NetlessIframeSDK, Events } from "../dist/index"; const { Title } = Typography; const { Content } = Layout; const { Panel } = Collapse; type AppState = { page: number; attributes: any; roomState: any; eventName: string; eventBody: string; }; export class App extends React.Component<{}, AppState> { private sdk: NetlessIframeSDK; private constructor(props: any) { super(props); } public async componentDidMount(): Promise { this.sdk = await createNetlessIframeSDK("http://localhost:3000"); this.setState({ attributes: this.sdk.attributes, roomState: this.sdk.roomState }); this.sdk.on(Events.AttributesUpdate, attr => { this.setState({ attributes: attr }); }); this.sdk.on(Events.RoomStateChanged, roomState => { this.setState({ roomState: Object.assign(this.state.roomState, roomState) }); }); const nextPageListener = () => { this.setState({ page: this.state.page + 1 }); }; const prevPageListener = () => { this.setState({ page: this.state.page - 1 }); }; this.sdk.addMagixEventListener("NextPage", nextPageListener); this.sdk.addMagixEventListener("PrevPage", prevPageListener); this.setState({ page: this.sdk.currentPage }); } public componentWillUnmount(): void { console.log("componentWillUnmount"); } private nextPage = () => { this.setState({ page: this.state.page + 1 }); this.sdk.nextPage(); } private prevPage = () => { this.setState({ page: this.state.page - 1 }); this.sdk.prevPage(); } private sendMagixEvent = () => { const { eventName, eventBody } = this.state; if (eventName && eventBody) { try { const body = JSON.parse(eventBody); this.sdk.dispatchMagixEvent(eventName, body); } catch (error) { message.error("事件体必须为合法的 JSON"); } } } public render(): React.ReactNode { return ( {this.state && JSON.stringify(this.state.attributes)}

{this.state && JSON.stringify(this.state.roomState)}

Page {this.state && this.state.page} this.setState({ eventName: e.target.value })}/> this.setState({ eventBody: e.target.value })}/>
); } }