@@ -2,6 +2,7 @@ import React, { useState, useEffect } from "react";
22import ReactPlayer from "react-player" ;
33import NavbarApp from "../components/NavbarApp" ;
44import { ToastContainer } from "react-toastify" ;
5+ import axios from "axios" ;
56// Hàm tạo mã sự kiện ngẫu nhiên gồm 5 ký tự
67const generateEventCode = ( ) => {
78 const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' ;
@@ -24,6 +25,7 @@ const Livestream = () => {
2425 // Lưu mã sự kiện vào localStorage khi eventCode thay đổi
2526 localStorage . setItem ( "eventCode" , eventCode ) ;
2627 } , [ eventCode ] ) ;
28+ const userName = localStorage . getItem ( "userName" ) || "defaultUser" ; // Lấy userName từ localStorage
2729
2830 // Hàm kiểm tra trạng thái của livestream
2931 const checkStreamStatus = async ( ) => {
@@ -84,17 +86,52 @@ const Livestream = () => {
8486 }
8587 } ;
8688
87- // Hàm xử lý khi người dùng gửi tin nhắn
88- const handleSendMessage = ( ) => {
89- if ( message . trim ( ) ) {
90- setChatMessages ( [ ...chatMessages , { text : message , timestamp : new Date ( ) . toLocaleTimeString ( ) } ] ) ;
91- setMessage ( '' ) ;
92- }
93- } ;
89+
9490
9591 // URL của livestream (dựa vào eventCode làm stream key)
9692 const streamUrl = `http://192.168.120.213:13000/hls/${ eventCode } .m3u8` ;
9793
94+
95+ const handleSendMessage = async ( ) => {
96+ if ( ! message . trim ( ) ) return ;
97+
98+ try {
99+ const payload = {
100+ username : userName ,
101+ message,
102+ eventCode,
103+ } ;
104+
105+ await axios . post ( `${ process . env . REACT_APP_API_CHAT } /chat` , payload ) ; // Thay đổi URL API nếu cần
106+ setMessage ( '' ) ; // Xóa nội dung ô nhập tin nhắn
107+ fetchMessages ( ) ; // Lấy lại danh sách tin nhắn
108+ } catch ( error ) {
109+ console . error ( 'Lỗi khi gửi tin nhắn:' , error ) ;
110+ }
111+ } ;
112+
113+ // Hàm lấy tin nhắn từ API
114+ const fetchMessages = async ( ) => {
115+ try {
116+ const response = await axios . get ( `${ process . env . REACT_APP_API_CHAT } /chat/${ eventCode } ` ) ; // Thay đổi URL API nếu cần
117+ setChatMessages ( response . data . messages ) ;
118+ } catch ( error ) {
119+ console . error ( 'Lỗi khi lấy tin nhắn:' , error ) ;
120+ }
121+ } ;
122+
123+ // Gọi API lấy tin nhắn khi component được tải
124+ useEffect ( ( ) => {
125+ fetchMessages ( ) ;
126+ // Thiết lập Polling: Gọi lại API mỗi 5 giây
127+ const interval = setInterval ( ( ) => {
128+ fetchMessages ( ) ;
129+ } , 2000 ) ;
130+
131+ // Cleanup interval khi component unmount
132+ return ( ) => clearInterval ( interval ) ;
133+ } , [ ] ) ;
134+
98135 return (
99136 < div className = " bg-[#f0f4f9] h-screen " >
100137 < ToastContainer position = 'bottom-right' />
@@ -173,14 +210,14 @@ const Livestream = () => {
173210 < div className = "w-1/3 ml-4 p-4 bg-white rounded-lg shadow-lg flex flex-col" >
174211 < h2 className = "text-xl font-bold mb-4" > Trò chuyện trực tiếp</ h2 >
175212
176- { /* Hiển thị tin nhắn trò chuyện */ }
213+ { /* Hiển thị tin nhắn */ }
177214 < div className = "flex-grow overflow-y-auto mb-4 border rounded-lg p-2 bg-gray-50" >
178215 { chatMessages . length === 0 ? (
179216 < p className = "text-gray-500 text-center" > Chưa có tin nhắn nào.</ p >
180217 ) : (
181218 chatMessages . map ( ( msg , index ) => (
182219 < div key = { index } className = "mb-2" >
183- < p className = "text-sm" > < span className = "font-semibold" > Người dùng :</ span > { msg . text } </ p >
220+ < p className = "text-sm" > < span className = "font-semibold" > { msg . username } :</ span > { msg . message } </ p >
184221 < p className = "text-xs text-gray-400" > { msg . timestamp } </ p >
185222 </ div >
186223 ) )
0 commit comments