Skip to content

Commit 660adb8

Browse files
committed
chore(debug): 디버깅 및 테스트를 위한 핸들러 수정, 로그 추가
1 parent ef50b5a commit 660adb8

3 files changed

Lines changed: 26 additions & 33 deletions

File tree

src/main/java/app/benchmark/WebSocketBenchmarkHandler.java

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -53,24 +53,19 @@ public void onError(WebSocketSession session, Throwable error) {
5353
error.printStackTrace();
5454
}
5555

56-
/**
57-
* Echo: 받은 메시지를 그대로 반환
58-
*/
5956
@MessageMapping("/echo")
6057
public void handleEcho(WebSocketSession session, @Payload String message) throws IOException {
6158
totalMessagesReceived++;
6259
session.sendText(createResponse("/echo", message));
60+
System.out.println("[WebSocket Benchmark] Echo: " + message);
6361
totalMessagesSent++;
6462
}
6563

66-
/**
67-
* Broadcast: 모든 연결된 클라이언트에 메시지 전송
68-
*/
6964
@MessageMapping("/broadcast")
7065
public void handleBroadcast(WebSocketSession session, @Payload String message) throws IOException {
7166
totalMessagesReceived++;
7267
String response = createResponse("/broadcast", "From " + session.getId() + ": " + message);
73-
68+
System.out.println("[WebSocket Benchmark] Broadcast: " + message);
7469
for (WebSocketSession s : sessions.values()) {
7570
if (s.isOpen()) {
7671
try {
@@ -83,9 +78,6 @@ public void handleBroadcast(WebSocketSession session, @Payload String message) t
8378
}
8479
}
8580

86-
/**
87-
* Chat: 채팅 메시지 처리
88-
*/
8981
@MessageMapping("/chat")
9082
public void handleChat(WebSocketSession session, @Payload String message) throws IOException {
9183
totalMessagesReceived++;
@@ -96,6 +88,7 @@ public void handleChat(WebSocketSession session, @Payload String message) throws
9688
}
9789

9890
String chatMessage = username + ": " + message;
91+
System.out.println("[WebSocket Benchmark] Chat: " + chatMessage);
9992
String response = createResponse("/chat", chatMessage);
10093

10194
// 채팅방의 모든 사용자에게 전송
@@ -111,21 +104,18 @@ public void handleChat(WebSocketSession session, @Payload String message) throws
111104
}
112105
}
113106

114-
/**
115-
* Ping-Pong: 간단한 응답
116-
*/
117107
@MessageMapping("/ping")
118-
public void handlePing(WebSocketSession session, String message) throws IOException {
108+
public void handlePing(WebSocketSession session, @Payload String message) throws IOException {
119109
totalMessagesReceived++;
120-
session.sendText(createResponse("/pong", "pong"));
110+
// 실제 WebSocket Ping 프레임 전송 (브라우저가 자동으로 Pong 응답)
111+
byte[] pingData = "ping".getBytes();
112+
session.sendPing(pingData);
113+
System.out.println("[WebSocket Benchmark] Sent Ping frame to client: " + session.getId());
121114
totalMessagesSent++;
122115
}
123116

124-
/**
125-
* Stats: 통계 정보 반환
126-
*/
127117
@MessageMapping("/stats")
128-
public void handleStats(WebSocketSession session, String message) throws IOException {
118+
public void handleStats(WebSocketSession session, @Payload String message) throws IOException {
129119
totalMessagesReceived++;
130120
String stats = String.format(
131121
"연결: %d, 수신: %d, 송신: %d, 누적 연결: %d",
@@ -135,18 +125,12 @@ public void handleStats(WebSocketSession session, String message) throws IOExcep
135125
totalMessagesSent++;
136126
}
137127

138-
/**
139-
* JSON 응답 메시지 생성
140-
*/
141128
private String createResponse(String destination, String payload) {
142129
// JSON 형식: {"destination": "...", "payload": "..."}
143130
return String.format("{\"destination\":\"%s\",\"payload\":\"%s\"}",
144131
destination, escapeJson(payload));
145132
}
146133

147-
/**
148-
* JSON 문자열 이스케이프
149-
*/
150134
private String escapeJson(String str) {
151135
if (str == null) return "";
152136
return str.replace("\\", "\\\\")
@@ -156,14 +140,12 @@ private String escapeJson(String str) {
156140
.replace("\t", "\\t");
157141
}
158142

159-
// 통계 초기화 (테스트용)
160143
public static void resetStats() {
161144
totalMessagesReceived = 0;
162145
totalMessagesSent = 0;
163146
totalConnections = 0;
164147
}
165148

166-
// 통계 조회 (테스트용)
167149
public static String getStats() {
168150
return String.format(
169151
"Sessions: %d, Received: %d, Sent: %d, Total Connections: %d",

src/main/java/sprout/server/argument/WebSocketArgumentResolver.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,5 @@
66

77
public interface WebSocketArgumentResolver {
88
boolean supports(Parameter parameter, InvocationContext context); // <- context 추가
9-
109
Object resolve(Parameter parameter, InvocationContext context) throws Exception; // <- context로 통합
1110
}

src/main/java/sprout/server/websocket/DefaultWebSocketSession.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,17 +181,29 @@ private void scheduleWrite(ByteBuffer buf) {
181181

182182
private void processFrame(WebSocketFrame frame) throws Exception {
183183
if (WebSocketFrameDecoder.isCloseFrame(frame)) {
184-
callOnCloseMethod(WebSocketFrameDecoder.getCloseCode(frame.getPayloadBytes()));
184+
byte[] payload = frame.getPayloadBytes();
185+
System.out.printf("[DEBUG] Close frame - Payload length: %d%n", payload.length);
186+
if (payload.length >= 2) {
187+
int code = ((payload[0] & 0xFF) << 8) | (payload[1] & 0xFF);
188+
System.out.printf("[DEBUG] Close code: %d (0x%X)%n", code, code);
189+
}
190+
callOnCloseMethod(WebSocketFrameDecoder.getCloseCode(payload));
185191
return;
186192
} else if (WebSocketFrameDecoder.isPingFrame(frame)) {
187-
System.out.println("Received Ping frame from client " + id);
188-
sendPong(frame.getPayloadBytes());
193+
byte[] payload = frame.getPayloadBytes();
194+
System.out.printf("[DEBUG] Received Ping frame from client %s (payload: %d bytes)%n", id, payload.length);
195+
sendPong(payload);
196+
return;
189197
} else if (WebSocketFrameDecoder.isPongFrame(frame)) {
190-
System.out.println("Received Pong frame from client " + id);
198+
byte[] payload = frame.getPayloadBytes(); // 반드시 스트림을 소비해야 함!
199+
System.out.printf("[DEBUG] Received Pong frame from client %s (payload: %d bytes)%n", id, payload.length);
200+
return;
191201
} else if (WebSocketFrameDecoder.isDataFrame(frame)) {
192202
dispatchMessage(frame);
193203
} else {
194-
System.err.println("Unknown or unsupported WebSocket opcode: " + frame.getOpcode());
204+
System.err.printf("[ERROR] Unknown WebSocket opcode: 0x%X%n", frame.getOpcode());
205+
// 알 수 없는 opcode의 경우에도 payload를 소비해야 함
206+
frame.getPayloadBytes();
195207
callOnErrorMethod(new WebSocketException("Unknown WebSocket opcode: " + frame.getOpcode()));
196208
}
197209
}

0 commit comments

Comments
 (0)