/* 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 [flowSection, setFlowSection] = useState<'MIC' | 'VIDEO' | 'SERVER'>('MIC') const [flowState, setFlowState] = useState({ color: 'blue', flow: 'SERVER', loading: false }) // const [flow, setFlow] = useState<{ // screen: ReactNode, // loading: boolean, // color: 'red' | 'blue' | 'green' | 'orange', // options?: any // }[]>([ // // { // // loading: false, // // screen: , // // color: 'orange', // // options: [{ // // label: 'Next', // // onSelect: (a: any) => { } // // }] // // }, // { // loading: false, // screen: <> // // Select video device // // , // color: 'orange' // }, { // loading: true, // screen: , // color: 'blue' // }]) 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 handleFindPeers = () => { peerRef.current?.listAllPeers(peers => { setPeers(() => peers) console.log(`PEERS`, peers) if (peers.length > 1) { peerRef.current?.connect(peers[1]) setFlowState(s => { return { ...s, flow: 'ROOM', color: 'blue', loading: true } }) } else { setFlowState(s => { return { ...s, flow: 'NO_PEERS', color: 'blue', loading: true } }) } }) } const handleConnectServer = () => { setFlowState(s => { return { ...s, flow: 'SERVER' } }) peerRef.current = new Peer({ host: "localhost", port: 4000, path: "/myapp", secure: false, // Use true with HTTPS }) // peerRef.current.socket.on('message') peerRef.current.on("open", function (id) { setPeerId(id) setFlowState(s => { return { ...s, color: 'green', loading: false } }) console.log("My peer ID is: " + id, 'Connected Peers:',); handleFindPeers() }); peerRef.current.on('error', function (err) { setFlowState(s => { return { ...s, color: 'red', loading: false } }) }) peerRef.current.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(() => { handleConnectServer() }, []) // 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 ( { { 'MIC': { setSelectedDevice((d: any) => { return { ...d, audio: { deviceId: { exact: e } } } }) setFlowState(s => { return { ...s, flow: 'VIDEO' } }) // setFlowSection('VIDEO') }} />, 'VIDEO': { setSelectedDevice((d: any) => { return { ...d, video: { deviceId: { exact: e } } } }) handleConnectServer() }} />, 'SERVER': , 'NO_PEERS': <>Waiting for others to join, 'ROOM': <> }[flowState.flow] } {/* {flow[flowIdx].screen} */} ) // 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)} /> // // //