WebSocket Connect Application
Overview
This application demonstrates a WebSocket client implementation that connects to a server and maintains a persistent connection for real-time communication.
Features
- WebSocket Client Connection: Establishes a connection to a WebSocket server
- Real-time Data Exchange: Sends periodic time tick messages to the server
- Asynchronous Socket Handling: Uses a dedicated task for reading incoming data
- Network Stack Integration: Built on NetBurner's network initialization framework
- Error Handling: Monitors socket status and handles connection errors gracefully
Application Structure
The application consists of two main components:
Main Task (UserMain)
- Initializes the network stack and waits for DHCP
- Establishes WebSocket connection to the specified server
- Sends periodic "TimeTick" messages every second
- Creates a separate task for handling incoming socket data
Socket Handling Task (UserTask)
- Monitors the WebSocket connection using
select()
- Handles incoming data from the server
- Manages socket errors and connection cleanup
- Echoes received data to stdout
Configuration
The application is configured with the following default settings:
#define SERVER_IP "192.168.1.209"
- Server IP: 192.168.1.209
- WebSocket Path: "/"
- Port: 80
- Message Interval: 1 second
Key Functions
Connection Management
Network Operations
select(): Monitors socket file descriptors for activity
read(): Receives data from the WebSocket
writestring(): Sends string data through the WebSocket
writeall(): Outputs received data to stdout
System Integration
init(): Initializes the NetBurner network stack
WaitForActiveNetwork(): Waits for network connectivity
OSSimpleTaskCreatewName(): Creates the socket handling task
Message Format
The application sends periodic status messages in the format:
TimeTick: [current_tick_value]
Where [current_tick_value] is the current system tick count, providing a timestamp for each message.
Error Handling
The application includes robust error handling:
- Socket error detection using
select() with error file descriptor sets
- Automatic socket cleanup on connection failure
- Graceful task termination when connection is lost
Build Requirements
- NetBurner development environment
- WebSocket library (
websockets.h)
- Standard C libraries
- NetBurner initialization framework (
init.h)
Usage
- Configure the
SERVER_IP define to match your WebSocket server
- Build and deploy to your NetBurner device
- The application will automatically:
- Initialize the network stack
- Connect to the specified WebSocket server
- Begin sending periodic time tick messages
- Display any received messages on the console
Technical Details
- Task Priority: Socket handling task runs at
MAIN_PRIO + 1
- Buffer Size: Uses
ETHER_BUFFER_SIZE for data reception
- Timing: Uses
TICKS_PER_SECOND for consistent timing intervals
- Diagnostics: Includes system diagnostics (should be disabled for production)