Skip to content

Commit 8deaf47

Browse files
committed
Add React Player for livestream functionality
1 parent 7f6070a commit 8deaf47

File tree

3 files changed

+77
-5
lines changed

3 files changed

+77
-5
lines changed

package-lock.json

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"react": "^18.2.0",
1313
"react-dom": "^18.2.0",
1414
"react-icons": "^5.0.1",
15+
"react-player": "^2.16.0",
1516
"react-scripts": "5.0.1",
1617
"stream-browserify": "^3.0.0",
1718
"web-vitals": "^2.1.4"

src/pages/Livestream.js

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import React, { useState } from "react";
1+
import React, { useState, useEffect } from "react";
2+
import ReactPlayer from "react-player";
23

34
// Hàm tạo mã sự kiện ngẫu nhiên gồm 5 ký tự
45
const generateEventCode = () => {
@@ -11,9 +12,26 @@ const generateEventCode = () => {
1112
};
1213

1314
const Livestream = () => {
14-
const [eventCode, setEventCode] = useState(generateEventCode());
15+
// Lấy mã sự kiện từ localStorage hoặc tạo mã mới nếu chưa có
16+
const [eventCode, setEventCode] = useState(localStorage.getItem("eventCode") || generateEventCode());
1517
const [message, setMessage] = useState('');
1618
const [chatMessages, setChatMessages] = useState([]);
19+
const [isStreamLive, setIsStreamLive] = useState(false);
20+
21+
useEffect(() => {
22+
// Lưu mã sự kiện vào localStorage khi eventCode thay đổi
23+
localStorage.setItem("eventCode", eventCode);
24+
}, [eventCode]);
25+
26+
// Hàm kiểm tra trạng thái của livestream
27+
const checkStreamStatus = async () => {
28+
try {
29+
const response = await fetch(`http://192.168.120.213:13000/hls/${eventCode}.m3u8`, { method: 'HEAD' });
30+
setIsStreamLive(response.ok); // Kiểm tra nếu stream có sẵn
31+
} catch (error) {
32+
setIsStreamLive(false);
33+
}
34+
};
1735

1836
// Hàm xử lý khi người dùng gửi tin nhắn
1937
const handleSendMessage = () => {
@@ -23,6 +41,10 @@ const Livestream = () => {
2341
}
2442
};
2543

44+
// URL của livestream (dựa vào eventCode làm stream key)
45+
const streamUrl = `http://192.168.120.213:13000/hls/${eventCode}.m3u8`;
46+
console.log(streamUrl)
47+
2648
return (
2749
<div className="flex bg-[#f0f4f9] min-h-screen p-5">
2850
<div className="w-2/3 p-4 bg-white rounded-lg shadow-lg">
@@ -49,12 +71,30 @@ const Livestream = () => {
4971

5072
{/* Khung phát trực tiếp */}
5173
<div className="aspect-w-16 aspect-h-9 bg-black mb-4 rounded-lg">
52-
{/* Khung video giả lập */}
53-
<p className="text-center text-white font-bold text-xl">Khung Video Trực Tiếp</p>
74+
{/* Hiển thị video phát trực tiếp */}
75+
{isStreamLive ? (
76+
<ReactPlayer
77+
url={streamUrl}
78+
playing
79+
controls
80+
width="100%"
81+
height="100%"
82+
config={{ file: { forceHLS: true } }}
83+
/>
84+
) : (
85+
<p className="text-gray-500 text-center py-10">Chưa có livestream nào đang phát.</p>
86+
)}
5487
</div>
5588

89+
<button
90+
onClick={checkStreamStatus}
91+
className="px-4 py-2 mt-4 bg-green-500 text-white font-semibold rounded-lg hover:bg-green-600"
92+
>
93+
Kiểm tra trạng thái livestream
94+
</button>
95+
5696
{/* Thông tin thêm */}
57-
<p className="text-gray-500 text-sm">Đang phát trực tiếp: Đây là khung video phát trực tiếp của bạn.</p>
97+
<p className="text-gray-500 text-sm mt-2">Đang phát trực tiếp: Đây là khung video phát trực tiếp của bạn.</p>
5898
</div>
5999

60100
{/* Thanh trò chuyện trực tiếp */}

0 commit comments

Comments
 (0)