fix dangling webrtc connections

This commit is contained in:
Nick Mowen 2023-10-20 16:06:35 -06:00
parent facd557f8c
commit d3e849fca4

View File

@ -4,6 +4,7 @@ import { useCallback, useEffect } from 'preact/hooks';
export default function WebRtcPlayer({ camera, width, height }) { export default function WebRtcPlayer({ camera, width, height }) {
const url = `${baseUrl.replace(/^http/, 'ws')}live/webrtc/api/ws?src=${camera}`; const url = `${baseUrl.replace(/^http/, 'ws')}live/webrtc/api/ws?src=${camera}`;
const ws = new WebSocket(url);
const PeerConnection = useCallback(async (media) => { const PeerConnection = useCallback(async (media) => {
const pc = new RTCPeerConnection({ const pc = new RTCPeerConnection({
@ -60,7 +61,6 @@ export default function WebRtcPlayer({ camera, width, height }) {
const connect = useCallback(async () => { const connect = useCallback(async () => {
const pc = await PeerConnection('video+audio'); const pc = await PeerConnection('video+audio');
const ws = new WebSocket(url);
ws.addEventListener('open', () => { ws.addEventListener('open', () => {
pc.addEventListener('icecandidate', (ev) => { pc.addEventListener('icecandidate', (ev) => {
@ -85,11 +85,15 @@ export default function WebRtcPlayer({ camera, width, height }) {
pc.setRemoteDescription({ type: 'answer', sdp: msg.value }); pc.setRemoteDescription({ type: 'answer', sdp: msg.value });
} }
}); });
}, [PeerConnection, url]); }, [PeerConnection, ws]);
useEffect(() => { useEffect(() => {
connect(); connect();
}, [connect]);
return () => {
ws.close();
}
}, [connect, ws]);
return ( return (
<div> <div>