Skip to content

Commit 79e70d2

Browse files
committed
feat: 웹소켓 일부 수정
1 parent a6993e6 commit 79e70d2

File tree

4 files changed

+42
-27
lines changed

4 files changed

+42
-27
lines changed

public/pets/BACK.png

3.61 KB
Loading

src/pages/Home.jsx

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,6 @@ const Home = () => {
2222
const [userLoading, setUserLoading] = useState(true);
2323
const [userError, setUserError] = useState(null);
2424

25-
const tierEmojis = {
26-
SEED: "🌱",
27-
SPROUT: "🌿",
28-
FLOWER: "🌺",
29-
FRUIT: "🍎",
30-
TREE: "🌳",
31-
};
32-
33-
// 최대 경험치 값 계산
34-
const petExp = userInfo.petExp; // 실제 경험치
35-
const maxExp = userInfo.petGrow === 'EGG' ? 150 : userInfo.petGrow === 'HATCH' ? 300 : 100;
36-
const progress = (petExp / maxExp) * 100;
37-
3825

3926
// 사용자 정보 불러오기
4027
useEffect(() => {

src/pages/Profile.jsx

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,46 @@ import Stomp from "stompjs";
55
const Profile = ({ userInfo }) => {
66
const [seasonCommitCount, setSeasonCommitCount] = useState(userInfo.seasonCommitCount);
77
const [petExp, setPetExp] = useState(userInfo.petExp);
8-
const [maxExp] = useState(100); // 최대 경험치 (필요하면 변경 가능)
8+
const [username, setUsername] = useState(userInfo.username);
9+
const [client, setClient] = useState(null); // WebSocket 클라이언트 상태
10+
const maxExp = userInfo.petGrow === 'EGG' ? 150 : userInfo.petGrow === 'HATCH' ? 300 : 300;
11+
const [commitCount, setCommitCount] = useState(0);
12+
const tierEmojis = {
13+
SEED: "🌱",
14+
SPROUT: "🌿",
15+
FLOWER: "🌺",
16+
FRUIT: "🍎",
17+
TREE: "🌳",
18+
};
919

1020
// 경험치 바 계산
1121
const progress = (petExp / maxExp) * 100;
1222

1323
useEffect(() => {
14-
// WebSocket 설정
24+
// username이 없으면 대기 (WebSocket 연결 X)
25+
if (!username) {
26+
console.log("Username is not available yet, waiting...");
27+
return;
28+
}
29+
30+
// 기존 WebSocket 연결이 있으면 끊기
31+
if (client) {
32+
client.disconnect(() => {
33+
console.log("Previous WebSocket disconnected.");
34+
});
35+
}
36+
37+
// 새로운 WebSocket 연결
1538
const socket = new SockJS("http://localhost:8090/ws");
16-
const client = Stomp.over(socket);
39+
const newClient = Stomp.over(socket);
1740

18-
client.connect({}, () => {
41+
newClient.connect({}, () => {
1942
console.log("WebSocket connected!");
2043

2144
// 커밋 수 업데이트 메시지 수신
22-
client.subscribe(`/topic/commit/${userInfo.username}`, (message) => {
45+
newClient.subscribe(`/topic/commit/${userInfo.username}`, (message) => {
2346
const newCommitCount = JSON.parse(message.body);
24-
47+
setCommitCount(newCommitCount);
2548
// 시즌 커밋 수 업데이트
2649
setSeasonCommitCount((prev) => prev + newCommitCount);
2750

@@ -32,14 +55,17 @@ const Profile = ({ userInfo }) => {
3255
console.error("WebSocket error:", error);
3356
});
3457

58+
// WebSocket 클라이언트 저장
59+
setClient(newClient);
60+
3561
return () => {
36-
if (client) {
37-
client.disconnect(() => {
62+
if (newClient) {
63+
newClient.disconnect(() => {
3864
console.log("WebSocket disconnected.");
3965
});
4066
}
4167
};
42-
}, [userInfo.username]);
68+
}, [username]); // username이 변경될 때마다 WebSocket 연결
4369

4470
return (
4571
<div className="flex-box">
@@ -59,7 +85,8 @@ const Profile = ({ userInfo }) => {
5985
<img src={userInfo.avatarUrl} alt="User Avatar" className="avatar" /> {userInfo.username}
6086
</div>
6187
<div>이번 시즌 커밋 수: {seasonCommitCount}</div>
62-
<div>티어: {userInfo.tier} / 마지막 커밋 날짜: {new Date(userInfo.lastCommitted).toLocaleDateString()}</div>
88+
<div>업데이트 커밋 수: {commitCount}</div>
89+
<div>티어: {tierEmojis[userInfo.tier] || userInfo.tier} / 마지막 커밋 날짜: {new Date(userInfo.lastCommitted).toLocaleDateString()}</div>
6390

6491
{/* 펫 정보 */}
6592
<div>🐾 펫 정보</div>

src/pages/profile.css

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,16 @@ h2 {
3131
height: 140px;
3232
background-color: #fefefe;
3333
color: #010101;
34-
border: 1px solid #ddd;
34+
border: 1px solid #fff;
3535
border-radius: 10px;
3636
line-height: 1.4;
3737
}
3838

3939
.pet-box {
40-
width: 130px;
41-
height: 130px;
42-
border: 1px solid #ff8a9e;;
40+
width: 128px;
41+
height: 128px;
42+
border: 1px solid #ff8a9e;
43+
background-image: url('/pets/BACK.png');
4344
}
4445
.pet-box img {
4546
width: 128px;

0 commit comments

Comments
 (0)