Use onEffect

This commit is contained in:
Nick Mowen 2022-10-21 09:28:11 -06:00
parent ae89e817a2
commit 89adc958cd

View File

@ -1,8 +1,11 @@
import { h } from 'preact'; import { h } from 'preact';
import { useEffect } from 'preact/hooks';
export default function WebRtcPlayer({ camera, width, height }) {
useEffect(() => {
let ws; let ws;
function initStream(camera) {
if (location.protocol == 'https:') { if (location.protocol == 'https:') {
ws = new WebSocket(`wss://${window.location.host}/go2rtc/api/ws?src=${camera}`); ws = new WebSocket(`wss://${window.location.host}/go2rtc/api/ws?src=${camera}`);
} else { } else {
@ -59,10 +62,8 @@ function initStream(camera) {
// so need to create transeivers manually // so need to create transeivers manually
pc.addTransceiver('video', {direction: 'recvonly'}); pc.addTransceiver('video', {direction: 'recvonly'});
pc.addTransceiver('audio', {direction: 'recvonly'}); pc.addTransceiver('audio', {direction: 'recvonly'});
} }, [camera]);
export default function WebRtcPlayer({ camera, width, height }) {
initStream(camera);
return ( return (
<div> <div>
<video id='video' autoplay playsinline controls muted width={width} height={height} /> <video id='video' autoplay playsinline controls muted width={width} height={height} />