Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
students
messenger
Commits
0bece6ad
Commit
0bece6ad
authored
Jan 25, 2021
by
Choi Ga Young
Browse files
Merge remote-tracking branch 'origin/sooModalTest' into young
parents
78b14ec0
dc646958
Changes
7
Show whitespace changes
Inline
Side-by-side
client/src/Components/Chat.js
View file @
0bece6ad
...
...
@@ -7,7 +7,6 @@ import catchErrors from '../utils/catchErrors';
function
Chat
(
props
)
{
const
[
sender
,
setSender
]
=
useState
([])
const
[
roomName
,
setRoomName
]
=
useState
(
''
)
const
[
inner
,
setInner
]
=
useState
([
''
])
const
[
chat
,
setChat
]
=
useState
([])
//object로 key는 보낸사람 value는 메세지
...
...
@@ -21,7 +20,10 @@ function Chat(props) {
const
[
user
,
setUser
]
=
useState
(
''
)
const
[
error
,
setError
]
=
useState
(
''
);
const
[
exit
,
setExit
]
=
useState
(
false
);
const
userId
=
isAuthenticated
()
async
function
getProfile
(
userId
)
{
try
{
const
response
=
await
axios
.
get
(
`/users/
${
userId
}
`
)
...
...
@@ -31,16 +33,6 @@ function Chat(props) {
}
}
async
function
getRoomName
(
roomCode
)
{
try
{
let
res
=
await
axios
.
get
(
'
/room/getRoomName
'
,
{
params
:
{
'
roomCode
'
:
roomCode
}
})
console
.
log
(
res
.
data
)
setRoomName
(
res
.
data
)
}
catch
(
error
)
{
catchErrors
(
error
,
setError
)
}
}
function
handleChange
(
e
)
{
e
.
preventDefault
()
setInner
(
e
.
target
.
value
)
...
...
@@ -61,11 +53,9 @@ function Chat(props) {
}
function
handleClick
()
{
setChat
([
''
])
props
.
closeChatRoom
(
props
.
roomCode
)
props
.
setRecievedMsg
(
''
)
console
.
log
(
'
chat1
'
,
chat
)
setChat
([
'
초기화
'
])
console
.
log
(
'
chat2
'
,
chat
)
props
.
handleChatc
()
props
.
setLeaveInfo
([...
props
.
leaveInfo
,
{
roomName
:
props
.
roomCode
,
leaveTime
:
realTime
}])
console
.
log
(
'
나간시간
'
,
realTime
,
usualTime
)
...
...
@@ -132,6 +122,13 @@ function Chat(props) {
setTime
(
timelist
)
}
async
function
exitAndCloseRoom
(){
props
.
exitRoom
(
props
.
roomCode
)
setExit
(
false
)
props
.
handleChatc
()
}
useEffect
(()
=>
{
getProfile
(
userId
)
},
[
userId
])
...
...
@@ -141,6 +138,8 @@ function Chat(props) {
setImg
([...
img
,
props
.
singleImg
])
setSender
([...
sender
,
props
.
singleUser
])
setChat
([...
chat
,
props
.
singleChat
])
console
.
log
(
'
chat
'
,
chat
)
console
.
log
(
'
sender
'
,
sender
)
},
[
props
.
singleChat
])
useEffect
(()
=>
{
...
...
@@ -159,20 +158,29 @@ function Chat(props) {
console
.
log
(
'
겟 룸네임
'
,
chat
)
},
[
props
.
roomCode
])
const
time
=
new
Date
().
toLocaleTimeString
()
return
(
<>
<
Container
id
=
"
chat
"
style
=
{{
overflow
:
'
auto
'
,
padding
:
'
20px
'
,
border
:
"
2px solid
"
,
height
:
"
500px
"
,
margin
:
"
1%
"
,
borderColor
:
"
#BDBDBD
"
,
background
:
''
}}
>
<
Row
className
=
"
d-flex
justify-content-center
"
style
=
{{
border
:
"
2px solid
"
,
borderWidth
:
"
medium
"
,
borderColor
:
"
#FFD75F
"
,
height
:
"
80px
"
,
margin
:
"
1%
"
}}
>
<
Row
className
=
"
justify-content-center
"
style
=
{{
border
:
"
2px solid
"
,
borderWidth
:
"
medium
"
,
borderColor
:
"
#FFD75F
"
,
height
:
"
80px
"
}}
>
<
Col
md
=
"
auto
"
>
<
Button
variant
=
"
light
"
onClick
=
{
handleClick
}
>
{
`<`
}
<
/Button
>
<
/Col
>
<
Col
>
<
Row
style
=
{{
fontWeight
:
"
bold
"
,
fontSize
:
"
x-large
"
}}
>
{
roomName
}
<
/Row
>
<
Row
>
{
props
.
roomCode
}
<
/Row
>
<
Row
style
=
{{
fontWeight
:
"
bold
"
,
fontSize
:
"
x-large
"
}}
>
{
props
.
roomName
}
<
/Row
>
<
Row
>
{
props
.
roomCode
}
<
/Row
>
<
/Col
>
<
Col
md
=
"
auto
"
>
<
Button
variant
=
"
light
"
onClick
=
{()
=>
setExit
(
true
)}
>
{
'
=
'
}
<
/Button
>
<
/Col
>
<
/Row
>
{
exit
?
<
Row
>
<
Col
>
퇴장하시겠습니까
?
<
/Col
>
<
Col
md
=
"
auto
"
><
Button
variant
=
"
light
"
onClick
=
{
exitAndCloseRoom
}
>
{
"
나가기
"
}
<
/Button></
Col
>
<
/Row
>
:
null
}
<
p
>
{
props
.
newUser
}
님이
입장하셨습니다
.
<
/p
>
{
chat
.
map
((
value
,
index
)
=>
{
if
(
!
(
value
==
''
))
{
if
(
!
(
sender
[
index
]
===
user
.
nickname
))
{
...
...
client/src/Components/ClosedList.js
View file @
0bece6ad
...
...
@@ -21,11 +21,14 @@ function ClosedList(props) {
}
function
enterChatRoomCH
(
e
)
{
if
(
props
.
roomCode
){
props
.
closeChatRoom
(
props
.
roomCode
)
}
const
roomCode
=
e
.
target
.
name
const
roomName
=
e
.
target
.
value
props
.
enterChatRoom
(
roomCode
)
props
.
setRoomCode
(
roomCode
)
props
.
setRoomName
(
roomName
)
}
...
...
client/src/Components/OpenList.js
View file @
0bece6ad
...
...
@@ -17,6 +17,10 @@ function OpenList(props) {
function
enterChatRoomCH
(
e
)
{
if
(
props
.
roomCode
){
props
.
closeChatRoom
(
props
.
roomCode
)
}
console
.
log
(
'
e확인
'
,
e
.
target
)
const
roomCode
=
e
.
target
.
name
// props.enterChatRoom(roomCode) // 각각의 room으로 들어가도록 설정해야 함
...
...
client/src/Pages/HomePage.js
View file @
0bece6ad
...
...
@@ -34,6 +34,7 @@ function Home() {
const
[
recievedMsg
,
setRecievedMsg
]
=
useState
(
''
)
const
[
roomCode
,
setRoomCode
]
=
useState
(
''
)
const
[
sysmsg
,
setSysmsg
]
=
useState
(
''
)
const
[
roomName
,
setRoomName
]
=
useState
(
''
)
const
[
singleUser
,
setSingleUser
]
=
useState
(
''
)
const
[
recievedUser
,
setRecievedUser
]
=
useState
(
''
)
...
...
@@ -45,6 +46,12 @@ function Home() {
const
[
recievedTime
,
setRecievedTime
]
=
useState
(
''
)
const
[
leaveInfo
,
setLeaveInfo
]
=
useState
([
''
])
const
handleCloseModal
=
()
=>
setShowModal
(
false
);
const
handleShowModal
=
()
=>
setShowModal
(
true
);
const
handleCloseEnter
=
()
=>
setShowEnter
(
false
);
const
handleShowEnter
=
()
=>
setShowEnter
(
true
);
const
handleChato
=
()
=>
setChat
(
true
);
const
handleChatc
=
()
=>
setChat
(
false
);
//SOCKET 관련 시작
async
function
enterChatRoom
(
rCode
)
{
//방 입장하기
...
...
@@ -55,21 +62,21 @@ function Home() {
}
const
[
roomName
,
setRoomName
]
=
useState
(
''
)
const
handleCloseModal
=
()
=>
setShowModal
(
false
);
const
handleShowModal
=
()
=>
setShowModal
(
true
);
const
handleCloseEnter
=
()
=>
setShowEnter
(
false
);
const
handleShowEnter
=
()
=>
setShowEnter
(
true
);
const
handleChato
=
()
=>
setChat
(
true
);
const
handleChatc
=
()
=>
setChat
(
false
);
function
closeChatRoom
(
rCode
)
{
socket
.
emit
(
'
closeRoom
'
,
rCode
)
console
.
log
(
`
${
rCode
}
방 보기 중단`
)
}
async
function
exitRoom
(
roomId
)
{
await
axios
.
put
(
'
/room/deleteMem
'
,
{
userId
:
userId
,
roomId
:
roomId
})
console
.
log
(
`
${
roomId
}
방
${
userId
}
탈퇴`
)
setRoomCode
(
""
)
}
//오픈채팅방에서 참가하기
async
function
openListroom
(
roomId
)
{
console
.
log
(
roomId
)
const
roomInf
=
await
axios
.
get
(
'
/room/changeMem
'
,
{
params
:
{
'
roomId
'
:
roomId
}
})
// console.log(roomInf)
// console.log(roomInf.data)
// console.log(roomInf.data[0])
setRoom
(
roomInf
.
data
[
0
])
setOpen
(
true
)
setShow
(
false
)
...
...
@@ -95,9 +102,8 @@ function Home() {
e
.
preventDefault
()
}
useEffect
(()
=>
{
if
(
!
(
singleChat
==
''
))
{
if
(
!
(
singleChat
==
""
)){
socket
.
emit
(
"
chat
"
,
{
roomInfo
:
roomCode
,
sendInfo
:
{
...
...
@@ -107,7 +113,7 @@ function Home() {
time
:
singleTime
}
})
setSingleChat
(
[
''
]
)
setSingleChat
(
""
)
}
},
[
singleChat
])
...
...
@@ -132,6 +138,7 @@ function Home() {
setRecievedImg
(
sendInfo
.
img
)
setRecievedUser
(
sendInfo
.
sender
)
setRecievedMsg
(
sendInfo
.
msg
)
console
.
log
(
sendInfo
.
msg
)
})
},
[])
...
...
@@ -140,12 +147,12 @@ function Home() {
<
Menu
/>
<
Row
className
=
"
mr-0
"
>
<
Col
className
=
"
list
"
md
=
{
5
}
>
<
Tabs
defaultActiveKey
=
"
closed
"
id
=
"
uncontrolled-tab-example
"
>
<
Tabs
defaultActiveKey
=
"
closed
"
id
=
"
uncontrolled-tab-example
"
>
<
Tab
eventKey
=
"
closed
"
title
=
"
내 채팅
"
onClick
=
{
handleChato
}
>
<
ClosedList
leaveInfo
=
{
leaveInfo
}
enterChatRoom
=
{
enterChatRoom
}
setRoomCode
=
{
setRoomCode
}
setRoomName
=
{
setRoomName
}
roomCode
=
{
roomCode
}
/
>
<
ClosedList
enterChatRoom
=
{
enterChatRoom
}
setRoomCode
=
{
setRoomCode
}
setRoomName
=
{
setRoomName
}
roomCode
=
{
roomCode
}
closeChatRoom
=
{
closeChatRoom
}
/
>
<
/Tab
>
<
Tab
eventKey
=
"
open
"
title
=
"
공개방
"
onClick
=
{
handleChatc
}
>
<
OpenList
enterChatRoom
=
{
enterChatRoom
}
openListroom
=
{
openListroom
}
setRoomCode
=
{
setRoomCode
}
setRoomName
=
{
setRoomName
}
roomCode
=
{
roomCode
}
/
>
<
OpenList
enterChatRoom
=
{
enterChatRoom
}
openListroom
=
{
openListroom
}
setRoomCode
=
{
setRoomCode
}
setRoomName
=
{
setRoomName
}
roomCode
=
{
roomCode
}
closeChatRoom
=
{
closeChatRoom
}
/
>
<
/Tab
>
<
/Tabs
>
<
/Col
>
...
...
@@ -159,7 +166,7 @@ function Home() {
<
/div
>
}
{
chat
?
<
Chat
handleChatc
=
{
handleChatc
}
leaveInfo
=
{
leaveInfo
}
setLeaveInfo
=
{
setLeaveInfo
}
sendMsg
=
{
sendMsg
}
singleChat
=
{
singleChat
}
singleUser
=
{
singleUser
}
singleImg
=
{
singleImg
}
singleTime
=
{
singleTime
}
recievedMsg
=
{
recievedMsg
}
recievedUser
=
{
recievedUser
}
recievedImg
=
{
recievedImg
}
recievedTime
=
{
recievedTime
}
setSingleChat
=
{
setSingleChat
}
set
SingleUser
=
{
setSingleUser
}
setSingle
Img
=
{
setSingle
Img
}
setSingle
Time
=
{
setSingle
Time
}
setRecievedMsg
=
{
setRecievedMs
g
}
roomCode
=
{
roomCode
}
roomName
=
{
roomName
}
/
>
<
Chat
handleChatc
=
{
handleChatc
}
sendMsg
=
{
sendMsg
}
singleChat
=
{
singleChat
}
singleUser
=
{
singleUser
}
singleImg
=
{
singleImg
}
recievedMsg
=
{
recievedMsg
}
recievedUser
=
{
recievedUser
}
recievedImg
=
{
recievedImg
}
setSingleChat
=
{
setSingleChat
}
set
RecievedMsg
=
{
setRecievedMsg
}
setSingle
User
=
{
setSingle
User
}
setSingle
Img
=
{
setSingle
Im
g
}
roomCode
=
{
roomCode
}
roomName
=
{
roomName
}
closeChatRoom
=
{
closeChatRoom
}
exitRoom
=
{
exitRoom
}
/
>
:
null
}
{
open
?
<
div
className
=
"
vh-90 flex-column align-items-center justify-content-center mt-2
"
variant
=
"
dark
"
>
...
...
server/controllers/room.controller.js
View file @
0bece6ad
...
...
@@ -41,7 +41,6 @@ const makeRoom = async (req, res) => {
const
getClosedList
=
async
(
req
,
res
)
=>
{
try
{
let
list
=
await
Room
.
find
({
member
:
req
.
query
.
_id
})
// console.log('c_list가져오기', list)
return
res
.
json
(
list
)
}
catch
(
error
)
{
res
.
status
(
500
).
send
(
'
리스트 불러오기를 실패하였습니다!
'
)
...
...
@@ -51,7 +50,6 @@ const getClosedList = async (req, res) => {
const
getOpenList
=
async
(
req
,
res
)
=>
{
try
{
let
list
=
await
Room
.
find
({
isOpen
:
true
})
// console.log('o_list가져오기', list)
return
res
.
json
(
list
)
}
catch
(
error
)
{
res
.
status
(
500
).
send
(
'
리스트 불러오기를 실패하였습니다!
'
)
...
...
@@ -110,6 +108,22 @@ const changemember = async (req, res) => {
}
}
const
deleteUserId
=
async
(
req
,
res
)
=>
{
console
.
log
(
req
.
body
)
const
{
userId
,
roomId
}
=
req
.
body
let
room
=
await
Room
.
findOne
({
roomId
:
roomId
}).
select
(
'
member
'
)
console
.
log
(
'
deletetest
'
,
room
)
const
memIndex
=
room
.
member
.
indexOf
(
userId
)
try
{
room
.
member
.
splice
(
memIndex
,
1
)
await
Room
.
updateOne
({
'
roomId
'
:
roomId
},
{
'
member
'
:
room
.
member
})
console
.
log
(
`
${
roomId
}
방
${
userId
}
삭제완료`
)
return
res
.
json
(
true
)
}
catch
(
error
)
{
res
.
status
(
500
).
send
(
'
멤버 업데이트 실패
'
)
}
}
const
roomInf
=
async
(
req
,
res
)
=>
{
try
{
console
.
log
(
req
.
query
.
roomId
)
...
...
@@ -163,4 +177,4 @@ const roomInf = async (req, res) => {
// }
export
default
{
makeRoom
,
getClosedList
,
getOpenList
,
getRoomName
,
changemember
,
roomInf
}
export
default
{
makeRoom
,
getClosedList
,
getOpenList
,
getRoomName
,
changemember
,
deleteUserId
,
roomInf
}
server/routes/room.routers.js
View file @
0bece6ad
...
...
@@ -31,4 +31,7 @@ router.route('/room/getRoomName')
// router.route('/room/dbChat')
// .get(roomCtrl.dbChat)
router
.
route
(
'
/room/deleteMem
'
)
.
put
(
roomCtrl
.
deleteUserId
)
export
default
router
server/server.js
View file @
0bece6ad
...
...
@@ -29,6 +29,18 @@ io.on("connection", (socket) => { // 기본 연결
socket
.
join
(
roomInfo
);
//클라이언트에서 data에 적힌 room으로 참여시킴
});
socket
.
on
(
'
closeRoom
'
,
(
data
)
=>
{
console
.
log
(
'
close_data확인
'
,
data
)
let
roomInfo
=
data
;
socket
.
leave
(
roomInfo
);
//클라이언트에서 data에 적힌 room정보를 브로드캐스팅 받지 않는다.
});
socket
.
on
(
'
newUser
'
,
(
data
)
=>
{
console
.
log
(
'
newUser
'
,
data
)
let
userInfo
=
data
.
userInfo
;
io
.
to
(
data
.
rmIf
).
emit
(
'
sendUser
'
,
userInfo
)
})
socket
.
on
(
'
chat
'
,
async
(
data
)
=>
{
console
.
log
(
'
roomname확인
'
,
data
)
const
room
=
await
Room
.
findOne
({
roomId
:
data
.
roomInfo
})
...
...
@@ -48,7 +60,6 @@ io.on("connection", (socket) => { // 기본 연결
socket
.
on
(
'
disconnect
'
,
()
=>
{
console
.
log
(
'
disconnected from server id=
'
,
socket
.
id
)
})
});
app
.
use
(
bodyParser
.
json
());
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment