This project demonstrates a WebSocket server and client application pair written in C++. The server broadcasts messages received from clients to all connected clients using WebSocket (TCP) and allows the client to broadcast these messages over UDP to a specified endpoint.
- C++ compiler that supports C++11 or later
- Boost C++ Libraries (specifically Boost.Asio)
websocketpplibrary
-
Install Dependencies:
- Ensure you have Boost installed. If not, you can download it from Boost website and follow their installation instructions.
websocketppcan be downloaded or cloned from its GitHub repository.
-
Compilation:
- Compile
websocket_server.cppandwebsocket_client.cppwith your C++ compiler. For example, using g++:g++ -std=c++11 -I path/to/websocketpp -lboost_system -lboost_thread websocket_server.cpp -o websocket_server g++ -std=c++11 -I path/to/websocketpp -lboost_system -lboost_thread websocket_client.cpp -o websocket_client
- Compile
websocket_msg_server.cppandwebsocket_msg_client.cppsimilarly:g++ -std=c++11 -I path/to/websocketpp -lboost_system -lboost_thread websocket_msg_server.cpp -o websocket_msg_server g++ -std=c++11 -I path/to/websocketpp -lboost_system -lboost_thread websocket_msg_client.cpp -o websocket_msg_client
- Compile
-
Running the WebSocket Server:
- Run the compiled
websocket_serverexecutable. It listens on port9002by default../websocket_server
- The server accepts WebSocket connections from clients and broadcasts received messages to all connected clients.
- Run the compiled
-
Connection Method:
- The server initializes a WebSocket server using
websocketppwith Boost.Asio configuration (asio_no_tls). - It listens for WebSocket connections on port
9002.
- The server initializes a WebSocket server using
-
Threading:
- Threading is managed internally within the WebSocket server using
std::thread. Therun()method starts a separate thread for the WebSocket server event loop (m_server.run()).
- Threading is managed internally within the WebSocket server using
-
Protocols Used:
- WebSocket (TCP): The server communicates with WebSocket clients using the WebSocket protocol over TCP/IP.
-
WebSocket Server Initialization (
WebSocketServerclass inwebsocket_server.cpp):- Initializes a WebSocket server using
websocketppand Boost.Asio (websocketpp::config::asio). - Handles WebSocket connection events (
on_open,on_close,on_message) to manage client connections and message broadcasting.
- Initializes a WebSocket server using
-
Event Handlers (
WebSocketServerclass):on_open(connection_hdl hdl): Called when a new WebSocket connection is established. Adds the connection to the list of active connections.on_close(connection_hdl hdl): Called when a WebSocket connection is closed. Removes the connection from the list of active connections.on_message(connection_hdl hdl, server::message_ptr msg): Called when a message is received from a WebSocket client. Broadcasts the message to all connected clients.
-
Thread Management:
- Uses
std::threadto manage concurrent execution of WebSocket server operations (m_server.run()).
- Uses
-
Running the WebSocket Client:
- Run the compiled
websocket_clientexecutable. Ensure it is provided with appropriate command-line arguments../websocket_client
- The client connects to the WebSocket server (
ws://localhost:9002by default) and broadcasts received messages over UDP to127.0.0.1:9003.
- Run the compiled
-
Connection Method:
- The client establishes a WebSocket connection (
ws://localhost:9002) using thewebsocketpplibrary'sasio_clientconfiguration.
- The client establishes a WebSocket connection (
-
Threading:
- Threading is managed internally within the WebSocket client using
std::thread. Therun()method starts a separate thread for the WebSocket client event loop (m_client.run()).
- Threading is managed internally within the WebSocket client using
-
Protocols Used:
- WebSocket (TCP): The client communicates with the WebSocket server using the WebSocket protocol over TCP/IP.
- UDP: Received WebSocket messages are broadcasted over UDP to the specified endpoint (
127.0.0.1:9003).
-
WebSocket Client Initialization (
WebSocketClientclass inwebsocket_client.cpp):- Initializes a WebSocket client using
websocketppand Boost.Asio (websocketpp::config::asio_client). - Connects to the WebSocket server (
ws://localhost:9002by default). - Sets up UDP socket (
boost::asio::ip::udp::socket) and endpoint (boost::asio::ip::udp::endpoint) for broadcasting received WebSocket messages.
- Initializes a WebSocket client using
-
Event Handlers (
WebSocketClientclass):on_open(websocketpp::connection_hdl hdl): Triggered when the WebSocket connection is established.on_message(websocketpp::connection_hdl hdl, client::message_ptr msg): Triggered when a message is received from the WebSocket server. Broadcasts the message over UDP.
-
Thread Management:
- Uses
std::threadto manage concurrent execution of WebSocket client operations (m_client.run()).
- Uses
-
Running the WebSocket Message Server:
- Run the compiled
websocket_msg_serverexecutable. It listens on port9002by default../websocket_msg_server
- The server accepts WebSocket connections from clients, logs received messages, and broadcasts them to all connected clients.
- Run the compiled
-
Logging Messages:
- The server logs the body of each message received from clients.
-
WebSocket Server Initialization (
WebSocketMsgServerclass inwebsocket_msg_server.cpp):- Initializes a WebSocket server using
websocketppand Boost.Asio (websocketpp::config::asio). - Handles WebSocket connection events (
on_open,on_close,on_message) to manage client connections, log received messages, and broadcast messages to all connected clients.
- Initializes a WebSocket server using
-
Event Handlers (
WebSocketMsgServerclass):on_open(connection_hdl hdl): Called when a new WebSocket connection is established. Adds the connection to the list of active connections.on_close(connection_hdl hdl): Called when a WebSocket connection is closed. Removes the connection from the list of active connections.on_message(connection_hdl hdl, server::message_ptr msg): Called when a message is received from a WebSocket client. Logs the message body and broadcasts it to all connected clients.
-
Running the WebSocket Message Client:
- Run the compiled
websocket_msg_clientexecutable../websocket_msg_client
- The client connects to the WebSocket server (
ws://localhost:9002by default) and broadcasts received messages over UDP to127.0.0.1:9003.
- Run the compiled
-
Broadcasting Messages:
- The client sends input messages over UDP and WebSocket and consumes broadcasted messages from the server.
-
WebSocket Client Initialization (
WebSocketMsgClientclass inwebsocket_msg_client.cpp):- Initializes a WebSocket client using
websocketppand Boost.Asio (websocketpp::config::asio_client). - Connects to the WebSocket server (
ws://localhost:9002by default). - Sets up UDP socket (
boost::asio::ip::udp::socket) and endpoint (boost::asio::ip::udp::endpoint) for broadcasting received WebSocket messages.
- Initializes a WebSocket client using
-
Event Handlers (
WebSocketMsgClientclass):on_open(websocketpp::connection_hdl hdl): Triggered when the WebSocket connection is established.on_message(websocketpp::connection_hdl hdl, client::message_ptr msg): Triggered when a message is received from the WebSocket server. Logs and broadcasts the message over UDP.
-
Thread Management:
- Uses
std::threadto manage concurrent execution of WebSocket client operations (m_client.run()).
- Uses
-
Additional Functionality:
- Logs received messages.
- Handles input from the console and sends it over both UDP and WebSocket.
This project demonstrates a WebSocket server-client pair where the server broadcasts messages to all connected clients, and the client broadcasts received messages over UDP. The enhanced versions (websocket_msg_server.cpp and websocket_msg_client.cpp) include logging of received messages and functionality to send and consume messages over both.