# WebSocket 연결 실패 해결 방법

**문제**: `wss://gr.ezywallets.com/ws` 연결 실패

**원인**: Cloudflare가 WebSocket 연결을 차단하고 있을 가능성

---

## ✅ 해결 방법 1: Cloudflare에서 WebSocket 활성화

### 1단계: Cloudflare 대시보드 접속
1. https://dash.cloudflare.com 로그인
2. `gr.ezywallets.com` 도메인 선택

### 2단계: WebSocket 활성화
1. 좌측 메뉴에서 **"Network"** 클릭
2. **"WebSockets"** 항목 찾기
3. **ON으로 설정**

### 3단계: SSL/TLS 설정 확인
1. 좌측 메뉴에서 **"SSL/TLS"** 클릭
2. **"Full"** 또는 **"Full (strict)"** 선택
   - ⚠️ "Flexible"은 WebSocket이 작동하지 않을 수 있음

### 4단계: 방화벽 규칙 확인
1. 좌측 메뉴에서 **"Security" → "WAF"** 클릭
2. `/ws` 경로에 대한 차단 규칙이 있는지 확인
3. 있다면 허용 규칙 추가:
   ```
   Field: URI Path
   Operator: equals
   Value: /ws
   Action: Allow
   ```

---

## ✅ 해결 방법 2: 서브도메인 사용 (권장)

Cloudflare를 우회하여 직접 연결:

### 1단계: DNS 레코드 추가
Cloudflare DNS 설정에서:
```
Type: A
Name: ws (또는 websocket)
IPv4 address: 서버 IP (45.76.221.251)
Proxy status: DNS only (회색 구름) ⚠️ 프록시 OFF!
TTL: Auto
```

### 2단계: Nginx 설정 추가
`C:\laragon\etc\nginx\sites-enabled\` 에 새 파일 생성:
**`ws.gr.ezywallets.com.conf`**
```nginx
server {
    listen 80;
    listen 443 ssl;
    server_name ws.gr.ezywallets.com;

    # SSL 설정
    ssl_certificate "C:/laragon/etc/ssl/ezywallets.pem";
    ssl_certificate_key "C:/laragon/etc/ssl/ezywallets.key";

    # WebSocket
    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_read_timeout 86400;
    }
}
```

### 3단계: 프론트엔드 환경변수 수정
`frontend/apps/up-down/.env.production`:
```
VITE_WS_URL=wss://ws.gr.ezywallets.com
```

### 4단계: Nginx 재시작
```bash
nginx -s reload
```

---

## ✅ 해결 방법 3: 포트 직접 노출 (임시)

보안상 권장하지 않지만 테스트용:

### 1단계: 방화벽에서 8080 포트 오픈
Windows 방화벽에서 8080 포트 허용

### 2단계: 프론트엔드 환경변수 수정
```
VITE_WS_URL=wss://gr.ezywallets.com:8080
```

### 3단계: Nginx에서 8080 프록시 설정
```nginx
server {
    listen 8080 ssl;
    server_name gr.ezywallets.com;

    ssl_certificate "C:/laragon/etc/ssl/ezywallets.pem";
    ssl_certificate_key "C:/laragon/etc/ssl/ezywallets.key";

    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}
```

---

## 🔍 현재 상태 확인

### WebSocket 서버 실행 확인
```bash
netstat -ano | findstr :8080
```

출력 예시:
```
TCP    0.0.0.0:8080           0.0.0.0:0              LISTENING       9968
```
✅ **정상**: 서버가 실행 중입니다.

### 로컬에서 WebSocket 테스트
브라우저 개발자 도구 콘솔에서:
```javascript
const ws = new WebSocket('ws://localhost:8080');
ws.onopen = () => console.log('✅ Connected!');
ws.onerror = (e) => console.error('❌ Error:', e);
```

---

## 📝 권장 순서

1. ✅ **먼저 해결 방법 1 시도** (Cloudflare 설정)
   - 가장 간단하고 안전
   - 추가 설정 불필요

2. ✅ **안 되면 해결 방법 2** (서브도메인)
   - Cloudflare 우회
   - 안정적인 WebSocket 연결

3. ❌ **해결 방법 3은 비추천** (보안 취약)

---

## 🚨 빠른 임시 해결

프론트엔드 빌드 후 로컬 테스트:

### 개발 환경에서 테스트
`frontend/apps/up-down/.env.development`:
```
VITE_WS_URL=ws://localhost:8080
```

그러면 로컬에서는 WebSocket이 작동합니다.

**프로덕션 배포 전에** 위 해결 방법 중 하나를 적용하세요.

---

## 📞 확인 방법

WebSocket 연결 성공 시 콘솔 로그:
```
[BackendWS] Connecting to wss://...
[BackendWS] Connected ✅
[BackendWS] Ping sent
[BackendWS] Pong received
```

---

**작성일**: 2026-01-16
**우선순위**: 해결 방법 1 → 해결 방법 2
