()
| 31 | `; |
| 32 | |
| 33 | export default function Viewer() { |
| 34 | const [fields, handleFieldChange] = useFormFields({ |
| 35 | style: "2", |
| 36 | }); |
| 37 | const webcamRef = useRef(null); |
| 38 | const [cartoonImg, setCartoonImg] = useState(null); |
| 39 | |
| 40 | const [isPaused, setPause] = useState(false); |
| 41 | const ws = useRef(null); |
| 42 | |
| 43 | useEffect(() => { |
| 44 | setPause(false); |
| 45 | const client_id = Date.now(); |
| 46 | const url = `${config.WS_SERVER}/${client_id}`; |
| 47 | console.log(url); |
| 48 | ws.current = new WebSocket(url); |
| 49 | ws.current.onopen = () => console.log("ws opened"); |
| 50 | ws.current.onclose = () => console.log("ws closed"); |
| 51 | |
| 52 | return () => { |
| 53 | ws.current.close(); |
| 54 | }; |
| 55 | }, []); |
| 56 | |
| 57 | useEffect(() => { |
| 58 | if (!ws.current) return; |
| 59 | |
| 60 | ws.current.onmessage = (event) => { |
| 61 | if (isPaused) return; |
| 62 | const message = JSON.parse(event.data); |
| 63 | setCartoonImg(message.output); |
| 64 | console.log(message); |
| 65 | }; |
| 66 | }, [isPaused]); |
| 67 | |
| 68 | function sendMessage(msg) { |
| 69 | if (!ws.current) return; |
| 70 | |
| 71 | ws.current.send(msg); |
| 72 | } |
| 73 | |
| 74 | const videoConstraints = { |
| 75 | width: 1280, |
| 76 | height: 720, |
| 77 | facingMode: "environment", // Can be "environment" or "user" |
| 78 | screenshotQuality: 1, |
| 79 | }; |
| 80 | |
| 81 | const capture = useCallback(() => { |
| 82 | const capturedImg = webcamRef.current.getScreenshot(); |
| 83 | |
| 84 | const data = JSON.stringify({ data: capturedImg, style: fields.style }); |
| 85 | sendMessage(data); |
| 86 | }, [webcamRef, fields]); |
| 87 | |
| 88 | return ( |
| 89 | <Wrapper> |
| 90 | <Webcam |
nothing calls this directly
no test coverage detected