A minimal WebSocket server and client implementation for tracking and managing connection statuses.
This project implements a lightweight WebSocket server with a simple status management system. It allows real-time status tracking between a server and client, with three primary states: Active, Sharing, and Inactive.
- 🚀 Simple WebSocket server implementation
- 🔄 Real-time status tracking
- 📡 Multiple status states
- 🔒 Thread-safe global status management
- Language: Rust
- WebSocket Library: tungstenite
- Serialization: serde
- Frontend: Vanilla JavaScript
.
├── src/
│ ├── lib.rs # Status management and shared types
│ └── main.rs # WebSocket server implementation
├── index.html # Client-side WebSocket interface
└── Cargo.toml # Project dependencies and configuration
The project defines three distinct statuses:
Active
: Connection is established and activeSharing
: Specific sharing modeInactive
: No active connection
- Rust (latest stable version)
- Cargo package manager
-
Clone the repository
-
Navigate to the project directory
-
Build the project:
cargo build --release
cargo run
The server will start on ws://localhost:8235
Open index.html
in a web browser:
- Input text and click "Send"
- Automatically sets status based on input
- Logs server responses in the console
- Manages global connection status
- Handles WebSocket connections
- Broadcasts status changes
- Establishes WebSocket connection
- Sends status updates
- Receives and processes server status messages
tungstenite
: WebSocket protocol implementationserde
: Serialization and deserializationonce_cell
: Lazy static initializationserde_json
: JSON parsing
// Setting status
set_status(&Status::Active);
// Getting current status
let current_status = get_brwoser_activity_status();
{
"status": "active" | "sharing" | "inactive"
}
- Graceful handling of connection errors
- Detailed error logging
- Automatic status reset on connection issues
- Add authentication
- Implement more robust error handling
- Create a more feature-rich client interface
- Add logging mechanisms
[Specify your license here, e.g., MIT, Apache 2.0]