index.jsx 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import Peer from 'peerjs'
  2. import React, { useEffect, useState } from 'react'
  3. import ReactDOM from 'react-dom'
  4. import { Layout } from 'antd'
  5. import 'antd/dist/antd.css'
  6. const {
  7. Header, Footer, Content
  8. } = Layout
  9. const App = () => {
  10. const [id, setId] = useState(null)
  11. useEffect(() => {
  12. console.log('PEER', Peer)
  13. const peer = new Peer()
  14. var conn = peer.connect('another-peers-id')
  15. conn.on('open', function () {
  16. setId(conn.id)
  17. })
  18. peer.on('connection', function (conn) {
  19. conn.on('data', function (data) {
  20. // Will print 'hi!'
  21. console.log(data)
  22. })
  23. })
  24. })
  25. return <Layout style={{
  26. height: '100vh',
  27. width: '100vw'
  28. }}>
  29. <Header style={{ display: 'flex', justifyContent: 'center', backgroundColor: '#DA6855' }}>
  30. <div style={{ height: '100%', display: 'inline-block' }}>
  31. <img style={{ height: '100%' }} src='https://avatars0.githubusercontent.com/u/3409784?s=200&v=4' />
  32. </div>
  33. </Header>
  34. <Content>
  35. {id && <span>Your ID is {id}</span>}
  36. </Content>
  37. <Footer style={{ textAlign: 'center' }}>PeerJS Team</Footer>
  38. </Layout>
  39. }
  40. ReactDOM.render(<App />,
  41. document.getElementById('app')
  42. )