-
Notifications
You must be signed in to change notification settings - Fork 3
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
b52f9bd
commit 5cf039a
Showing
8 changed files
with
170 additions
and
12 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,104 @@ | ||
'use client'; | ||
|
||
import React, { useState, useEffect } from 'react'; | ||
import SockJS from 'sockjs-client'; | ||
import { CompatClient, Stomp } from '@stomp/stompjs'; | ||
|
||
// test 완료되면 지우도록 하겠습니다 | ||
|
||
const ROOMID = '02d6b08d-60f8-4c21-b5b2-0ba7af752e29'; | ||
|
||
type Content = { | ||
type: 'ENTER' | 'TALK' | 'LEAVE'; | ||
message: string; | ||
sender: string; | ||
roomId: string; | ||
userId: string; | ||
}; | ||
|
||
const StompPage = () => { | ||
const [message, setMessage] = useState<Content[]>([]); | ||
const [ws, setWs] = useState<CompatClient | null>(null); | ||
|
||
const connect = () => { | ||
const sockjs = new SockJS('http://13.124.240.142:8080/ws-stomp'); | ||
const ws = Stomp.over(sockjs); | ||
|
||
setWs(ws); | ||
|
||
// eslint-disable-next-line | ||
ws.connect({}, () => { | ||
ws.subscribe(`/sub/chat/room/${ROOMID}`, (message) => { | ||
const recv = JSON.parse(message.body); | ||
setMessage((prev) => [...prev, recv]); | ||
}); | ||
|
||
ws.publish({ | ||
destination: `/pub/chat/message`, | ||
body: JSON.stringify({ | ||
roomId: ROOMID, | ||
sender: '지운', | ||
type: 'ENTER', | ||
userId: 'user2', | ||
message: '소켓 연결 성공!', | ||
}), | ||
}); | ||
}); | ||
}; | ||
|
||
useEffect(() => { | ||
connect(); | ||
return () => { | ||
ws?.disconnect(); | ||
}; | ||
// eslint-disable-next-line | ||
}, []); | ||
|
||
const sendMessage = () => { | ||
if (!ws) return; | ||
ws.publish({ | ||
destination: `/pub/chat/message`, | ||
body: JSON.stringify({ | ||
roomId: ROOMID, | ||
sender: '지운', | ||
type: 'TALK', | ||
userId: 'user2', | ||
message: '안녕하세용', | ||
}), | ||
}); | ||
}; | ||
|
||
const sendMessage2 = () => { | ||
if (!ws) return; | ||
ws.publish({ | ||
destination: `/pub/chat/message`, | ||
body: JSON.stringify({ | ||
roomId: ROOMID, | ||
sender: '민섭', | ||
type: 'TALK', | ||
userId: 'user1', | ||
message: '안녕하세용', | ||
}), | ||
}); | ||
}; | ||
|
||
return ( | ||
<> | ||
{message.map((item, index) => ( | ||
<div key={index}> | ||
{item.sender} : {item.message} | ||
</div> | ||
))} | ||
<div className="w-full flex justify-between"> | ||
<button className="bg-mint" onClick={sendMessage}> | ||
채팅 보내기 | ||
</button> | ||
<button className="bg-mint" onClick={sendMessage2}> | ||
민섭 채팅 보내기 | ||
</button> | ||
</div> | ||
</> | ||
); | ||
}; | ||
|
||
export default StompPage; |
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 StompPage from '@/app/stomp/sock/page'; | ||
import React from 'react'; | ||
|
||
const page = ({ params }: { params: { room: string } }) => { | ||
const { room } = params; | ||
return ( | ||
<div> | ||
page {room} | ||
<StompPage /> | ||
</div> | ||
); | ||
}; | ||
|
||
export default page; |
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,13 @@ | ||
import { ChatContentType } from '@/app/stomp/sock/page'; | ||
import { CompatClient } from '@stomp/stompjs'; | ||
import { atom } from 'recoil'; | ||
|
||
export const chatContentAtom = atom<ChatContentType[]>({ | ||
key: 'chatContentAtom', | ||
default: [], | ||
}); | ||
|
||
export const wsAtom = atom<CompatClient | null>({ | ||
key: 'wsAtom', | ||
default: null, | ||
}); |
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