-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
221 lines (205 loc) · 8.49 KB
/
index.html
File metadata and controls
221 lines (205 loc) · 8.49 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
217
218
219
220
221
<!DOCTYPE html>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>Chat App Client</title>
<!-- Axios CDN -->
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<!-- Socket.IO Client CDN -->
<script src="https://cdn.socket.io/4.5.1/socket.io.min.js"></script>
<style>
body { font-family: Arial, sans-serif; margin: 0; padding: 0; background: #f2f2f2; }
header { background-color: #333; color: #fff; padding: 10px; text-align: center; }
.container { max-width: 600px; margin: 20px auto; background: #fff; padding: 20px; border-radius: 5px; }
.hidden { display: none; }
form { margin-bottom: 20px; }
input[type="text"], input[type="password"], input[type="email"] {
width: 100%; padding: 8px; margin: 5px 0 10px; box-sizing: border-box;
}
button { padding: 10px 15px; margin-right: 10px; }
.chat-messages {
border: 1px solid #ccc; height: 300px; overflow-y: auto; padding: 10px; background: #fafafa;
}
.chat-input { width: 100%; padding: 10px; margin-top: 10px; box-sizing: border-box; }
h2 { border-bottom: 1px solid #ddd; padding-bottom: 5px; }
</style>
</head>
<body>
<header>
<h1>Chat App</h1>
</header>
<div class="container">
<!-- 인증 영역 -->
<section id="auth-section">
<h2>인증</h2>
<button id="show-signup">회원가입</button>
<button id="show-login">로그인</button>
<div id="signup-form" class="hidden">
<h3>회원가입</h3>
<form id="form-signup">
<input type="text" id="signup-username" placeholder="사용자 이름" required>
<input type="email" id="signup-email" placeholder="이메일" required>
<input type="password" id="signup-password" placeholder="비밀번호" required>
<button type="submit">회원가입</button>
</form>
</div>
<div id="login-form" class="hidden">
<h3>로그인</h3>
<form id="form-login">
<input type="text" id="login-username" placeholder="사용자 이름" required>
<input type="password" id="login-password" placeholder="비밀번호" required>
<button type="submit">로그인</button>
</form>
</div>
</section>
<!-- 방 관련 영역 -->
<section id="room-section" class="hidden">
<h2>방 관리</h2>
<button id="create-room-btn">방 생성</button>
<button id="join-room-btn">방 입장</button>
<div id="create-room-form" class="hidden">
<h3>방 생성</h3>
<form id="form-create-room">
<input type="text" id="room-name-create" placeholder="방 이름" required>
<button type="submit">생성</button>
</form>
</div>
<div id="join-room-form" class="hidden">
<h3>방 입장</h3>
<form id="form-join-room">
<input type="text" id="room-name-join" placeholder="방 이름" required>
<button type="submit">입장</button>
</form>
</div>
</section>
<!-- 채팅 영역 -->
<section id="chat-section" class="hidden">
<h2>채팅 - 방: <span id="current-room-name"></span></h2>
<div class="chat-messages" id="chat-messages"></div>
<input type="text" id="chat-input" class="chat-input" placeholder="메시지 입력 후 엔터">
</section>
</div>
<script>
const BASE_URL = 'http://castberry:36000';
let authToken = null;
let socket = null;
let currentRoom = null;
// 섹션 보이기/숨기기 함수
function showSection(id) {
document.getElementById('auth-section').classList.add('hidden');
document.getElementById('room-section').classList.add('hidden');
document.getElementById('chat-section').classList.add('hidden');
if(document.getElementById(id)) {
document.getElementById(id).classList.remove('hidden');
}
}
// 채팅 메시지 추가 함수
function appendMessage(message) {
const msgDiv = document.getElementById('chat-messages');
const p = document.createElement('p');
p.textContent = message;
msgDiv.appendChild(p);
msgDiv.scrollTop = msgDiv.scrollHeight;
}
// 회원가입, 로그인 폼 토글
document.getElementById('show-signup').addEventListener('click', () => {
document.getElementById('signup-form').classList.remove('hidden');
document.getElementById('login-form').classList.add('hidden');
});
document.getElementById('show-login').addEventListener('click', () => {
document.getElementById('login-form').classList.remove('hidden');
document.getElementById('signup-form').classList.add('hidden');
});
// 회원가입 처리
document.getElementById('form-signup').addEventListener('submit', async (e) => {
e.preventDefault();
const username = document.getElementById('signup-username').value;
const email = document.getElementById('signup-email').value;
const password = document.getElementById('signup-password').value;
try {
const res = await axios.post(BASE_URL + '/auth/signup', { username, email, password });
alert('회원가입 성공! 로그인 해주세요.');
} catch(err) {
alert('회원가입 오류: ' + (err.response?.data?.message || err.message));
}
});
// 로그인 처리
document.getElementById('form-login').addEventListener('submit', async (e) => {
e.preventDefault();
const username = document.getElementById('login-username').value;
const password = document.getElementById('login-password').value;
try {
const res = await axios.post(BASE_URL + '/auth/login', { username, password });
authToken = res.data.token;
axios.defaults.headers.common['Authorization'] = 'Bearer ' + authToken;
alert('로그인 성공!');
// 로그인 후 방 관리 영역 표시
showSection('room-section');
} catch(err) {
alert('로그인 오류: ' + (err.response?.data?.message || err.message));
}
});
// 방 생성 토글 및 처리
document.getElementById('create-room-btn').addEventListener('click', () => {
document.getElementById('create-room-form').classList.toggle('hidden');
});
document.getElementById('form-create-room').addEventListener('submit', async (e) => {
e.preventDefault();
const roomName = document.getElementById('room-name-create').value;
try {
const res = await axios.post(BASE_URL + '/rooms', { name: roomName });
alert('방 생성 완료: ' + roomName);
} catch(err) {
alert('방 생성 오류: ' + (err.response?.data?.message || err.message));
}
});
// 방 입장 토글 및 처리
document.getElementById('join-room-btn').addEventListener('click', () => {
document.getElementById('join-room-form').classList.toggle('hidden');
});
document.getElementById('form-join-room').addEventListener('submit', async (e) => {
e.preventDefault();
const roomName = document.getElementById('room-name-join').value;
currentRoom = roomName;
document.getElementById('current-room-name').textContent = roomName;
// 소켓 연결 (기존 소켓이 있다면 종료)
if(socket) {
socket.disconnect();
}
socket = io(BASE_URL, {
auth: { token: authToken }
});
socket.on('connect', () => {
console.log('소켓 연결됨: ' + socket.id);
// 방 입장 요청 (ephemeral)
socket.emit('joinRoom', roomName, (response) => {
console.log('방 입장 응답:', response);
});
});
socket.on('chatMessage', (data) => {
const { user, text, createdAt } = data;
const time = createdAt ? new Date(createdAt).toLocaleTimeString() : '';
appendMessage(`[${time}] ${user.username}: ${text}`);
});
socket.on('disconnect', () => {
console.log('소켓 연결 종료');
});
// 채팅 영역 표시
showSection('chat-section');
});
// 채팅 메시지 전송 (엔터키 입력 시)
document.getElementById('chat-input').addEventListener('keypress', (e) => {
if(e.key === 'Enter') {
const message = e.target.value;
if(message && socket && currentRoom) {
socket.emit('chatMessage', { roomName: currentRoom, message });
const time = new Date().toLocaleTimeString();
appendMessage(`[${time}] Me: ${message}`);
e.target.value = '';
}
}
});
</script>
</body>
</html>