The WebSocket client transport implementation has been refactored and optimized to improve reliability, performance, and maintainability. The changes focus on better connection management, message handling, error recovery, and thread safety.
- Added a proper state machine for connection states (DISCONNECTED, CONNECTING, CONNECTED, CLOSING, ERROR)
- Implemented robust reconnection with exponential backoff
- Added proper mutex protection for connection state changes
- Improved connection timeout handling
- Added a thread-safe message queue for outgoing messages
- Messages are now queued and sent when the connection is established
- Implemented proper message handling in the WebSocket callback
- Added support for both text and binary message types
- Added more detailed error reporting
- Improved cleanup in error cases
- Added timeout handling for operations
- Better handling of connection failures
- Ensured all shared data is properly protected by mutexes
- Implemented proper synchronization for connection state changes
- Added thread-safe message queuing
- Improved resource cleanup
- Refactored common code into helper functions
- Improved code organization and readability
- Added more detailed comments
- Better initialization and cleanup of resources
The refactored WebSocket client transport can be tested using the existing examples/websocket_client.c and examples/websocket_server.c examples. The client should now be more resilient to connection issues and should automatically reconnect when the connection is lost.
Potential future improvements could include:
- Adding support for WebSocket ping/pong for connection health monitoring
- Implementing a configurable message retry mechanism
- Adding support for WebSocket extensions
- Implementing a more sophisticated message prioritization system