/* eslint-disable @typescript-eslint/no-explicit-any */ 'use client' import { CameraIcon, HdIcon, MicrochipIcon, Server, VideoIcon } from 'lucide-react'; import Peer, { DataConnection } from 'peerjs'; import React, { ReactNode, useCallback, useEffect, useRef, useState } from 'react' type FlowStateType = { color: 'red' | 'blue' | 'green' | 'orange', flow: 'MIC' | 'VIDEO' | 'SERVER' | 'NO_PEERS' | 'ROOM', loading: boolean } export default function WebRTCChatUsingPeer() { let mediaStream // UI const peerRef = useRef(undefined) const [peers, setPeers] = useState([]) const connection = useRef(undefined) const videoRef = useRef(null) // const [mediaStream, setMediaStream] = useState(null) const [received, setReceivved] = useState('') const [peerId, setPeerId] = useState('') const [theirPeerId, setTheirPeerId] = useState('') const [myStream, setMyStream] = useState(null); const [devices, setDevices] = useState([]) const [selectedDevice, setSelectedDevice] = useState<{ video: { deviceId: { exact: string } }, audio: { deviceId: { exact: string } } }>() const peer = new Peer({ host: "localhost", port: 4000, path: "/myapp", secure: false, // Use true with HTTPS }); useEffect(() => { peer.on("open", function (id) { setPeerId(id) console.log("My peer ID is: " + id); }); }, []) peer.on('connection', function (conn) { console.log(`Got Connection`, conn) conn.on("data", function (data) { setReceivved(data as string) console.log("Received", data); }); conn.send("Hello!"); }); // useEffect(() => { // // Get user media // navigator.mediaDevices.getUserMedia({ video: true, audio: true }) // .then(stream => { // // setMyStream(stream); // // if (videoRef.current) { // // videoRef.current.srcObject = stream; // // } // }) // .catch(err => console.error("Failed to get local stream", err)); // // Initialize PeerJS // // const peer = new Peer(); // PeerJS manages server connection // // currentPeer.current = peer; // peer.on('open', (id) => { // setPeerId(id); // console.log('My peer ID is:', id); // }); // // Listen for incoming calls // peer.on('call', (call) => { // if (myStream) { // // Answer the call with your stream // call.answer(myStream); // call.on('stream', (remoteStream) => { // // Show stream in remote video element // if (videoRef.current) { // videoRef.current.srcObject = remoteStream; // } // }); // } // }); // peer.on('error', (err) => console.error(err)); // return () => { // if (peer) { // peer.destroy(); // } // }; // }, [myStream]); // useEffect(() => { // const load = async () => { // // Load media devices // const res = await navigator.mediaDevices.getUserMedia({ video: true, audio: true }) // // .then(async res => // setDevices(await navigator.mediaDevices.enumerateDevices()) // // ) // // setFlowIdx(1) // // setFlowIdx(2) // // console.log(await navigator.mediaDevices.enumerateDevices()) // // console.log(await navigator.mediaDevices.getUserMedia().) // } // load() // peer.on("call", function (call) { // alert(`You have a call`) // // Answer the call, providing our mediaStream // // if (mediaStream) { // call.answer(mediaStream); // call.on('stream', (remoteStream) => { // // Show stream in remote video element // if (videoRef.current) { // videoRef.current.srcObject = remoteStream; // } // }); // // } // }); // }, []) return (
{JSON.stringify(peerId)}
Settings Video {devices.filter(a => a.kind === 'videoinput').map(device =>
setSelectedDevice((d: any) => { return { ...d, video: { deviceId: { exact: device.deviceId } } } })} className={`${selectedDevice && selectedDevice.video && device.deviceId === selectedDevice?.video.deviceId.exact ? 'bg-green-300' : ''}`} > {device.label}
)} Audio {devices.filter(a => a.kind === 'audioinput').map(device =>
setSelectedDevice((d: any) => { return { ...d, audio: { deviceId: { exact: device.deviceId } } } })} className={`${selectedDevice && selectedDevice.audio && device.deviceId === selectedDevice?.audio.deviceId.exact ? 'bg-green-300' : ''}`} > {device.label}
)}
setTheirPeerId(e.target.value)} />