Skip to content

Commit 99697c6

Browse files
committed
refactor: 웹소켓 연결 확인 여부로 판단
1 parent f932f3a commit 99697c6

File tree

2 files changed

+34
-34
lines changed

2 files changed

+34
-34
lines changed

src/pages/main/Main.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ const Main = () => {
5353

5454
useEffect(() => {
5555
const webSocketManager = WebSocketManager.getInstance();
56-
if(webSocketManager){
56+
if(webSocketManager.isConnected()){
5757
webSocketManager.disconnect();
5858
resetGameState();
5959
resetUserState();

src/services/WebSocketManager.ts

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Client, IFrame, IMessage } from '@stomp/stompjs';
22
import { NavigateFunction } from 'react-router-dom';
33
import SockJS from 'sockjs-client';
4-
import { RoomDataProps } from "../types/RoomData.type.ts";
5-
import { GameStateDataProps } from "../types/GameStateData.type.ts";
4+
import { RoomDataProps } from '../types/RoomData.type.ts';
5+
import { GameStateDataProps } from '../types/GameStateData.type.ts';
66

77
type GameStateUpdater = (state: RoomDataProps) => void;
88
type GameReadyUpdater = (state: GameStateDataProps) => void;
@@ -26,11 +26,11 @@ class WebSocketManager {
2626
private roomId!: string;
2727
private nickname!: string;
2828
private updateGameState!: GameStateUpdater;
29-
private updateGameReadyState!: GameReadyUpdater
30-
private navigate!: NavigateFunction
31-
private showMessage!: ShowMessageFunction
32-
private showRoomChiefLeaveMessage!: ShowRoomChiefLeaveMessageFunction
33-
private showResultMessage!: ShowResultMessageFunction
29+
private updateGameReadyState!: GameReadyUpdater;
30+
private navigate!: NavigateFunction;
31+
private showMessage!: ShowMessageFunction;
32+
private showRoomChiefLeaveMessage!: ShowRoomChiefLeaveMessageFunction;
33+
private showResultMessage!: ShowResultMessageFunction;
3434

3535
init(params: WebSocketManagerParams) {
3636
this.roomId = params.roomId;
@@ -42,8 +42,7 @@ class WebSocketManager {
4242
this.showRoomChiefLeaveMessage = params.showRoomChiefLeaveMessage;
4343
}
4444

45-
public static getInstance(
46-
): WebSocketManager{
45+
public static getInstance(): WebSocketManager {
4746
if (!WebSocketManager.instance) {
4847
WebSocketManager.instance = new WebSocketManager();
4948
}
@@ -88,55 +87,57 @@ class WebSocketManager {
8887
});
8988
}
9089

90+
isConnected(): boolean {
91+
return this.client !== null && this.client.connected;
92+
}
93+
9194
// stomp 메세지 데이터 처리 함수
9295
processData(message: any): void {
9396
switch (message.type) {
9497
case 'ROOM':
95-
console.log(`${message.type} 처리`);
96-
this.updateGameState(message.data);
97-
98+
console.log(`${message.type} 처리`);
99+
this.updateGameState(message.data);
98100
break;
99101

100102
case 'GAME_READY':
101-
console.log(`${message.type} 처리`);
102-
this.updateGameReadyState(message.data);
103+
console.log(`${message.type} 처리`);
104+
this.updateGameReadyState(message.data);
103105

104-
// 플레이어 준비 요청
105-
this.playerReadyRequest();
106+
// 플레이어 준비 요청
107+
this.playerReadyRequest();
106108
break;
107109

108110
case 'GAME_START':
109-
console.log(`${message.type} 처리`);
110-
this.updateGameReadyState(message.data);
111+
console.log(`${message.type} 처리`);
112+
this.updateGameReadyState(message.data);
111113

112-
// game 사이트로 이동
113-
this.navigate(`/game/${this.roomId}`);
114+
// game 사이트로 이동
115+
this.navigate(`/game/${this.roomId}`);
114116
break;
115117

116118
case 'GAME_PROGRESS':
117-
console.log(`${message.type} 처리`);
118-
this.updateGameState(message.data);
119+
console.log(`${message.type} 처리`);
120+
this.updateGameState(message.data);
119121
break;
120122

121123
case 'ROOM_LEAVE':
122-
console.log(`${message.type} 처리`);
123-
this.updateGameState(message.data.data);
124-
this.showMessage(`${message.data.target}님이 나갔습니다.`);
125-
124+
console.log(`${message.type} 처리`);
125+
this.updateGameState(message.data.data);
126+
this.showMessage(`${message.data.target}님이 나갔습니다.`);
126127
break;
127128

128129
case 'ROOM_ENTER':
129-
this.showMessage(`${message.data.target}님이 입장하였습니다.`);
130+
this.showMessage(`${message.data.target}님이 입장하였습니다.`);
130131
break;
131132

132133
case 'GAME_END':
133-
console.log(`${message.type} 처리`);
134-
this.updateGameState(message.data);
135-
this.showResultMessage(true);
134+
console.log(`${message.type} 처리`);
135+
this.updateGameState(message.data);
136+
this.showResultMessage(true);
136137
break;
137138

138139
case 'ROOM_CHIEF_LEAVE':
139-
this.showRoomChiefLeaveMessage(true);
140+
this.showRoomChiefLeaveMessage(true);
140141
break;
141142

142143
default:
@@ -198,8 +199,7 @@ class WebSocketManager {
198199
startGameRequest() {
199200
if (this.roomId) {
200201
this.sendMessage(`/app/start/${this.roomId}`);
201-
} else
202-
console.error('Room ID is not set');
202+
} else console.error('Room ID is not set');
203203
}
204204

205205
// 플레이어 준비 요청

0 commit comments

Comments
 (0)