22< html lang ="en ">
33< head >
44 < meta charset ="UTF-8 " />
5- < title > Socket.IO 채팅</ title >
5+ < title > Socket.IO 채팅 (JWT 토큰 입력) </ title >
66 < script src ="https://cdn.socket.io/4.6.1/socket.io.min.js "> </ script >
77</ head >
88< body >
9- < h2 > 실시간 채팅</ h2 >
9+ < h2 > 실시간 채팅 (JWT 인증) </ h2 >
1010
1111 < div >
12- < label for ="senderIdInput " > 사용자 ID 입력:</ label >
13- < input type ="text " id ="senderIdInput " placeholder ="사용자 ID를 입력하세요 " />
14- < button id ="joinBtn " > 채팅방 입장</ button >
12+ < label for ="tokenInput " > JWT 토큰 입력:</ label >
13+ < input type ="text " id ="tokenInput " placeholder ="JWT 토큰을 입력하세요" style =" width: 400px; " />
14+ < button id ="connectBtn " > 서버 연결 및 채팅방 입장</ button >
1515 </ div >
16-
17- < div id ="chatroom " style ="display:none; ">
16+
17+ < div id ="chatroom " style ="display:none; margin-top: 20px; ">
1818 < div id ="messages " style ="border:1px solid #ccc; height:300px; overflow-y:scroll; margin-bottom:10px; padding:5px; "> </ div >
1919
2020 < input type ="text " id ="messageInput " placeholder ="메시지 입력 (최대 255자) " maxlength ="255 " style ="width:60%; " />
2121 < input type ="file " id ="imageInput " accept ="image/* " />
2222 < button id ="sendBtn "> 전송</ button >
2323 </ div >
24-
24+
2525 < script >
26- // 서버 소켓 연결 (URL 바꾸기)
27- const socket = io ( 'http://localhost:3000' ) ;
26+ let socket = null ;
2827 const chatroomId = '1' ; // 실제 채팅방 아이디 동적으로 받아야 함
2928
30- let senderId = null ;
29+ const tokenInput = document . getElementById ( 'tokenInput' ) ;
30+ const connectBtn = document . getElementById ( 'connectBtn' ) ;
3131
32- const joinBtn = document . getElementById ( 'joinBtn' ) ;
33- const senderIdInput = document . getElementById ( 'senderIdInput' ) ;
3432 const chatroomDiv = document . getElementById ( 'chatroom' ) ;
35-
3633 const messagesDiv = document . getElementById ( 'messages' ) ;
3734 const messageInput = document . getElementById ( 'messageInput' ) ;
3835 const imageInput = document . getElementById ( 'imageInput' ) ;
3936 const sendBtn = document . getElementById ( 'sendBtn' ) ;
4037
41- joinBtn . addEventListener ( 'click' , ( ) => {
42- const inputVal = senderIdInput . value . trim ( ) ;
43- if ( ! inputVal ) {
44- alert ( '사용자 ID를 입력해주세요.' ) ;
38+ connectBtn . addEventListener ( 'click' , ( ) => {
39+ const token = tokenInput . value . trim ( ) ;
40+ if ( ! token ) {
41+ alert ( 'JWT 토큰을 입력해주세요.' ) ;
4542 return ;
4643 }
47- senderId = inputVal ;
4844
49- // 채팅방 입장
50- socket . emit ( 'join' , chatroomId ) ;
51-
52- // UI 변경
53- senderIdInput . disabled = true ;
54- joinBtn . disabled = true ;
55- chatroomDiv . style . display = 'block' ;
56- } ) ;
57-
58- // 메시지 받기
59- socket . on ( 'chat message' , ( msg ) => {
60- const div = document . createElement ( 'div' ) ;
61- div . style . borderBottom = '1px solid #eee' ;
62- div . style . padding = '5px' ;
63-
64- let content = `<b>사용자 ${ msg . senderId } :</b> ${ msg . content || '' } ` ;
65- if ( msg . imageUrl ) {
66- content += `<br><img src="${ msg . imageUrl } " alt="이미지" style="max-width:200px; max-height:200px;" />` ;
45+ // 기존 연결 있으면 끊기
46+ if ( socket ) {
47+ socket . disconnect ( ) ;
6748 }
68- div . innerHTML = content ;
69-
70- messagesDiv . appendChild ( div ) ;
71- messagesDiv . scrollTop = messagesDiv . scrollHeight ;
72- } ) ;
7349
74- // 에러 받기
75- socket . on ( 'error' , ( err ) => {
76- alert ( '에러: ' + err . message ) ;
50+ // 소켓 연결 (토큰을 auth에 담아 보냄)
51+ socket = io ( 'http://localhost:3000' , {
52+ auth : {
53+ token : token ,
54+ }
55+ } ) ;
56+
57+ socket . on ( 'connect' , ( ) => {
58+ console . log ( '서버 연결됨, 채팅방 입장' ) ;
59+ socket . emit ( 'join' , chatroomId ) ;
60+
61+ chatroomDiv . style . display = 'block' ;
62+ tokenInput . disabled = true ;
63+ connectBtn . disabled = true ;
64+ } ) ;
65+
66+ socket . on ( 'chat message' , ( msg ) => {
67+ const div = document . createElement ( 'div' ) ;
68+ div . style . borderBottom = '1px solid #eee' ;
69+ div . style . padding = '5px' ;
70+
71+ let content = `<b>사용자 ${ msg . senderId } :</b> ${ msg . content || '' } ` ;
72+ if ( msg . imageUrl ) {
73+ content += `<br><img src="${ msg . imageUrl } " alt="이미지" style="max-width:200px; max-height:200px;" />` ;
74+ }
75+ div . innerHTML = content ;
76+
77+ messagesDiv . appendChild ( div ) ;
78+ messagesDiv . scrollTop = messagesDiv . scrollHeight ;
79+ } ) ;
80+
81+ socket . on ( 'error' , ( err ) => {
82+ alert ( '에러: ' + err . message ) ;
83+ } ) ;
84+
85+ socket . on ( 'disconnect' , ( ) => {
86+ alert ( '서버 연결이 끊겼습니다.' ) ;
87+ chatroomDiv . style . display = 'none' ;
88+ tokenInput . disabled = false ;
89+ connectBtn . disabled = false ;
90+ } ) ;
7791 } ) ;
7892
79- // 전송 버튼 클릭
8093 sendBtn . addEventListener ( 'click' , ( ) => {
81- if ( ! senderId ) {
82- alert ( '먼저 사용자 ID를 입력하고 채팅방에 입장하세요 .' ) ;
94+ if ( ! socket || ! socket . connected ) {
95+ alert ( '서버에 연결되어 있지 않습니다 .' ) ;
8396 return ;
8497 }
8598
@@ -102,7 +115,6 @@ <h2>실시간 채팅</h2>
102115
103116 socket . emit ( 'chat message' , {
104117 chatroomId,
105- senderId,
106118 content,
107119 imageBase64 : base64Image ,
108120 } ) ;
@@ -111,7 +123,6 @@ <h2>실시간 채팅</h2>
111123 } else {
112124 socket . emit ( 'chat message' , {
113125 chatroomId,
114- senderId,
115126 content,
116127 } ) ;
117128 }
0 commit comments