You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The library requires the Boost Beast and Jsoncpp libraries to handle WebSocket communications and JSON data manipulation respectively. Install these dependencies using CMake if they are not already present on your system.
6
+
7
+
### Required Libraries
8
+
-**Boost Beast** (version >= 1.76.0): Used for WebSocket implementation. Available at [boost.org](https://www.boost.org/).
9
+
-**Jsoncpp**: Used for JSON parsing and serialization. Known for its performance and ease of use in C++ environments.
10
+
11
+
## Examples
12
+
Two simple examples are provided demonstrating the Library in a simple raw C and a C++ classed implementation. Compile the examples using:
13
+
```bash
14
+
- mkdir build &&cd build
15
+
- cmake ..
16
+
- make
17
+
- ./example_client_simple or ./example_client_classed
18
+
```
19
+
20
+
## Classes
21
+
### Connection
22
+
The `Connection` class manages WebSocket connections using Boost Beast. It handles asynchronous read, write, and connection management activities necessary for real-time communication.
23
+
24
+
#### Constructor
25
+
-`Connection(net::io_context& ioc)`: Initializes a new connection with a given I/O context. This context is used to handle all I/O operations for the WebSocket.
26
+
27
+
#### Methods
28
+
-`std::thread launch_socket(const char *host, const char *port)`: Starts the WebSocket connection on a separate thread.
29
+
-`void stop()`: Stops the WebSocket connection and cleans up resources.
30
+
-`void subscribe(std::string channel, socketCallback callback)`: Subscribes to a specific channel with a callback to handle incoming messages.
31
+
-`void unsubscribe(std::string channel)`: Unsubscribes from a specific channel.
32
+
-`void publish(std::string channel, std::string data)`: Publishes data to a specific channel.
33
+
-`void message_processing()`: Handles the internal message processing in its thread.
34
+
35
+
#### Callbacks
36
+
-`socketCallback`: A function type that handles incoming messages. Takes an event as `std::string` and data as `Json::Value`.
37
+
38
+
#### Members
39
+
-`websocket::stream<beast::tcp_stream> ws_`: WebSocket stream for the connection.
40
+
-`beast::flat_buffer buffer_`: Buffer used for reading WebSocket messages.
41
+
42
+
### SocketClusterClient
43
+
44
+
The `SocketClusterClient` class manages multiple WebSocket connections and provides methods to create and retrieve these connections.
45
+
46
+
#### Constructor
47
+
-`SocketClusterClient()`: Initializes a new client capable of handling WebSocket connections.
48
+
49
+
#### Methods
50
+
-`std::shared_ptr<Connection> createConnection(const char *url, const char *port)`: Creates and returns a new connection to the specified URL and port.
51
+
-`std::list<std::shared_ptr<Connection>>& getConnections()`: Returns a list of all active connections.
52
+
53
+
#### Members
54
+
-`std::list<std::shared_ptr<Connection>> m_connections`: List storing all managed connections.
55
+
56
+
57
+
## Errors & Exceptions
58
+
-`1000 - Normal Closure` : Connection closed successfully.
59
+
-`1001 - Going Away` : Server or client is shutting down.
-`1003 - Unsupported Data` : The endpoint received data of a type it cannot accept.
62
+
-`1006 - Abnormal Closure` : Connection closed abnormally without a status code.
63
+
64
+
65
+
## Example Usage of SocketClusterClient Library
66
+
67
+
This example demonstrates how to use the `SocketClusterClient` and `Connection` classes to connect to a WebSocket server, subscribe to a channel, and handle incoming messages.
68
+
69
+
```cpp
70
+
#include<iostream>
71
+
#include"SocketClusterClient.h"
72
+
73
+
// Define a callback function to handle messages received on the WebSocket
This repository hosts a [SocketCluster](https://socketcluster.io/) C++ Client designed to facilitate communication between C++ applications and SocketCluster servers. This client supports real-time, scalable, bi-directional communication, making it ideal for applications requiring high performance and efficient data exchange.
4
+
S
5
+
This client is developed using the [Boost Beast](https://github.com/boostorg/beast) and [jsoncpp](https://github.com/open-source-parsers/jsoncpp) libraries in C++.
6
+
7
+
## Features
8
+
9
+
-**Real-Time Communication**: Enables real-time connectivity with SocketCluster servers.
10
+
-**Bi-Directional Communication**: Supports both sending and receiving messages efficiently.
11
+
-**Scalability**: Designed to handle high-load scenarios, making it suitable for large-scale deployments.
12
+
-**WebSocket Support**: Utilizes WebSockets for low-latency communication.
13
+
14
+
15
+
## Getting Starting
16
+
A detailed list of the libraries API can be found [here](API.md).
17
+
18
+
## Security / SSL
19
+
#### Tokens
20
+
To address the threat of unauthenticated connections, we will utilize SocketCluster's built-in JWT-based authentication mechanism. Each JWT is uniquely signed using a server-specific authKey, ensuring secure and verified connections right from the initial handshake. Follow the guide [here](https://socketcluster.io/docs/authentication/) to enable JWTs with SocketCluster.
21
+
#### WSS
22
+
To allow development and production runs a flag can be set to enable SSL assuming the SocketCluster server has been configured to accept SSL connections. To best achieve this a flag can be set in the client to put the data transfer into the secure mode.
23
+
```cpp
24
+
#defineSOCKETCLUSTER_SLL 1
25
+
```
26
+
27
+
## Performance
28
+
The maximum output and input rates have not been tested yet. This document will be updated with statistics after tests have been run. **Both example programs have been profiled and show no memory leaks.**
29
+
30
+
The SocketCluster server has been thoroughly tested in an [academic paper](https://arxiv.org/pdf/1409.3367.pdf). This client library aims to match the results listed.
0 commit comments