This repository contains an example library for single threaded client/server programs based on web sockets. The server is built using boost beast and C++. Clients support either boost beast or WebAssembly using Emscripten. Multiple clients and the server can transfer simple string messages back and forth. Because the API is single threaded, it integrates easily into a main update loop, e.g. for a game. Because it is web socket based, it supports both native and browser based clients at the same time. Examples of both are provided.
Note: Both the creation of the communication channel as well as all communication between the client and the server is insecure. It is trivially subject to interception and alteration, and it should not be used to transmit sensitive information of any sort.
In addition, a simple chat server is provided as well as multiple clients using NCurses, FTXUI with C++ or WebAssembly, and Flutter.
This project requires:
- C++17 or newer
- Boost >= 1.72
- CMake >= 3.24
- NCurses (only tested with 6.1) [Optional]
- FTXUI >= 5.0 [Optional]
- Flutter >= 3.0 [Optional]
-
Clone the repository.
git clone https://github.com/nsumner/web-socket-networking.git
-
Create a new directory for building.
mkdir networkbuild
-
Change into the new directory.
cd networkbuild
-
Run CMake with the path to the source.
cmake ../web-socket-networking/
-
Run make inside the build directory:
make
This produces chat server and client tools called bin/chatserver
and
bin/client-ncurses
respectively. The library for single threaded clients and
servers is built in lib/
.
Note, building with a tool like ninja can be done by adding -G Ninja
to
the cmake invocation and running ninja
instead of make
.
First run the chat server on an unused port of the server machine. The server
also takes an HTML file that it will server to standard http requests for
index.html
.
bin/chatserver 8000 ../web-socket-networking/webchat.html
In separate terminals, run multiple instances of the chat client using:
bin/client-ncurses localhost 8000
This will connect to the given port (8000 in this case) of the local machine.
Connecting to a remote machine can be done by explicitly using the remote
machine's IP address instead of localhost
. Inside the chat client, you can
enter commands or chat with other clients by typing text and hitting the
ENTER key. You can disconnect from the server by typing quit
. You can shut
down the server and disconnect all clients by typing shutdown
. Typing
anything else will send a chat message to other clients.
A browser based interface can be accessed by opening the URL
http://localhost:8000/index.html
. The server will respond with the
specified web page above. By clicking Connect
, the page gains access to
chat on the server via web sockets in browsers that support web sockets.
If you have Flutter installed, then a very simple chat client in Flutter will also be built inside the build directory. From the build directory, this can be run using:
cd flutterclient
flutter run
You can then enter the chat server location into the client to connect to the server and start chatting. For instance, to connect to the server configured in the previous example, we could enter:
ws://localhost:8000
to connect to the local server on port 8000.
If you have emscripten installed, then you can configure and build a C++ client that runs in the browser with WebAssembly. The GUI is driven by FTXUI, and the browser client should behave the same as the native client.
Building is similar to a normal CMake process:
-
Create a new directory specifically for building with emscripten.
mkdir embuild
-
Change into the new directory.
cd networkbuild
-
Run the emscripten CMake wrapper with the path to the source.
emcmake cmake ../web-socket-networking/
-
Run the emscripten make wrapper inside the build directory:
emmake make
Running the client requires starting a simple web server.
cd bin/
./run_webassembly.py