|
| 1 | +import { useEffect, useRef, useState } from 'react'; |
| 2 | + |
| 3 | +export default function ChatPageTest() { |
| 4 | + const [output, setOutput] = useState(''); |
| 5 | + const [input, setInput] = useState(''); |
| 6 | + const [sessionId, setSessionId] = useState<string | null>(null); |
| 7 | + const socketRef = useRef<WebSocket | null>(null); |
| 8 | + const outputRef = useRef<HTMLTextAreaElement>(null); |
| 9 | + |
| 10 | + const mountedRef = useRef(false); |
| 11 | + |
| 12 | + useEffect(() => { |
| 13 | + if (mountedRef.current) return; |
| 14 | + mountedRef.current = true; |
| 15 | + |
| 16 | + const socket = new WebSocket('wss://backendbase.site/ws/chat'); |
| 17 | + socketRef.current = socket; |
| 18 | + |
| 19 | + socket.onopen = () => { |
| 20 | + console.log('Connected'); |
| 21 | + setOutput((prev) => prev + 'Connected\n'); |
| 22 | + }; |
| 23 | + |
| 24 | + socket.onmessage = (event) => { |
| 25 | + // bedrock stream done 메시지는 콘솔에만 표시 |
| 26 | + if (event.data.includes('[bedrock stream done]')) { |
| 27 | + console.log('Stream done:', event.data); |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + try { |
| 32 | + const json = JSON.parse(event.data); |
| 33 | + if (json.type === 'session') { |
| 34 | + setSessionId(json.session_id); |
| 35 | + setOutput((prev) => prev + `[세션 ID: ${json.session_id}]\n`); |
| 36 | + return; |
| 37 | + } |
| 38 | + } catch { |
| 39 | + /* not JSON */ |
| 40 | + } |
| 41 | + setOutput((prev) => prev + event.data + '\n'); |
| 42 | + }; |
| 43 | + |
| 44 | + socket.onerror = (error) => { |
| 45 | + console.error('WebSocket Error:', error); |
| 46 | + setOutput((prev) => prev + 'Error occurred\n'); |
| 47 | + }; |
| 48 | + |
| 49 | + socket.onclose = (event) => { |
| 50 | + console.log('Disconnected:', event.code, event.reason); |
| 51 | + setOutput((prev) => prev + `Disconnected: ${event.code}\n`); |
| 52 | + }; |
| 53 | + |
| 54 | + return () => { |
| 55 | + socket.close(); |
| 56 | + }; |
| 57 | + }, []); |
| 58 | + |
| 59 | + // 자동 스크롤 |
| 60 | + useEffect(() => { |
| 61 | + if (outputRef.current) { |
| 62 | + outputRef.current.scrollTop = outputRef.current.scrollHeight; |
| 63 | + } |
| 64 | + }, [output]); |
| 65 | + |
| 66 | + const sendMessage = () => { |
| 67 | + const text = input.trim(); |
| 68 | + if (!text || !socketRef.current) return; |
| 69 | + |
| 70 | + if (socketRef.current.readyState === WebSocket.OPEN) { |
| 71 | + socketRef.current.send(JSON.stringify({ role: 'user', content: text })); |
| 72 | + setInput(''); |
| 73 | + } else { |
| 74 | + setOutput((prev) => prev + 'WebSocket이 연결되지 않음\n'); |
| 75 | + } |
| 76 | + }; |
| 77 | + |
| 78 | + const handleKeyPress = (e: React.KeyboardEvent<HTMLInputElement>) => { |
| 79 | + if (e.key === 'Enter') { |
| 80 | + sendMessage(); |
| 81 | + } |
| 82 | + }; |
| 83 | + |
| 84 | + return ( |
| 85 | + <div style={{ padding: '20px', maxWidth: '600px', margin: '0 auto' }}> |
| 86 | + <h3>Chat Test</h3> |
| 87 | + {sessionId && <p style={{ color: '#666' }}>세션 ID: {sessionId}</p>} |
| 88 | + <textarea |
| 89 | + ref={outputRef} |
| 90 | + value={output} |
| 91 | + readOnly |
| 92 | + rows={15} |
| 93 | + style={{ |
| 94 | + width: '100%', |
| 95 | + marginBottom: '10px', |
| 96 | + padding: '10px', |
| 97 | + border: '1px solid #ccc', |
| 98 | + borderRadius: '4px', |
| 99 | + fontFamily: 'monospace', |
| 100 | + }} |
| 101 | + /> |
| 102 | + <br /> |
| 103 | + <div style={{ display: 'flex', gap: '10px' }}> |
| 104 | + <input |
| 105 | + value={input} |
| 106 | + onChange={(e) => setInput(e.target.value)} |
| 107 | + onKeyPress={handleKeyPress} |
| 108 | + placeholder="메시지를 입력하세요" |
| 109 | + style={{ |
| 110 | + flex: 1, |
| 111 | + padding: '8px', |
| 112 | + border: '1px solid #ccc', |
| 113 | + borderRadius: '4px', |
| 114 | + }} |
| 115 | + /> |
| 116 | + <button |
| 117 | + onClick={sendMessage} |
| 118 | + style={{ |
| 119 | + padding: '8px 20px', |
| 120 | + background: '#007bff', |
| 121 | + color: 'white', |
| 122 | + border: 'none', |
| 123 | + borderRadius: '4px', |
| 124 | + cursor: 'pointer', |
| 125 | + }} |
| 126 | + > |
| 127 | + 보내기 |
| 128 | + </button> |
| 129 | + </div> |
| 130 | + </div> |
| 131 | + ); |
| 132 | +} |
0 commit comments