UNPKG

@webrtc2/client

Version:

WebRTC2 Client - React Native, Web & Electron WebRTC hooks and components for cross-platform real-time video calls, audio communication, and screen sharing

57 lines (49 loc) 1.74 kB
import { useCallback, useState } from 'react'; import type { CallState, ConnectionState, WebRTCHookReturn } from '../types'; /** * Main WebRTC hook for managing peer connections * * @example * ```tsx * const { connect, localStream, remoteStream, connectionState } = useWebRTC(); * ``` */ export function useWebRTC(): WebRTCHookReturn { const [localStream, setLocalStream] = useState<MediaStream | null>(null); const [remoteStream, setRemoteStream] = useState<MediaStream | null>(null); const [connectionState, setConnectionState] = useState<ConnectionState>('disconnected'); const [callState, setCallState] = useState<CallState>('idle'); const connect = useCallback(async (roomId: string): Promise<void> => { try { setConnectionState('connecting'); setCallState('calling'); // TODO: Implement WebRTC connection logic console.log('Connecting to room:', roomId); // Placeholder implementation setTimeout(() => { setConnectionState('connected'); setCallState('connected'); }, 1000); } catch (error) { console.error('Failed to connect:', error); setConnectionState('failed'); setCallState('ended'); } }, []); const disconnect = useCallback(() => { setConnectionState('disconnected'); setCallState('idle'); setLocalStream(null); setRemoteStream(null); // TODO: Cleanup WebRTC connections console.log('Disconnected'); }, []); return { connect, disconnect, localStream, remoteStream, connectionState, callState }; }