# WebSocket Testing Guide

## Quick Test - Browser Console

Open the game in your browser and press F12 to open the console. Look for these messages:

### Expected Console Messages
```
🔌 Connecting to WebSocket: wss://echoesoftheashgame.patacuack.net/ws/game/...
✅ WebSocket connected
📨 WebSocket message: connected
```

### Test WebSocket Messages

1. **Test Movement:**
   - Move your character to a new location
   - You should see instant state updates (no 5-second delay)
   - Check console for: `📨 WebSocket message: state_update`

2. **Test Combat:**
   - Enter combat with an NPC
   - Attack and observe instant feedback
   - Check console for: `📨 WebSocket message: combat_update`

3. **Test Multi-User (Optional):**
   - Open game in two different browsers/accounts
   - Move one character
   - Other browser should see "Player arrived" notification

## Verify WebSocket Connection

Run this in your browser console while in the game:

```javascript
// Check if WebSocket is connected
console.log('WebSocket Status:', window.performance.getEntriesByType('resource').filter(r => r.name.includes('/ws/game/')))
```

## Check Server-Side

Check API logs for WebSocket connections:

```bash
cd /opt/dockers/echoes_of_the_ashes
docker compose logs echoes_of_the_ashes_api | grep "WebSocket"
```

You should see:
```
🔌 WebSocket connected: username (player_id=123)
```

## Performance Comparison

### Before WebSocket
- Open browser DevTools Network tab
- Filter for "game" API calls
- Count requests per minute: Should be ~60 requests (5 endpoints × 12 times/min)

### After WebSocket
- Same network tab
- Should see 1 WebSocket connection (ws:// or wss://)
- Regular polling should drop to ~2-6 requests/min (backup polling every 30s)

## Troubleshooting

### WebSocket Not Connecting

1. **Check Token:**
   ```javascript
   console.log('Token:', localStorage.getItem('token'))
   ```
   Should show a JWT token string.

2. **Check CORS/Network:**
   - Look for errors in console
   - Check if using HTTPS (wss://) on production
   - Check if firewall blocking WebSocket

3. **Check API Container:**
   ```bash
   docker compose logs echoes_of_the_ashes_api | tail -50
   ```

### Frequent Disconnections

1. Check heartbeat is working (every 30s)
2. Look for network issues
3. Check nginx timeout settings (if using reverse proxy)

### Messages Not Updating

1. Verify WebSocket shows "connected" in console
2. Check if fallback polling is taking over
3. Look for errors in API logs

## Success Indicators

✅ WebSocket connection established within 2 seconds  
✅ Movement updates are instant (<100ms)  
✅ Combat actions show immediate feedback  
✅ Network tab shows reduced API calls (87% reduction)  
✅ No WebSocket errors in console  
✅ Auto-reconnect works after network interruption  

## Next Steps

Once WebSocket is confirmed working:

1. **Monitor for 24 hours** - Check stability
2. **Gather user feedback** - Are updates faster?
3. **Check server metrics** - CPU/memory usage should be lower
4. **Plan new features** - Live chat, parties, real-time map

## Known Limitations

- WebSocket requires modern browsers (all current browsers support it)
- Fallback polling ensures old browsers still work
- First connection may take 1-2 seconds (JWT validation)

Good luck! 🎮
