Best Practices
1. WebSocket Connection
- Always check
readyState === WebSocket.OPEN - Check connection status before sending messages
- Use auto-reconnect feature
2. Token Management
- Store tokens in localStorage
- Check token refresh time
- Send token in every message
3. State Management
- Update state only with
gameStatemessages from server - Keep local state synchronized with server state
4. Event Handling
- Clean up event listeners on component unmount
- Keep event payloads type-safe
5. Error Handling
- Catch all WebSocket errors
- Show meaningful error messages to users
- Add retry mechanism
6. Play Again Flow
- Track the 30-second timeout
- Show/hide modals at the right time
- Don't forget to send message to parent window
Important Notes
-
Token Refresh: Tokens are automatically refreshed.
shouldRefreshTokencheck is performed. -
State Recovery: When connection is lost, state is recovered via
state_recoverymessage. -
Action State: After each action, action state is reported via
actionStatefield. -
Timer Management: Timers are managed by server. Client only displays them.
-
Penalty System: Penalty system is managed by server and comes in
gameState. -
Meld Management: Sets (melds) are validated by server and stored in
openSeriesfield. -
Play Again Timeout: There is a 30-second timeout at game end. Play again request can be sent during this time.
-
Parent Window Communication:
window.parent.postMessageis used to return to lobby.
Last Updated: 2024