Commit 44665bf6 authored by Choi Ga Young's avatar Choi Ga Young
Browse files

chat 디비 저장

parent 918ba292
...@@ -39,6 +39,12 @@ function Chat(props) { ...@@ -39,6 +39,12 @@ function Chat(props) {
setDisabled(true) setDisabled(true)
console.log(chat) console.log(chat)
} }
function handleClick(e) {
setChat([])
props.handleChatc()
}
useEffect(() => { useEffect(() => {
getProfile(userId) getProfile(userId)
}, [userId]) }, [userId])
...@@ -107,11 +113,7 @@ function Chat(props) { ...@@ -107,11 +113,7 @@ function Chat(props) {
}) })
} }
<Button variant="light" onClick={handleClick} >{`<`}</Button>
<Button variant="light" onClick={props.handleChatc} >{`<`}</Button>
<Form onSubmit={sendMsgCH}> <Form onSubmit={sendMsgCH}>
<Form.Group className='d-flex flex-wrap-nowrap justify-content-center ml-2 mr-2'> <Form.Group className='d-flex flex-wrap-nowrap justify-content-center ml-2 mr-2'>
<Form.Control className='border border-warning' name='chat' type="text" value={inner} onChange={handleChange} style={{ width: '85%' }} /> <Form.Control className='border border-warning' name='chat' type="text" value={inner} onChange={handleChange} style={{ width: '85%' }} />
......
...@@ -13,6 +13,8 @@ import EnterRoom from "../Components/EnterRoom" ...@@ -13,6 +13,8 @@ import EnterRoom from "../Components/EnterRoom"
const socket = io(); const socket = io();
function Home() { function Home() {
const userName = sessionStorage.getItem('name')
const [showModal, setShowModal] = useState(false); const [showModal, setShowModal] = useState(false);
const [showEnter, setEnter] = useState(false); const [showEnter, setEnter] = useState(false);
const [chat, setChat] = useState(false); const [chat, setChat] = useState(false);
...@@ -36,9 +38,9 @@ function Home() { ...@@ -36,9 +38,9 @@ function Home() {
//SOCKET 관련 시작 //SOCKET 관련 시작
function enterChatRoom(rName) { //방 입장하기 function enterChatRoom(rCode) { //방 입장하기
socket.emit('joinRoom', rName) socket.emit('joinRoom', rCode)
console.log(`joinRoom : ${rName} 입장`) console.log(`joinRoom : ${rCode} 입장`)
} }
const sendMsg = (e) => { const sendMsg = (e) => {
...@@ -49,7 +51,8 @@ function Home() { ...@@ -49,7 +51,8 @@ function Home() {
if (!(singleChat == '')) { if (!(singleChat == '')) {
socket.emit("chat", { socket.emit("chat", {
roomInfo: roomCode, roomInfo: roomCode,
msg: singleChat username: userName,
msg: singleChat,
}) })
setSingleChat(['']) setSingleChat([''])
} }
...@@ -78,9 +81,8 @@ function Home() { ...@@ -78,9 +81,8 @@ function Home() {
</Tabs> </Tabs>
</Col> </Col>
<Col style={{ padding: "0" }}> <Col style={{ padding: "0" }}>
{chat ? <Chat handleChatc={handleChatc} sendMsg={sendMsg} singleChat={singleChat} recievedMsg={recievedMsg} setSingleChat={setSingleChat} roomCode={roomCode} /> : null}
<div style={{ position: "fixed", bottom: "20px", right: "30px" }}> {chat ? <Chat handleChatc={handleChatc} sendMsg={sendMsg} singleChat={singleChat} recievedMsg={recievedMsg} setSingleChat={setSingleChat} roomCode={roomCode} /> : <div style={{ position: "fixed", bottom: "20px", right: "30px" }}>
<Button variant="primary" onClick={handleShowModal} size="lg" block> <Button variant="primary" onClick={handleShowModal} size="lg" block>
생성 생성
</Button> </Button>
...@@ -88,11 +90,11 @@ function Home() { ...@@ -88,11 +90,11 @@ function Home() {
<Button variant="secondary" onClick={handleShowEnter} size="lg" block> <Button variant="secondary" onClick={handleShowEnter} size="lg" block>
참가 참가
</Button> </Button>
</div> </div>}
</Col> </Col>
</Row> </Row>
<RoomMake showModal={showModal} handleCloseModal={handleCloseModal} /> <RoomMake showModal={showModal} handleCloseModal={handleCloseModal} />
<EnterRoom showEnter={showEnter} enterChatRoom={enterChatRoom} handleCloseEnter={handleCloseEnter} handleChato={handleChato} setRoomName={setRoomName}/> <EnterRoom showEnter={showEnter} enterChatRoom={enterChatRoom} handleCloseEnter={handleCloseEnter} handleChato={handleChato} setRoomName={setRoomName} />
</> </>
); );
} }
......
...@@ -7,7 +7,7 @@ const nanoid = customAlphabet('1234567890abcdef', 10) ...@@ -7,7 +7,7 @@ const nanoid = customAlphabet('1234567890abcdef', 10)
const makeRoom = async (req, res) => { const makeRoom = async (req, res) => {
console.log(req.body) console.log(req.body)
const { roomName, interest, isOpen, member } = req.body; const { roomName, interest, isOpen, member } = req.body;
console.log('콘솔확인',roomName, interest, isOpen, member) console.log('콘솔확인', roomName, interest, isOpen, member)
const roomId = nanoid() const roomId = nanoid()
const room = await Room.findOne({ roomId }) const room = await Room.findOne({ roomId })
...@@ -39,7 +39,7 @@ const makeRoom = async (req, res) => { ...@@ -39,7 +39,7 @@ const makeRoom = async (req, res) => {
const getClosedList = async (req, res) => { const getClosedList = async (req, res) => {
try { try {
console.log('req확인',req.query._id) console.log('req확인', req.query._id)
let list = await Room.find({ member: [req.query._id] }) let list = await Room.find({ member: [req.query._id] })
console.log('c_list가져오기', list) console.log('c_list가져오기', list)
return res.json(list) return res.json(list)
......
...@@ -4,16 +4,18 @@ const { String } = mongoose.Schema.Types ...@@ -4,16 +4,18 @@ const { String } = mongoose.Schema.Types
const ChatSchema = new mongoose.Schema({ const ChatSchema = new mongoose.Schema({
room: { room: {
type: ObjectId, type: mongoose.ObjectId,
required: true, required: true,
ref: 'Room', ref: 'Room',
}, },
username: { username: {
type: String, type: String,
required: true, required: true,
ref: 'User',
}, },
message: String, message: {
type: String,
required: true
},
}, { }, {
timestamps: true timestamps: true
}) })
......
...@@ -11,4 +11,6 @@ router.route('/room/closedlist') ...@@ -11,4 +11,6 @@ router.route('/room/closedlist')
router.route('/room/openlist') router.route('/room/openlist')
.get(roomCtrl.getOpenList) .get(roomCtrl.getOpenList)
export default router export default router
\ No newline at end of file
import express from 'express' import express, { response } from 'express'
import connectDb from './utils/connectDb.js' import connectDb from './utils/connectDb.js'
import userRouter from './routes/user.routes.js' import userRouter from './routes/user.routes.js'
import authRouter from './routes/auth.routes.js' import authRouter from './routes/auth.routes.js'
...@@ -8,6 +8,9 @@ import bodyParser from "body-parser"; ...@@ -8,6 +8,9 @@ import bodyParser from "body-parser";
import http from "http"; import http from "http";
import { Server } from 'socket.io'; import { Server } from 'socket.io';
import cors from "cors" import cors from "cors"
import Room from './models/Room.js'
import { nextTick } from 'process'
import Chat from "./models/Chat.js"
connectDb() connectDb()
...@@ -20,21 +23,31 @@ const io = new Server(server) ...@@ -20,21 +23,31 @@ const io = new Server(server)
io.on("connection", (socket) => { // 기본 연결 io.on("connection", (socket) => { // 기본 연결
console.log("socket connect ok", socket.id) console.log("socket connect ok", socket.id)
socket.on('joinRoom', (data)=>{ // joinRoom을 클라이언트가 emit했을 때 socket.on('joinRoom', (data) => { // joinRoom을 클라이언트가 emit했을 때
console.log('join_data확인',data) console.log('join_data확인', data)
let roomInfo=data; let roomInfo = data;
socket.join(roomInfo); //클라이언트에서 data에 적힌 room으로 참여시킴 socket.join(roomInfo); //클라이언트에서 data에 적힌 room으로 참여시킴
}); });
socket.on('chat', (data) => { socket.on('chat', async (data) => {
console.log('roomname확인', data) console.log('roomname확인', data)
socket.broadcast.to(data.roomInfo).emit('sendedMSG', data.msg ); // sender 제외 특정 방으로 const room = await Room.findOne({ roomId: data.roomInfo })
console.log('room이 떴나', room)
}); const chat = await new Chat({
room: room._id,
username: data.username,
message: data.msg
}).save()
console.log('resChat확인', chat)
socket.broadcast.to(data.roomInfo).emit('sendedMSG', data.msg); // sender 제외 특정 방으로
socket.on('disconnect', () => { });
socket.on('disconnect', () => {
console.log('disconnected from server id=', socket.id) console.log('disconnected from server id=', socket.id)
}) })
}); });
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment