This project provides a WebSocket server and client implementation in Rust. The server manages multiple chat rooms, allowing clients to create rooms, join rooms, send messages, and leave rooms. The client connects to the server and interacts with the chat rooms.
-
WebSocket Server:
- Manages chat rooms.
- Handles client connections.
- Broadcasts messages to clients in the same room.
-
WebSocket Client:
- Connects to the WebSocket server.
- Allows users to create or join rooms.
- Sends and receives messages in real-time.
- Rust (1.60 or later)
- Cargo (included with Rust)
First, clone the repository and navigate to the project directory:
git clone https://github.com/SuryodayDevHub/websocket_rust.git
cd websocket_rust
Make sure you have the following dependencies in your Cargo.toml
:
[dependencies]
tokio = { version = "1", features = ["full"] }
tokio-tungstenite = "0.17"
tungstenite = "0.15"
futures = "0.3"
futures-util = "0.3"
To start the WebSocket server, use the following command:
cargo run -- server
The server will listen for connections on ws://127.0.0.1:8080.
To start the WebSocket client, use the following command:
cargo run -- client
The client will connect to the WebSocket server at ws://127.0.0.1:8080.
To run both the server and the client simultaneously, use the following command:
cargo run -- both
This command will start both the server and the client in separate tasks.
Once the client is running, you can interact with it using the following commands:
- Create a Room: Type
CREATE <room_name>
to create and join a new chat room. - Join a Room: Type
JOIN <room_name>
to join an existing chat room. - Leave a Room: Type
/leave
to leave the current chat room. - Send a Message: Type your message and press Enter to send it to the current chat room.
-
Start the server in one terminal:
cargo run -- server
-
In another terminal, start the client:
cargo run -- client
-
Follow the prompts in the client to create or join a chat room and send messages.
main.rs
: Entry point for running the server, client, or both.server.rs
: Contains the WebSocket server logic.client.rs
: Contains the WebSocket client logic.
Contributions are welcome! Feel free to open issues or submit pull requests to enhance the project.
If you find this project useful and would like to support its continued development, you can make a donation via Buy Me a Coffee.
Thank you for your support!
This project is licensed under the MIT License. See the LICENSE file for more details.