|
| 1 | +import React, { useState, useEffect } from "react"; |
| 2 | +import NavbarApp from "../components/NavbarApp"; |
| 3 | +import { AiOutlineLike, AiOutlineDislike } from "react-icons/ai"; |
| 4 | +import { CiShare1 } from "react-icons/ci"; |
| 5 | +import CopyButton from "../components/ButtonCustom/CopyButton"; |
| 6 | +import ReactPlayer from "react-player"; |
| 7 | +import { ToastContainer, toast } from "react-toastify"; |
| 8 | + |
| 9 | +const WatchLiveStream = () => { |
| 10 | + const [isStreamLive, setIsStreamLive] = useState(false); // Trạng thái livestream |
| 11 | + const [chatMessages, setChatMessages] = useState([]); // Phải là một mảng |
| 12 | + const [message, setMessage] = useState(""); // Tin nhắn hiện tại |
| 13 | + const streamUrl = `http://192.168.120.213:13000/hls/2DEKS.m3u8`; |
| 14 | + const eventCode = "event123"; // Mã sự kiện mặc định |
| 15 | + |
| 16 | + // Kiểm tra trạng thái livestream |
| 17 | + const checkStreamStatus = async () => { |
| 18 | + try { |
| 19 | + const response = await fetch(streamUrl, { method: "HEAD" }); |
| 20 | + setIsStreamLive(response.ok); // Kiểm tra nếu stream có sẵn |
| 21 | + } catch (error) { |
| 22 | + console.error("Lỗi khi kiểm tra trạng thái livestream:", error); |
| 23 | + setIsStreamLive(false); |
| 24 | + } |
| 25 | + }; |
| 26 | + |
| 27 | + // Lấy danh sách tin nhắn từ API |
| 28 | + const fetchChatMessages = async () => { |
| 29 | + try { |
| 30 | + const response = await fetch(`${process.env.REACT_APP_API_CHAT}/chat/event123`); |
| 31 | + |
| 32 | + // Kiểm tra nếu không phải là JSON hợp lệ, có thể là HTML do lỗi |
| 33 | + const text = await response.text(); // Đọc phản hồi dưới dạng văn bản |
| 34 | + if (response.ok) { |
| 35 | + try { |
| 36 | + const data = JSON.parse(text); // Thử chuyển thành JSON |
| 37 | + if (Array.isArray(data.messages)) { |
| 38 | + setChatMessages(data.messages); |
| 39 | + } else { |
| 40 | + console.error("Dữ liệu messages không phải là mảng:", data.messages); |
| 41 | + setChatMessages([]); |
| 42 | + } |
| 43 | + } catch (error) { |
| 44 | + console.error("Lỗi khi phân tích JSON:", error); |
| 45 | + console.error("Dữ liệu trả về:", text); // In ra nội dung trả về để kiểm tra |
| 46 | + } |
| 47 | + } else { |
| 48 | + console.error("Lỗi khi gọi API:", response.status); |
| 49 | + console.error("Nội dung trả về:", text); // In ra nội dung trả về để kiểm tra |
| 50 | + setChatMessages([]); |
| 51 | + } |
| 52 | + } catch (error) { |
| 53 | + console.error("Lỗi khi gọi API:", error); |
| 54 | + setChatMessages([]); |
| 55 | + } |
| 56 | + }; |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | + useEffect(() => { |
| 62 | + checkStreamStatus(); // Kiểm tra trạng thái khi tải trang |
| 63 | + fetchChatMessages(); // Lấy tin nhắn khi tải trang |
| 64 | + }, [streamUrl]); |
| 65 | + const username = localStorage.getItem("userName"); |
| 66 | + // Xử lý gửi tin nhắn |
| 67 | + const handleSendMessage = async () => { |
| 68 | + if (message.trim()) { |
| 69 | + const newMessage = { username: username, message, eventCode }; // Tin nhắn mới |
| 70 | + try { |
| 71 | + const response = await fetch(`${process.env.REACT_APP_API_CHAT}/chat`, { |
| 72 | + method: "POST", |
| 73 | + headers: { "Content-Type": "application/json" }, |
| 74 | + body: JSON.stringify(newMessage), |
| 75 | + }); |
| 76 | + if (response.ok) { |
| 77 | + toast.success("Gửi tin nhắn thành công!"); |
| 78 | + setMessage(""); // Xóa ô nhập sau khi gửi |
| 79 | + fetchChatMessages(); // Tải lại danh sách tin nhắn |
| 80 | + } else { |
| 81 | + toast.error("Lỗi khi gửi tin nhắn!"); |
| 82 | + console.error("Lỗi khi gửi tin nhắn:", response.status); |
| 83 | + } |
| 84 | + } catch (error) { |
| 85 | + toast.error("Không thể gửi tin nhắn!"); |
| 86 | + console.error("Lỗi khi gọi API gửi tin nhắn:", error); |
| 87 | + } |
| 88 | + } |
| 89 | + }; |
| 90 | + |
| 91 | + return ( |
| 92 | + <div className="bg-[#f0f4f9] h-screen"> |
| 93 | + <ToastContainer position="bottom-right" /> |
| 94 | + <NavbarApp /> |
| 95 | + <div className="h-[60px]"></div> |
| 96 | + <div className="container mx-auto px-4 py-6"> |
| 97 | + {/* Sắp xếp hàng ngang */} |
| 98 | + <div className="flex flex-col md:flex-row gap-6"> |
| 99 | + {/* Video Player và chi tiết */} |
| 100 | + <div className="flex-1 bg-white shadow-md rounded-lg p-4"> |
| 101 | + <div className="aspect-w-16 aspect-h-9 bg-black rounded-lg"> |
| 102 | + {isStreamLive ? ( |
| 103 | + <ReactPlayer |
| 104 | + url={streamUrl} |
| 105 | + playing |
| 106 | + controls |
| 107 | + width="100%" |
| 108 | + height="100%" |
| 109 | + config={{ file: { forceHLS: true } }} |
| 110 | + /> |
| 111 | + ) : ( |
| 112 | + <p className="text-center text-gray-500 py-10"> |
| 113 | + Chưa có livestream nào đang phát. |
| 114 | + </p> |
| 115 | + )} |
| 116 | + </div> |
| 117 | + |
| 118 | + {/* Thông tin livestream */} |
| 119 | + <div className="mt-4 flex justify-between items-center"> |
| 120 | + <div className="flex space-x-4"> |
| 121 | + <button className="flex items-center text-gray-700 hover:text-blue-600"> |
| 122 | + <AiOutlineLike className="mr-1" size={20} /> |
| 123 | + <span>Like</span> |
| 124 | + </button> |
| 125 | + <button className="flex items-center text-gray-700 hover:text-red-600"> |
| 126 | + <AiOutlineDislike className="mr-1" size={20} /> |
| 127 | + <span>Dislike</span> |
| 128 | + </button> |
| 129 | + <button className="flex items-center text-gray-700 hover:text-green-600"> |
| 130 | + <CiShare1 className="mr-1" size={20} /> |
| 131 | + <span>Share</span> |
| 132 | + </button> |
| 133 | + </div> |
| 134 | + <CopyButton textToCopy={streamUrl} /> |
| 135 | + </div> |
| 136 | + </div> |
| 137 | + |
| 138 | + {/* Chat box */} |
| 139 | + <div className="w-full md:w-1/3 bg-white rounded-lg shadow-lg flex flex-col"> |
| 140 | + <h2 className="text-xl font-bold mb-4 p-4">Trò chuyện trực tiếp</h2> |
| 141 | + |
| 142 | + {/* Hiển thị tin nhắn trò chuyện */} |
| 143 | + <div className="flex-grow overflow-y-auto mb-4 border rounded-lg p-4 bg-gray-50"> |
| 144 | + {Array.isArray(chatMessages) && chatMessages.length === 0 ? ( |
| 145 | + <p className="text-gray-500 text-center">Chưa có tin nhắn nào.</p> |
| 146 | + ) : ( |
| 147 | + Array.isArray(chatMessages) && chatMessages.map((msg, index) => ( |
| 148 | + <div key={index} className="mb-2"> |
| 149 | + <p className="text-sm"> |
| 150 | + <span className="font-semibold">{msg.username}:</span> {msg.message} |
| 151 | + </p> |
| 152 | + <p className="text-xs text-gray-400">{msg.timestamp}</p> |
| 153 | + </div> |
| 154 | + )) |
| 155 | + )} |
| 156 | + </div> |
| 157 | + |
| 158 | + |
| 159 | + {/* Nhập và gửi tin nhắn */} |
| 160 | + <div className="flex items-center p-4"> |
| 161 | + <input |
| 162 | + type="text" |
| 163 | + value={message} |
| 164 | + onChange={(e) => setMessage(e.target.value)} |
| 165 | + placeholder="Nhập tin nhắn..." |
| 166 | + className="border rounded-lg p-2 flex-grow mr-2" |
| 167 | + /> |
| 168 | + <button |
| 169 | + onClick={handleSendMessage} |
| 170 | + className="px-4 py-2 bg-blue-500 text-white rounded-lg hover:bg-blue-600" |
| 171 | + > |
| 172 | + Gửi |
| 173 | + </button> |
| 174 | + </div> |
| 175 | + </div> |
| 176 | + </div> |
| 177 | + </div> |
| 178 | + </div> |
| 179 | + ); |
| 180 | +}; |
| 181 | + |
| 182 | +export default WatchLiveStream; |
0 commit comments