-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
154 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
VITE_HISTORY_API_URL=http://3.34.196.131:8080 | ||
VITE_AI_API_URL=http://13.124.103.220:8000 | ||
VITE_WS_PUBLISHER=ws://13.124.103.220:8000/ws/publisher | ||
VITE_WS_SUBSCRIBER=ws://13.124.103.220:8000/ws/subscriber |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const ConnectionStatus = ({isConnected}) => { | ||
return ( | ||
<div> | ||
서버 연결 여부 : {isConnected ? '🟢' : '🔴'} | ||
</div> | ||
); | ||
}; | ||
|
||
export default ConnectionStatus; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import React, {useEffect, useRef, useState} from 'react'; | ||
import ConnectionStatus from "../components/ConnectionStatus.jsx"; | ||
|
||
const Publisher = () => { | ||
const websocketRef = useRef(null); | ||
const videoRef = useRef(null); | ||
const [isConnected, setConnected] = useState(false); | ||
|
||
useEffect(() => { | ||
const videoElement = videoRef.current; | ||
const socket = new WebSocket(import.meta.env.VITE_WS_PUBLISHER); | ||
websocketRef.current = socket; | ||
|
||
socket.binaryType = 'arraybuffer'; | ||
socket.onopen = () => { | ||
setConnected(true); | ||
console.log('WebSocket connection opened'); | ||
}; | ||
|
||
socket.onclose = () => { | ||
setConnected(false); | ||
console.log('WebSocket connection closed'); | ||
}; | ||
|
||
socket.onerror = (error) => { | ||
setConnected(false); | ||
console.error('WebSocket error:', error); | ||
}; | ||
|
||
function captureFrame() { | ||
if (videoElement.readyState !== videoElement.HAVE_ENOUGH_DATA) | ||
return | ||
|
||
const offscreenCanvas = new OffscreenCanvas(videoElement.videoWidth, videoElement.videoHeight); | ||
const context = offscreenCanvas.getContext('2d'); | ||
context.drawImage(videoElement, 0, 0, offscreenCanvas.width, offscreenCanvas.height); | ||
|
||
offscreenCanvas.convertToBlob({ type: 'image/jpeg' }).then(blob => { | ||
if (socket.readyState === WebSocket.OPEN) { | ||
socket.send(blob); | ||
} | ||
}); | ||
} | ||
|
||
navigator.mediaDevices.getUserMedia({video: true}).then(stream => { | ||
videoElement.srcObject = stream; | ||
const intervalId = setInterval(captureFrame, 100); | ||
|
||
return () => { | ||
clearInterval(intervalId); | ||
socket?.close(); | ||
}; | ||
}).catch(error => { | ||
console.error('Error accessing media devices.', error); | ||
}); | ||
|
||
return () => { | ||
socket?.close(); | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<div> | ||
<h1>Streamer</h1> | ||
<ConnectionStatus isConnected={isConnected}/> | ||
<video ref={videoRef} autoPlay></video> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Publisher; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import React, {useEffect, useRef, useState} from 'react'; | ||
import ConnectionStatus from "../components/ConnectionStatus.jsx"; | ||
import defaultImage from '../assets/loading.png'; | ||
|
||
const Subscriber = () => { | ||
const imgRef = useRef(null); | ||
const socketRef = useRef(null); | ||
const [isConnected, setConnected] = useState(false); | ||
const handleOnErrorLoadingImage = () => { | ||
imgRef.current.src = defaultImage | ||
} | ||
|
||
useEffect(() => { | ||
const imgElement = imgRef.current; | ||
imgElement.src = defaultImage | ||
|
||
const socket = new WebSocket(import.meta.env.VITE_WS_SUBSCRIBER); | ||
socket.binaryType = 'arraybuffer'; | ||
socketRef.current = socket; | ||
|
||
socket.onopen = () => { | ||
setConnected(true); | ||
console.log('WebSocket connection opened'); | ||
}; | ||
|
||
socket.onclose = () => { | ||
setConnected(false); | ||
console.log('WebSocket connection closed'); | ||
}; | ||
|
||
socket.onerror = (error) => { | ||
setConnected(false) | ||
console.error('WebSocket error:', error); | ||
}; | ||
|
||
socket.onmessage = (event) => { | ||
const blob = new Blob([event.data], {type: 'image/jpeg'}); | ||
imgElement.src = URL.createObjectURL(blob); | ||
}; | ||
|
||
return () => { | ||
socketRef.current?.close(); | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<div> | ||
<h1>Viewer</h1> | ||
<ConnectionStatus isConnected={isConnected}/> | ||
<img ref={imgRef} alt="Video Stream" onError={handleOnErrorLoadingImage}/> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Subscriber; |