|
1 | 1 | import clsx from "clsx";
|
2 |
| -import React, { useEffect, useRef, useState } from "react"; |
| 2 | +import React, { useCallback, useEffect, useRef, useState } from "react"; |
3 | 3 | import toast from "react-hot-toast";
|
4 | 4 |
|
5 |
| -const CameraComponent = ({ isGameStarted }: { isGameStarted: boolean }) => { |
| 5 | +const CameraComponent = ({ isGameStarted ,countFaults}: { isGameStarted: boolean ,countFaults:()=> void}) => { |
| 6 | + const [socket, setSocket] = useState<WebSocket | null>(null); |
6 | 7 | const [isCheating, setIsCheating] = useState<boolean>(false);
|
7 | 8 | const videoRef = useRef<HTMLVideoElement>(null);
|
| 9 | + const getCamera = async () => { |
| 10 | + const stream=await navigator.mediaDevices.getUserMedia({ video: true }); |
| 11 | + if (videoRef.current) { |
| 12 | + videoRef.current.srcObject = stream; |
| 13 | + videoRef.current.play(); |
| 14 | + } |
| 15 | + return stream; |
| 16 | + }; |
| 17 | + //when the game is started and socket exist , send the frame to the server |
| 18 | + const sendFrame=useCallback(function () { |
| 19 | + //used canvas to send blob object |
| 20 | + if (!videoRef.current) return; |
| 21 | + const canvas = document.createElement("canvas"); |
| 22 | + canvas.width = videoRef?.current?.videoWidth || 0; |
| 23 | + canvas.height = videoRef?.current?.videoWidth || 0; |
| 24 | + const context = canvas.getContext("2d"); |
| 25 | + context?.drawImage( |
| 26 | + videoRef.current, |
| 27 | + 0, |
| 28 | + 0, |
| 29 | + canvas.width, |
| 30 | + canvas.height |
| 31 | + ); |
| 32 | + canvas.toBlob(function (blob) { |
| 33 | + if (blob && isGameStarted && socket?.readyState===1) { |
| 34 | + socket.send(blob); |
| 35 | + } |
| 36 | + }, "image/jpeg"); |
| 37 | + },[socket,isGameStarted]); |
| 38 | + // function sendFrame() { |
| 39 | + // //used canvas to send blob object |
| 40 | + // if (!videoRef.current) return; |
| 41 | + // const canvas = document.createElement("canvas"); |
| 42 | + // canvas.width = videoRef?.current?.videoWidth || 0; |
| 43 | + // canvas.height = videoRef?.current?.videoWidth || 0; |
| 44 | + // const context = canvas.getContext("2d"); |
| 45 | + // context?.drawImage( |
| 46 | + // videoRef.current, |
| 47 | + // 0, |
| 48 | + // 0, |
| 49 | + // canvas.width, |
| 50 | + // canvas.height |
| 51 | + // ); |
| 52 | + // canvas.toBlob(function (blob) { |
| 53 | + // if (blob && isGameStarted && socket?.readyState===1) { |
| 54 | + // socket.send(blob); |
| 55 | + // } |
| 56 | + // }, "image/jpeg"); |
| 57 | + // } |
| 58 | + useEffect(() => { |
| 59 | + //used to connect to the websocket server once the component is mounted |
| 60 | + const websocket = new WebSocket(import.meta.env.VITE_WS_URL as string); |
| 61 | + setSocket(websocket); |
| 62 | + websocket.onopen = () => { |
| 63 | + console.log("Websocket connected"); |
| 64 | + }; |
| 65 | + websocket.onerror = () => { |
| 66 | + toast.error("Error in socket connection"); |
| 67 | + }; |
| 68 | + }, []); |
8 | 69 | useEffect(() => {
|
| 70 | + console.log("isGameStarted", isGameStarted); |
9 | 71 | try {
|
10 |
| - let websocket: WebSocket; |
11 |
| - if (isGameStarted) { |
12 |
| - websocket = new WebSocket("ws://localhost:8765"); |
13 |
| - websocket.onopen = () => { |
14 |
| - toast.success("Video is being monitored, don't cheat!"); |
15 |
| - }; |
16 |
| - websocket.onerror = () => { |
17 |
| - toast.error("Error in websocket connection"); |
18 |
| - }; |
19 |
| - websocket.onmessage = () => { |
20 |
| - toast.error("Keyboard me mat dekh", { |
| 72 | + if (isGameStarted && socket) { |
| 73 | + |
| 74 | + socket.onmessage = () => { |
| 75 | + toast.error("Looking at Keyboard ! Points will be reduced", { |
21 | 76 | position: "top-center",
|
22 | 77 | });
|
| 78 | + countFaults(); |
23 | 79 | setIsCheating(true);
|
24 |
| - setTimeout(() => { |
| 80 | + const id = setTimeout(() => { |
25 | 81 | setIsCheating(false);
|
26 | 82 | }, 500);
|
| 83 | + if (!isCheating) { |
| 84 | + clearTimeout(id); |
| 85 | + } |
27 | 86 | };
|
28 | 87 | }
|
29 |
| - if (navigator.mediaDevices.getUserMedia) { |
30 |
| - navigator.mediaDevices |
31 |
| - .getUserMedia({ video: true }) |
32 |
| - .then(function (stream) { |
33 |
| - if (videoRef.current) { |
34 |
| - videoRef.current.srcObject = stream; |
35 |
| - if (isGameStarted) websocket.binaryType = "arraybuffer"; |
36 |
| - videoRef.current.play(); |
37 |
| - //send the stream to the server every 0.1s |
38 |
| - videoRef.current.addEventListener("play", function () { |
39 |
| - if (isGameStarted) setInterval(sendFrame, 100); |
40 |
| - }); |
41 |
| - } |
42 |
| - function sendFrame() { |
43 |
| - //used canvas to send blob object |
44 |
| - if (!videoRef.current) return; |
45 |
| - const canvas = document.createElement("canvas"); |
46 |
| - canvas.width = videoRef?.current?.videoWidth || 0; |
47 |
| - canvas.height = videoRef?.current?.videoWidth || 0; |
48 |
| - const context = canvas.getContext("2d"); |
49 |
| - context?.drawImage( |
50 |
| - videoRef.current, |
51 |
| - 0, |
52 |
| - 0, |
53 |
| - canvas.width, |
54 |
| - canvas.height |
55 |
| - ); |
56 |
| - canvas.toBlob(function (blob) { |
57 |
| - if (blob) { |
58 |
| - websocket.send(blob); |
59 |
| - } |
60 |
| - }, "image/jpeg"); |
61 |
| - } |
62 |
| - }) |
63 |
| - .catch(function (error) { |
64 |
| - console.log("Something went wrong with webcam access: ", error); |
65 |
| - }); |
| 88 | + getCamera(); |
| 89 | + if(videoRef.current && socket){ |
| 90 | + socket.binaryType = "arraybuffer"; |
| 91 | + videoRef.current.addEventListener("play", function () { |
| 92 | + const id=setInterval(sendFrame, 100); |
| 93 | + if(!isGameStarted) clearInterval(id); |
| 94 | + }); |
66 | 95 | }
|
67 | 96 | } catch (e) {
|
68 |
| - console.log(e); |
| 97 | + console.log("Error in webcam access: ", e); |
69 | 98 | }
|
| 99 | + return () => { |
| 100 | + if (socket) socket.close(); |
| 101 | + }; |
70 | 102 | }, [isGameStarted]);
|
71 | 103 | return (
|
72 | 104 | <div>
|
| 105 | + <p className="text-red-600 font-extrabold text-xl h-7"> |
| 106 | + {isCheating && "Don't Look at keyboard!"} |
| 107 | + </p> |
73 | 108 | <video
|
74 | 109 | ref={videoRef}
|
75 | 110 | className={clsx(
|
76 |
| - "absolute bottom-0 right-0 w-[300px] h-auto border-2 border-white", |
| 111 | + "absolute bottom-0 right-0 w-[250px] h-auto border-2 border-white", |
77 | 112 | isCheating && "border-red-500 object-cover"
|
78 | 113 | )}
|
79 | 114 | ></video>
|
|
0 commit comments