Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I get this working with React Native? Any suggestions? #282

Open
adityaxp opened this issue Sep 26, 2024 · 0 comments
Open

How can I get this working with React Native? Any suggestions? #282

adityaxp opened this issue Sep 26, 2024 · 0 comments
Labels
question Further information is requested

Comments

@adityaxp
Copy link

adityaxp commented Sep 26, 2024

I'm trying to display an RTSP stream in React Native using a basic approach with WebViews, but I'm unable to load the stream.

Backend:

const express = require('express')
const app = express()

const {proxy, scriptUrl} = require('rtsp-relay')(app)

const handler = proxy({
  url: STREAM_URL,

  verbose: false
})

// the endpoint our RTSP uses
app.ws('/api/stream', handler)


app.get('/', (req, res) =>
  res.send(`
  <canvas id='canvas'></canvas>

  <script src='${scriptUrl}'></script>
  <script>
    loadPlayer({
      url: 'ws://' + location.host + '/api/stream',
      canvas: document.getElementById('canvas')
    });
  </script>
`)
)

app.listen(2000)

React-Native code:

import React, {useState, useEffect} from 'react'
import {View, Text} from 'react-native'
import {WebView} from 'react-native-webview'

export default function App() {
  const [html, sethtml] = useState('')
  const [error, setError] = useState('')

  useEffect(() => {
    const fetchData = async () => {
      try {
        const response = await fetch('http://10.1.3.4:2000/')
        const html = await response.text()
        console.log('Fetched HTML:', html)
        sethtml(html)
      } catch (err) {
        console.error('error:', err)
        setError(err.message)
      }
    }

    fetchData()
  }, [])

  return (
    <View style={{flex: 1}}>
      {error ? (
        <Text>Error: {error}</Text>
      ) : (
        <WebView
          originWhitelist={['*']}
          source={{html: html}}
          style={{flex: 1}}
          javaScriptEnabled={true}
        />
      )}
    </View>
  )
}

@k-yle k-yle added the question Further information is requested label Oct 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants