-
Notifications
You must be signed in to change notification settings - Fork 1
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
1 parent
6d5275c
commit 8b6acaa
Showing
8 changed files
with
224 additions
and
19 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
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,79 @@ | ||
import {React, useState, useEffect} from 'react'; | ||
import io from "socket.io-client" | ||
import './chat.css' | ||
|
||
function ChatBox({roomId, username}) { | ||
console.log("room id recieved is ", roomId) | ||
const socket = io.connect("http://localhost:5000") | ||
const [currentMessage, setCurrentMessage] = useState(""); | ||
const [chats, setChats]=useState([]); | ||
useEffect(()=>{ | ||
if(username!==""&&roomId!==""){ | ||
socket.emit("joinRoom",{roomId : roomId}); | ||
socket.on("initialChats",(initialChats)=>{ | ||
setChats(initialChats); | ||
}) | ||
} | ||
},[roomId, username]); | ||
const handelEnterPress = (event) => { | ||
if (event.key === "Enter") { | ||
sendMessage(); | ||
} | ||
}; | ||
useEffect(()=>{ | ||
socket.on("receiveMessage",(receivedChat)=>{ | ||
setChats((prevChats)=>[...prevChats,receivedChat]); | ||
}) | ||
},[socket]) | ||
|
||
const sendMessage = async()=>{ | ||
if(currentMessage!=""){ | ||
const messageData = { | ||
roomId:roomId, | ||
chat:{ | ||
author:username, | ||
message:currentMessage, | ||
timeStamp: new Date(Date.now()).getHours() + | ||
":" + | ||
new Date(Date.now()).getMinutes(), | ||
} | ||
} | ||
await socket.emit("sendMessage",messageData); | ||
setCurrentMessage(""); | ||
} | ||
} | ||
return ( | ||
<div className="chatbox"> | ||
<div className="chat-container"> | ||
<div className="header"> | ||
<p>Live Chat : {username} : {roomId}</p> | ||
</div> | ||
<div className="chat-messages"> | ||
{chats.map((messageContent, index) => ( | ||
<div className="message" key={index}> | ||
<span className="author">{messageContent.author}</span> | ||
<span className="content">{messageContent.message}</span> | ||
<span className="timestamp">{messageContent.timeStamp}</span> | ||
</div> | ||
))} | ||
</div> | ||
<div className="footer"> | ||
<input | ||
className="input-field" | ||
type="text" | ||
value={currentMessage} | ||
placeholder="Type your message..." | ||
onChange={(event) => setCurrentMessage(event.target.value)} | ||
onKeyDown={handelEnterPress} | ||
/> | ||
<button className="send-button" onClick={sendMessage}> | ||
Send | ||
</button> | ||
</div> | ||
</div> | ||
</div> | ||
) | ||
|
||
} | ||
|
||
export default ChatBox |
14 changes: 14 additions & 0 deletions
14
Frontend-Realm/src/components/PersonalChat/PrivateChat.jsx
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,14 @@ | ||
import React, { useEffect } from 'react' | ||
import {privateRoomIdState} from "../../atoms/atoms" | ||
import { useRecoilValue } from 'recoil' | ||
import ChatBox from "./ChatBox" | ||
function PrivateChat() { | ||
const privateRoomId = useRecoilValue(privateRoomIdState); | ||
return ( | ||
<div> | ||
<ChatBox roomId={privateRoomId} username ={localStorage.getItem("username")} /> | ||
</div> | ||
) | ||
} | ||
|
||
export default PrivateChat |
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,78 @@ | ||
.chatbox { | ||
display: flex; | ||
justify-content: center; | ||
margin-bottom: 100px; | ||
margin-top: 100px; | ||
} | ||
|
||
.chat-container { | ||
width: 1000px; | ||
height: 600px; | ||
border: 1px solid #ddd; | ||
border-radius: 10px; | ||
overflow: hidden; | ||
font-family: Arial, sans-serif; | ||
} | ||
|
||
.header { | ||
color: #fff; | ||
padding: 10px; | ||
text-align: center; | ||
font-weight: bold; | ||
} | ||
|
||
.chat-messages { | ||
height: 450px; | ||
overflow-y: auto; | ||
padding: 10px; | ||
} | ||
|
||
.message { | ||
margin-bottom: 10px; | ||
} | ||
|
||
.author { | ||
font-weight: bold; | ||
color: #075e54; | ||
} | ||
|
||
.content { | ||
background-color: #dcf8c6; | ||
padding: 8px 12px; | ||
border-radius: 10px; | ||
display: inline-block; | ||
margin-left: 10px; | ||
} | ||
|
||
.timestamp { | ||
font-size: 12px; | ||
color: #999; | ||
margin-left: 10px; | ||
} | ||
|
||
.footer { | ||
display: flex; | ||
align-items: center; | ||
padding: 10px; | ||
border-top: 1px solid #ddd; | ||
} | ||
|
||
.input-field { | ||
flex: 1; | ||
padding: 8px; | ||
border: none; | ||
border-radius: 20px; | ||
outline: none; | ||
color: #000; | ||
} | ||
|
||
.send-button { | ||
background-color: #075e54; | ||
color: #fff; | ||
border: none; | ||
border-radius: 50%; | ||
width: 30px; | ||
height: 30px; | ||
margin-left: 10px; | ||
cursor: pointer; | ||
} |
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
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