-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstomp-test.html
More file actions
216 lines (189 loc) · 8.16 KB
/
stomp-test.html
File metadata and controls
216 lines (189 loc) · 8.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8" />
<title>SockJS / STOMP 테스트 툴</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<!-- Bootstrap CSS -->
<link
href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH"
crossorigin="anonymous"
/>
<!-- CDN에서 SockJS & STOMP 로드 -->
<script src="https://cdn.jsdelivr.net/npm/sockjs-client@1/dist/sockjs.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/stompjs@2.3.3/lib/stomp.min.js"></script>
<style>
body {
font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
#log {
height: 260px;
overflow-y: auto;
white-space: pre-wrap;
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
font-size: 13px;
}
</style>
</head>
<body class="bg-light">
<div class="container py-4">
<div class="row mb-3">
<div class="col">
<h1 class="h3 fw-semibold">SockJS / STOMP WebSocket 테스트 툴</h1>
<p class="text-muted mb-0">엔드포인트 · 구독/전송 destination을 바꿔가며 즉석 테스트용으로 사용하세요.</p>
</div>
</div>
<div class="row g-4">
<!-- 왼쪽: 설정 / 입력 -->
<div class="col-lg-8">
<div class="card shadow-sm mb-3">
<div class="card-header bg-white">
<span class="fw-semibold">연결 / 메시지 설정</span>
</div>
<div class="card-body">
<div class="mb-3">
<label for="endpoint" class="form-label">SockJS 엔드포인트</label>
<input id="endpoint" type="text" class="form-control"
value="http://localhost:8080/ws-connect" />
<div class="form-text">예) http://localhost:8080/ws-connect</div>
</div>
<div class="row g-3">
<div class="col-md-6">
<label for="anonymousId" class="form-label">anonymousId</label>
<input id="anonymousId" type="text" class="form-control" value="1" />
<div class="form-text">예: 1, 42, anon-xxx 등</div>
</div>
<div class="col-md-6">
<label for="roomId" class="form-label">채팅방 ID (chatRoomId)</label>
<input id="roomId" type="number" class="form-control" value="1" />
</div>
</div>
<hr class="my-4" />
<div class="mb-3">
<label for="subDest" class="form-label">구독 destination</label>
<input id="subDest" type="text" class="form-control"
value="/subs/chat-rooms/1" />
</div>
<div class="mb-3">
<label for="sendDest" class="form-label">전송 destination</label>
<input id="sendDest" type="text" class="form-control"
value="/pubs/send" />
<div class="form-text">@MessageMapping("/send") → /pubs/send</div>
</div>
<div class="mb-3">
<label for="message" class="form-label">메시지 내용 (content)</label>
<input id="message" type="text" class="form-control"
value="hello from SockJS tester" />
</div>
</div>
</div>
<div class="d-flex flex-wrap gap-2">
<button class="btn btn-success" onclick="connect()">
연결
</button>
<button class="btn btn-outline-secondary" onclick="disconnect()">
연결 종료
</button>
<button class="btn btn-primary" onclick="sendMessage()">
메시지 전송
</button>
<button class="btn btn-outline-danger" onclick="clearLog()">
로그 지우기
</button>
</div>
</div>
<!-- 오른쪽: 로그 -->
<div class="col-lg-4">
<div class="card shadow-sm h-100">
<div class="card-header bg-white d-flex justify-content-between align-items-center">
<span class="fw-semibold">로그</span>
<span class="badge text-bg-secondary">실시간</span>
</div>
<div class="card-body p-2">
<pre id="log" class="bg-dark text-light rounded-2 p-2 mb-0"></pre>
</div>
</div>
</div>
</div>
</div>
<!-- Bootstrap JS (선택) -->
<script
src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"
integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz"
crossorigin="anonymous"
></script>
<script>
let stompClient = null;
function logLine(text) {
const log = document.getElementById('log');
const now = new Date().toISOString().substring(11, 19);
log.textContent += `[${now}] ${text}\n`;
log.scrollTop = log.scrollHeight;
}
function clearLog() {
document.getElementById('log').textContent = '';
}
function connect() {
clearLog();
const endpoint = document.getElementById('endpoint').value.trim();
const subDest = document.getElementById('subDest').value.trim();
const anonymousId = document.getElementById('anonymousId').value.trim();
logLine(`SockJS 연결 시도: ${endpoint}`);
const sock = new SockJS(endpoint);
stompClient = Stomp.over(sock);
stompClient.debug = null; // 콘솔 로그 끄기
// CONNECT 헤더 구성
const headers = {};
if (anonymousId) {
headers["anonymousId"] = anonymousId;
logLine(`CONNECT 헤더 anonymousId=${anonymousId}`);
} else {
logLine("경고: anonymousId가 비어 있습니다. 서버에서 거부할 수 있습니다.");
}
stompClient.connect(
headers,
function onConnect(frame) {
logLine("STOMP 연결 성공: " + frame);
if (subDest) {
logLine("구독: " + subDest);
stompClient.subscribe(subDest, function (message) {
logLine("수신: destination=" + message.headers.destination + ", body=" + message.body);
});
}
},
function onError(error) {
logLine("연결 실패 / 에러: " + error);
}
);
}
function disconnect() {
if (stompClient && stompClient.connected) {
stompClient.disconnect(() => {
logLine("연결 종료 완료");
});
} else {
logLine("현재 연결되어 있지 않습니다.");
}
}
function sendMessage() {
if (!stompClient || !stompClient.connected) {
logLine("먼저 연결을 수행하세요.");
return;
}
const dest = document.getElementById('sendDest').value.trim();
const roomIdRaw = document.getElementById('roomId').value.trim();
const msg = document.getElementById('message').value;
const roomId = roomIdRaw === "" ? null : Number(roomIdRaw);
const payload = {
chatRoomId: roomId,
content: msg
// 필요하면 여기서 더 필드 추가: messageType, senderId 등
};
logLine(`전송 → destination=${dest}, payload=${JSON.stringify(payload)}`);
stompClient.send(dest, {}, JSON.stringify(payload));
}
</script>
</body>
</html>