WebSocket Echo Application
Overview
The WebSocket Echo Application is a real-time communication server that implements a WebSocket endpoint capable of echoing messages back to connected clients. Built for embedded systems or microcontroller environments, this application demonstrates basic WebSocket server functionality using a network stack framework.
Application Details
Application Name:** WebsocketEcho
Features
- WebSocket Server: Implements a WebSocket endpoint at the pattern "echo*"
- Real-time Echo: Receives messages from WebSocket clients and echoes them back
- Network Stack Integration: Built on top of a custom network initialization framework
- HTTP Server: Includes web server functionality on default port 80
- Connection Management: Handles WebSocket connection lifecycle with proper cleanup
- System Diagnostics: Optional diagnostic capabilities for development
Architecture
Core Components
- WebSocket Endpoint Handler:
CallBackWSEndPoint wsep("echo*", WSStart)
- Registers a WebSocket endpoint that matches URLs starting with "echo"
- Calls
WSStart() callback when new connections are established
- Connection Management:
- Global WebSocket file descriptor (
ws_fd)
- Semaphore-based synchronization (
waitingForWS)
- Select-based I/O multiplexing for non-blocking operations
- Echo Logic:
- Reads incoming WebSocket data
- Writes data to stdout (for logging/debugging)
- Echoes the same data back to the WebSocket client
- Flushes WebSocket buffers to ensure immediate transmission
Application Flow
- Initialization Phase:
- Initialize network stack
- Enable system diagnostics (development feature)
- Start HTTP server on port 80
- Wait for active network connection (5-second timeout)
- Initialize file descriptor sets for select() operations
- Main Loop:
- Wait for WebSocket connection using semaphore
- Monitor WebSocket file descriptor for read/error events
- Handle incoming data by echoing it back to the client
- Manage connection cleanup on errors or disconnections
- Implement 1-second delay between connection attempts
Technical Implementation
Key Functions
WSStart(int rv): Callback function for new WebSocket connections
UserMain(void *pd): Main application entry point and event loop
System Dependencies
- Custom network initialization framework (
init.h)
- WebSocket implementation (
websockets.h)
- POSIX-style socket operations (select, read, write, close)
- Real-time operating system primitives (semaphores, timing)
Buffer Management
- Uses
SMPoolPtr for memory pool management
- Buffer size defined by
ETHER_BUFFER_SIZE
- Automatic buffer cleanup and reuse
Usage
Starting the Application
The application automatically starts when deployed to the target system:
- Network stack initializes
- HTTP server starts on port 80
- WebSocket endpoint becomes available at URLs matching "echo*"
- Application waits for WebSocket connections
Connecting Clients
WebSocket clients can connect to endpoints such as:
ws://[device-ip]/echo
ws://[device-ip]/echo-test
ws://[device-ip]/echo-anything
Message Flow
- Client sends WebSocket message
- Server receives and logs message to stdout
- Server immediately echoes message back to client
- Client receives the echoed message
Development Notes
Production Considerations
- System diagnostics should be disabled in production builds
- Consider implementing authentication for WebSocket endpoints
- Add error handling for memory allocation failures
- Implement rate limiting for DoS protection
Debugging Features
- Real-time message logging to stdout
- Connection status messages
- Socket close notifications
Error Handling
The application includes error handling:
- Automatic connection cleanup on socket errors
- Graceful handling of multiple connection attempts
- Resource management with proper file descriptor cleanup
- Timeout handling for network operations