#include <websocketconnection.h>
Public Member Functions | |
~WebSocketServerEndPointSetup () override | |
bool | init (utility::ErrorState &errorState) override |
bool | start (nap::utility::ErrorState &error) override |
bool | isOpen () const override |
void | stop () override |
bool | send (const WebSocketConnection &connection, const std::string &message, EWebSocketOPCode code, nap::utility::ErrorState &error) override |
bool | send (const WebSocketConnection &connection, void const *payload, int length, EWebSocketOPCode code, nap::utility::ErrorState &error) override |
bool | broadcast (const std::string &message, EWebSocketOPCode code, nap::utility::ErrorState &error) override |
bool | broadcast (void const *payload, int length, EWebSocketOPCode code, nap::utility::ErrorState &error) override |
std::string | getHostName (const WebSocketConnection &connection) override |
void | getHostNames (std::vector< std::string > &outHosts) override |
int | getConnectionCount () override |
bool | acceptsNewConnections () override |
Protected Member Functions | |
void | registerListener (IWebSocketServer &server) override |
void | unregisterListener (IWebSocketServer &server) override |
void | run () |
void | onConnectionOpened (wspp::ConnectionHandle connection) |
void | onConnectionClosed (wspp::ConnectionHandle connection) |
void | onConnectionFailed (wspp::ConnectionHandle connection) |
void | onMessageReceived (wspp::ConnectionHandle con, wspp::MessagePtr msg) |
void | onHTTP (wspp::ConnectionHandle con) |
bool | onValidate (wspp::ConnectionHandle con) |
bool | onPing (wspp::ConnectionHandle con, std::string msg) |
bool | disconnect (nap::utility::ErrorState &error) |
Protected Attributes | |
bool | mRunning = false |
If the server is accepting and managing client connections. More... | |
std::mutex | mConnectionMutex |
Ensures connections are added / removed safely. More... | |
std::unordered_set< WebSocketTicketHash > | mClientHashes |
Accepted client ticket hashes. More... | |
websocketpp::server< config > | mEndPoint |
The websocketpp server end-point. More... | |
uint32 | mLogLevel = 0 |
Converted library log level. More... | |
uint32 | mAccessLogLevel = 0 |
Log client / server connection data. More... | |
std::future< void > | mServerTask |
The background server thread. More... | |
std::vector< wspp::ConnectionHandle > | mConnections |
List of all low level connections. More... | |
std::mutex | mListenerMutex |
Ensures registration is thread safe. More... | |
std::vector< IWebSocketServer * > | mListeners |
All registered web socket servers. More... | |
Web socket server endpoint implementation using a specific configuration. Manages all client connections, where the config
is the websocketpp (secured / unsecured) end-point configuration type.
On start the web-socket endpoint starts listening to connection requests, updates and messages on a background thread. The endpoint is a device that can be started and stopped. When stopped all active client-server connections are closed. This occurs when file changes are detected and the content of the application is hot-reloaded. A call to open is non blocking. Messages are forwarded to all clients that implement the nap::IWebSocketClient interface. Clients must register themselves to the various signals to receive connection updates and messages. Note that depending on your operating system you might have to run the application as administrator to open a web-socket.
By default the server accepts all client connection requests. Change the 'AccessMode' property to 'Ticket' or 'Reserved' to add a client identification scheme. When set to 'Ticket' the server accepts every client connection with a ticket. When set to 'Reserved' the server only accepts the connection if it has a matching ticket. Tickets that are accepted by the server can be added to the 'Clients' property.
A good analogy is that of attending a concert. When the mode is set to 'Everyone' you don't need to pick up a ticket in order to enter the venue. You can just walk in. When the mode is set to 'Ticket' you need to buy a ticket and show it at the front door. Everyone can buy a ticket. When the mode is set to 'Reserved' you need to be on the guest list. If you're not on the guest-list you are not allowed to buy a ticket, let alone enter the venue.
For both modes (Ticket and Reserved) you need to acquire a ticket by sending a HTTP 'POST' request to the server. The body of the post should contain a JSON formatted string that contains 2 fields: 'user' and 'pass'. Both fields are always required. For example:
The server generates a new ticket based on the provided information and sends it back to the client that made the request. The received ticket should be specified as the first SUB-PROTOCOL argument when creating the web-socket. The server extracts the ticket on authorization and checks if it is valid. After validation the connection is accepted or rejected. Note that when the mode is set to 'Everyone' the server will not serve any tickets and you should not provide the system with one when connecting to the server. In that case: connect without specifying a sub-protocol.
The reason for having the 'Ticket' validation mode (next to 'Reserved') is to prevent any user or bot to log in automatically but allow 'interested' users free access. Every nap::WebSocketClient can make it's own ticket and therefore doesn't require the http post request. The NAP client still needs to have a correct username and password if required by the server.
|
override |
|
override |
|
override |
Sends a message to a all connected clients.
message | the message to send |
code | message type |
error | contains the error if sending fails |
|
override |
Broadcasts a message to all connected clients using the given payload and opcode.
payload | the message buffer |
length | size of the buffer in bytes |
code | message type |
error | contains the error if sending fails |
|
protected |
Closes all active client connections.
|
override |
|
override |
|
override |
Returns a list of all currently connected client host-names.
outHosts | the list of connected client host-names. |
|
override |
Initializes the server endpoint.
errorState | contains the error when initialization fails |
|
override |
|
protected |
Called when a client collection is closed
|
protected |
Called on a failed client connection attempt.
|
protected |
Called when a new client connection opened.
|
protected |
Called when a http request is made. Creates and serves a ticket based on the current Access Mode.
|
protected |
Called when a new client message is received.
|
protected |
Called when the server receives a ping message. Automatically pongs back.
|
protected |
Validates the incoming connection. For now all connections are accepted.
|
overrideprotected |
Register a server for this endpoint so that it receives notifications from the endpoint.
server | the server to register |
|
protected |
Runs the end point in a background thread until stopped.
|
override |
Sends a message to a client.
connection | the client connection |
message | the message to send |
code | message type |
error | contains the error if sending fails |
|
override |
Sends a message to a client using the given payload and opcode.
connection | the client connection |
payload | the message buffer |
length | size of the buffer in bytes |
code | message type |
error | contains the error if sending fails |
|
override |
Opens the port and starts listening to connection requests, connection updates and messages in a background thread.
error | contains the error if the operation failed. |
|
override |
Stops the end-point from running, all active connections are closed.
|
overrideprotected |
Unregister a server for this endpoint so that it stops receiving notifications from the endpoint.
server | the server to unregister |
|
protected |
Log client / server connection data.
|
protected |
Accepted client ticket hashes.
|
protected |
Ensures connections are added / removed safely.
|
protected |
List of all low level connections.
|
protected |
The websocketpp server end-point.
|
protected |
Ensures registration is thread safe.
|
protected |
All registered web socket servers.
|
protected |
Converted library log level.
|
protected |
If the server is accepting and managing client connections.
|
protected |
The background server thread.